- 建一个数据库文件
MyHelper
package com.example.test3;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class MyHelper extends SQLiteOpenHelper {
public MyHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
/*public MyHelper(@Nullable Context context, @Nullable String name) {
super(context, name, null, 1);
}
public MyHelper(@Nullable Context context) {
super(context, "user.db", null, 1);
}*/
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String sql = "create table user(_id integer primary key autoincrement,username text,password text)";
sqLiteDatabase.execSQL(sql);//执行上述sql语句建表过程
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
1. 创建登录注册界面,功能界面(简化界面,全部在一个界面)
activity_sqlite_demo
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SQLiteDemoActivity">
<ImageView
android:id="@+id/sql_im1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/teal_700"></ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="USER"
android:textSize="30sp"
android:textColor="#fff"
android:layout_centerHorizontal="true"></TextView>
<EditText
android:id="@+id/sql_et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:hint="用户名"
android:layout_marginTop="10dp"
android:layout_below="@id/sql_im1"></EditText>
<EditText
android:id="@+id/sql_et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:hint="密码"
android:layout_marginTop="10dp"
android:layout_below="@id/sql_et1"></EditText>
<LinearLayout
android:id="@+id/sql_lly1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/sql_et2"
android:layout_marginTop="20dp"
android:padding="20dp">
<Button
android:id="@+id/sql_btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="添加"
android:layout_marginRight="10dp"></Button>
<Button
android:id="@+id/sql_btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="查询"
android:layout_marginRight="10dp"></Button>
<Button
android:id="@+id/sql_btn3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="修改"
android:layout_marginRight="10dp"></Button>
<Button
android:id="@+id/sql_btn4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="删除"
android:layout_marginRight="10dp"></Button>
</LinearLayout>
<TextView
android:id="@+id/sql_tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/sql_lly1"
android:layout_marginTop="20dp"
android:text="用户:"></TextView>
</RelativeLayout>
SQLiteDemoActivity
package com.example.test3;
import androidx.appcompat.app.AppCompatActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.StringTokenizer;
public class SQLiteDemoActivity extends AppCompatActivity implements View.OnClickListener {
private EditText ed1,ed2;
private Button btn_add,btn_query,btn_modify,btn_delete;
private TextView tv1;
private MyHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sqlite_demo);
helper = new MyHelper(this,"user.db",null,1);
init();
}
public void init(){
ed1 = findViewById(R.id.sql_et1);
ed2 = findViewById(R.id.sql_et2);
btn_add = findViewById(R.id.sql_btn1);
btn_query = findViewById(R.id.sql_btn2);
btn_modify = findViewById(R.id.sql_btn3);
btn_delete = findViewById(R.id.sql_btn4);
tv1 = findViewById(R.id.sql_tv1);
btn_delete.setOnClickListener(this);
btn_query.setOnClickListener(this);
btn_modify.setOnClickListener(this);
btn_add.setOnClickListener(this);
}
@Override
public void onClick(View view) {
SQLiteDatabase db;
String username,password;
String sql;
switch (view.getId()){
case R.id.sql_btn1:
db = helper.getWritableDatabase();
username = ed1.getText().toString();
password = ed2.getText().toString();
sql = "insert into user(username,password) values ('"+username+"','"+password+"')";
db.execSQL(sql);
Toast.makeText(this,"添加成功",Toast.LENGTH_SHORT).show();
db.close();
break;
case R.id.sql_btn2:
tv1.setText("");
db = helper.getReadableDatabase();
sql = "select username,password from user";
Cursor cursor = db.rawQuery(sql,null);
while (cursor.moveToNext()){
int index = cursor.getColumnIndex("username");
int index2 = cursor.getColumnIndex("password");
String username1 = cursor.getString(index);
String password1 = cursor.getString(index2);
tv1.append(username1+":"+password1+"\n");
}
cursor.moveToFirst();
cursor.close();
db.close();
break;
case R.id.sql_btn3:
db = helper.getWritableDatabase();
username = ed1.getText().toString();
password = ed2.getText().toString();
sql = "update user set password ='" + password + "' where username = '" + username + "'";
db.execSQL(sql);
Toast.makeText(this,"修改了",Toast.LENGTH_SHORT).show();
db.close();
break;
case R.id.sql_btn4:
db = helper.getWritableDatabase();
username = ed1.getText().toString();
sql ="delete from user where username = "+username;
sql = "delete from user where username = '" + username + "'";
db.execSQL(sql);
Toast.makeText(this,"删除了",Toast.LENGTH_SHORT).show();
db.close();
break;
}
}
}