设计思路
设计一个MyDatabaseHelper类继承帮助类SQLiteOpenHelper,先创建表的SQL命令,然后重载onCreate()函数和onUpgrade()函数。onCreate()函数在数据库第一次建立时被调用,一般用来创建数据库中的表,并完成初始化,在里面调用SQLiteDatabase实例的execSQL()方法,执行创建表的SQL命令。onUpgrade()函数在数据库升级时被调用,一般用来删除旧的表,并将数据转移到新版本的表中。
设计一个People类,用于将查询结果转换为自定义的People类实例。
在MainActicity中,先以读写方式打开数据库,再初始化控件,并为各个按钮设置监听器。当点击“添加数据”按钮时,先判断姓名、年龄、身高是否为空,若为空,则提示填入完整信息;若填写完整,则构造一个ContentValues()实例,调用put()方法,将每个属性的值写入。当点击“全部显示”按钮时,使用Cursor类作为返回数据集的指针,判断查询结果是否为空,再遍历表,将数据库信息显示到textView。当点击“清除显示”按钮时,直接将textView清空。当点击“全部删除”按钮时,调用delete()方法将数据库中的数据清空。当点击“ID删除”按钮时,先判断Id是否为空,主要方法与“全部显示”按钮相同,多添加一个根据id删除对应数据。当点击“ID更新”按钮时,先找到需要更新的数据,然后弹出自定义的AlertDialog,通过update()方法对相应数据进行更新。
运行结果
代码
MainActivity.java
package com.example.test64;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private EditText eName,eAge,eHeight,eId;
private Button addButton,AsButton,DeleteButton,AdButton;
private Button IdDelete,IdSelect,IdUpdate;
private TextView textView;
private MyDatabaseHelper databaseHelper;
private SQLiteDatabase db;
private List<People> peoples=new ArrayList<People>();
private int findId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseHelper=new MyDatabaseHelper(this,"people.db",null,1);
//以读写方式打开数据库
db=databaseHelper.getWritableDatabase();
//初始化控件
getId();
//设置监听器
//添加数据按钮
addButton.setOnClickListener(this);
//全部显示
AsButton.setOnClickListener(this);
//清除显示
DeleteButton.setOnClickListener(this);
//全部删除
AdButton.setOnClickListener(this);
//ID删除
IdDelete.setOnClickListener(this);
//ID查询
IdSelect.setOnClickListener(this);
//ID更新
IdUpdate.setOnClickListener(this);
}
//初始化控件
private void getId(){
eName=(EditText)findViewById(R.id.editText8);
eAge=(EditText)findViewById(R.id.editText9);
eHeight=(EditText)findViewById(R.id.editText7);
eId=(EditText)findViewById(R.id.editText10);
addButton=(Button)findViewById(R.id.button9);
AsButton=(Button)findViewById(R.id.button12);
DeleteButton=(Button)findViewById(R.id.button13);
AdButton=(Button)findViewById(R.id.button14);
IdDelete=(Button)findViewById(R.id.button15);
IdSelect=(Button)findViewById(R.id.button16);
IdUpdate=(Button)findViewById(R.id.button17);
textView=(TextView)findViewById(R.id.textView5);
}
//设置每个按钮对数据库的操作进行控制
public void onClick(View v){
switch (v.getId()){
//添加数据
case R.id.button9:
addData();
eName.setText("");
eAge.setText("");
eHeight.setText("");
break;
//全部显示
case R.id.button12:
ShowAllData();
break;
//清除显示
case R.id.button13:
ClearView();
break;
//全部删除
case R.id.button14:
deleteAll();
break;
//ID删除
case R.id.button15:
findId=getFindId();
deleteId(findId);
ShowAllData();
eId.setText("");
break;
//ID查询
case R.id.button16:
findId=getFindId();
selectId(findId);
eId.setText("");
break;
//ID更新
case R.id.button17:
findId=getFindId();
updateId(findId);
eId.setText("");
break;
default:
break;
}
}
//添加数据
private void addData(){
//判断姓名、年龄、身高是否为空
if(eName.getText().toString().equals("")
&&eAge.getText().toString().equals("")
&&eHeight.getText().toString().equals("")){
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("提示");
builder.setMessage("请输入完整信息!");
builder.setCancelable(false);
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick