Android开发之sqlite数据库
效果:sqlite的创建,增删改查
3步走:配置文件AndroidManifest.xml无改动,此处不展示。
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
/**
* 建立sqlite数据库,进行用户名和密码的存储
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
//编辑框,输入
EditText username;
EditText password;
EditText selectuser;
//结果文本框
TextView result;
//3个按钮
Button buttonadd;
Button buttondelete;
Button buttonselect;
//帮助建立sqlite数据库
SQLiteOpenHelper mysqlhelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
buttonadd.setOnClickListener(this);
buttondelete.setOnClickListener(this);
buttonselect.setOnClickListener(this);
}
public void init(){
username = findViewById(R.id.username);
password = findViewById(R.id.password);
selectuser = findViewById(R.id.selectuser);
result = findViewById(R.id.result);
buttonadd = findViewById(R.id.buttonadd);
buttondelete = findViewById(R.id.buttondelete);
buttonselect = findViewById(R.id.buttonselect);
// mysqlhelper = new MyDBHelper(MainActivity.this, "MyDatabase.db", null, 666);
// //SQLiteDatabase db = mysqlhelper.getWritableDatabase();
}
@Override
public void onClick(View v) {
mysqlhelper = new MyDBHelper(MainActivity.this, "MyDatabase.db", null, 666);
SQLiteDatabase db;
String username1 = null;
String password1 = null;
Log.v("start","按钮事件:");
switch(v.getId()){
case R.id.buttonadd:
db = mysqlhelper.getWritableDatabase();
username1 = username.getText().toString();
password1 = password.getText().toString();
if(username1==null&&username1==""&&username1==" "){
Toast.makeText(MainActivity.this,"用户名不能为空",Toast.LENGTH_LONG).show();
Log.v("start","添加失败");
}else if(password1==null){
Toast.makeText(MainActivity.this,"密码不能为空",Toast.LENGTH_LONG).show();
}else {
db.execSQL("insert into user(username,password) values(?,?)", new Object[]{username1, password1});
Toast.makeText(MainActivity.this,"添加成功",Toast.LENGTH_LONG).show();
result.setText("查询结果为:");
result.append("用户名"+username1+"密码"+password1);
}
db.close();
break;
case R.id.buttondelete:
db = mysqlhelper.getWritableDatabase();
String usernamed = selectuser.getText().toString();
db.execSQL("delete from user where username=?",new Object[]{usernamed});
db.close();
Toast.makeText(MainActivity.this,"删除成功",Toast.LENGTH_LONG).show();
break;
case R.id.buttonselect:
result.setText("查询结果为:");
db = mysqlhelper.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from user", null);
while (cursor.moveToNext()){
result.append("\n" + "第" + cursor.getInt(0) +
",用户名:" + cursor.getString(1) +
",密码是:" + cursor.getString(2));
}
cursor.close();
db.close();
Toast.makeText(MainActivity.this,"查询成功",Toast.LENGTH_LONG).show();
Log.v("start","查询成功");
break;
}
}
/**
* sqlite数据库创建帮助类
*/
public class MyDBHelper extends SQLiteOpenHelper{
public MyDBHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE user(" +
"user_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"username VARCHAR(20),"+
"password VARCHAR(20)"+
")");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:layout_gravity="center"
android:background="@mipmap/ic_launcher"
android:layout_width="100dp"
android:layout_height="100dp">
</ImageView>
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名">
</EditText>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="输入密码">
</EditText>
<EditText
android:id="@+id/selectuser"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:hint="目标用户"></EditText>
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询结果"
android:textSize="25sp"
>
</TextView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/buttonadd"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="添加">
</Button>
<Button
android:id="@+id/buttondelete"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="删除">
</Button>
<Button
android:id="@+id/buttonselect"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="查询">
</Button>
</LinearLayout>
</LinearLayout>
More1:sqlite在哪看数值
在屏幕左侧View栏选择Tool Windows,就可以看到Device File Explorer。于是我们就得到了屏幕右侧的Device File Explorer,在data中的data中的com.example.项目名中,找到database中的db文件,将它导入到Navicat数据库管理工具中即可查看具体数据。
More2: 设计模式(代码处的巧妙之处)
下面这个implements View.OnClickListener有什么用呢:它将MainActivity变成它,在按钮监听的时候就不用每(1)次都去new了,巧妙利用选择switch语句(n)。