SQLiteOpenHelper数据库帮助器是Android提供的数据库辅助工具。
1、继承SQLiteOpenHelper类,需要重写onCreate和onUpgrade两个方法
案例:实现增删改查
package com.example.databases_text;import android.app.PictureInPictureParams;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.util.Rational;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;import com.example.databases_text.R;import java.util.List;public class MainActivity extends AppCompatActivity implements View.OnClickListener{private EditText edit_name;private EditText edit_password;private UserDBHelper mUserDBHelper;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);edit_name=findViewById(R.id.et_name);edit_password=findViewById(R.id.et_pws);findViewById(R.id.btn_add).setOnClickListener(this);findViewById(R.id.btn_del).setOnClickListener(this);findViewById(R.id.btn_update).setOnClickListener(this);findViewById(R.id.btn_query).setOnClickListener(this);}@Overrideprotected void onStart() {super.onStart();//获得数据库帮助器实例mUserDBHelper = UserDBHelper.getInstance(this);mUserDBHelper.openWriteLink();//打开数据库mUserDBHelper.openReadLink();}@Overrideprotected void onDestroy() {super.onDestroy();}@Overridepublic void onClick(View view) {User user = new User();String name=edit_name.getText().toString();String psw=edit_password.getText().toString();switch (view.getId()){case R.id.btn_add:user.setName(name);user.setPassword(psw);if( mUserDBHelper.InsertData(user)>0)Toast.makeText(this,"添加成功", Toast.LENGTH_SHORT).show();break;case R.id.btn_del:user.setName(name);user.setPassword(psw);if( mUserDBHelper.deleteData(user)>0)Toast.makeText(this,"删除成功", Toast.LENGTH_SHORT).show();break;case R.id.btn_update:user.setName(name);user.setPassword(psw);if( mUserDBHelper.updateData(user)>0)Toast.makeText(this,"修改成功", Toast.LENGTH_SHORT).show();break;case R.id.btn_query:List<User> userList=mUserDBHelper.queryData(name);if(userList !=null){edit_password.setText(userList.get(0).getPassword());//userList.get(x).getPassword()表示查询第几个的密码Toast.makeText(this,"查询成功", Toast.LENGTH_SHORT).show();}break;}}
}
package com.example.databases_text;public class User {private String name;private String password;public User() {}public User(String name, String password) {this.name = name;this.password = password;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}@Overridepublic String toString() {return "User{" +"name='" + name + '\'' +", password='" + password + '\'' +'}';}
}
package com.example.databases_text;import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;import java.util.ArrayList;
import java.util.List;public class UserDBHelper extends SQLiteOpenHelper {// 数据库版本号public static final int DATABASE_VERSION = 1;// 数据库名称public static final String DATABASE_NAME = "User.db";public static final String DATABASE_table_name = "uer_table";// 用户表名public static final String TABLE_NAME = "user";// 用户名public static final String COLUMN_NAME = "name";// 用户密码public static final String COLUMN_PASSWORD = "password";private static UserDBHelper instance=null;private SQLiteDatabase mRDB=null;private SQLiteDatabase mWDB=null;public UserDBHelper( Context context) {super(context, DATABASE_NAME, null, DATABASE_VERSION);}//利用单例模式,获取数据库的唯一实例,当数据库不存在的时候,经行创建,如果已存在就直接返回public static UserDBHelper getInstance(Context context){if(instance==null){instance=new UserDBHelper(context);}return instance;}//打开数据库的读连接public SQLiteDatabase openReadLink(){if(mRDB==null || !mRDB.isOpen()) {mRDB = instance.getReadableDatabase();}return mRDB;}//打开数据库的写连接public SQLiteDatabase openWriteLink(){if(mWDB==null || !mWDB.isOpen()) {mWDB = instance.getReadableDatabase();}return mWDB;}//数据库的关闭操作public void closeLink() {if (mRDB != null && mRDB.isOpen()) {mRDB.close();mRDB = null;}if (mWDB != null && mWDB.isOpen()) {mWDB.close();mWDB = null;//置为空为了回收}}//创建数据库,执行sql语句@Overridepublic void onCreate(SQLiteDatabase sqLiteDatabase) {String sql = "create table " + TABLE_NAME + " (" + COLUMN_NAME + " text, " + COLUMN_PASSWORD+ " text)";sqLiteDatabase.execSQL(sql);}@Overridepublic void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {}//添加数据到数据库public long InsertData(User user){ContentValues values = new ContentValues();values.put("name", user.getName());values.put("password", user.getPassword());return mWDB.insert(TABLE_NAME,null,values);}public long deleteData(User user){//删除所有的return mWDB.delete(TABLE_NAME,"name=? and password=?",new String[]{user.getName(),user.getPassword()});}public long updateData(User user){ContentValues values = new ContentValues();values.put("name", user.getName());values.put("password", user.getPassword());//更改所有符合条件的数据return mWDB.update(TABLE_NAME,values,"name=? ",new String[]{user.getName()});}public List<User> queryData(String name){List<User> list = new ArrayList<>();//查询所有数据,得到游标Cursor cursor =mWDB.query(TABLE_NAME,null,"name=? ",new String[]{name},null,null,null);//逐个取出游标指向的数据while (cursor.moveToNext()){User user1=new User();user1.setName(cursor.getString(0));user1.setPassword(cursor.getString(1));list.add(user1);}return list;}}
<?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"tools:context=".MainActivity"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="用户名"/><EditTextandroid:id="@+id/et_name"android:layout_width="match_parent"android:layout_height="wrap_content"/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="密码"/><EditTextandroid:id="@+id/et_pws"android:layout_width="match_parent"android:layout_height="wrap_content"/><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/btn_add"android:text="添加"/><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/btn_del"android:text="删除"/><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/btn_update"android:text="修改"/><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/btn_query"android:text="查询"/>
</LinearLayout>
2、onUpgrade()是在数据库版本更新时,执行操作。