一、数据库的创建
数据库版本为1
//Ctrl Shift U:大写 public static final String DATABASE_NAME ="zzw.db"; public static final int VERSION_CODE =1; public static final String TABLE_NAME ="employee";
DatabaseHelper.java
package com.example.databasedemo; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import androidx.annotation.Nullable; public class DatabaseHelper extends SQLiteOpenHelper { private static final String TAG="DatabaseHelper"; /** * * @ context 上下文 * @ name 数据库名称 * @ factory 游标工厂:可以移动的指针,默认为null * @ version 版本号 */ public DatabaseHelper(@Nullable Context context) { super(context, Constants.DATABASE_NAME, null, Constants.VERSION_CODE); } @Override public void onCreate(SQLiteDatabase db) { //第一次创建数据库时调用 //创建时的回调 Log.d(TAG,"创建数据库...."); //创建数据库表 String sql ="create table " Constants.TABLE_NAME "(_id integer,name varchar,age integer,salary integer)"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //升级数据库时的回调 Log.d(TAG,"升级数据库...."); // String sql; switch (oldVersion){ case 1: sql ="alter table " Constants.TABLE_NAME " add phone integer"; db.execSQL(sql); break; case 2: break; } } }
第一次运行,无数据库时的文件结构
创建数据库
//创建数据库 DatabaseHelper helper =new DatabaseHelper(this); helper.getWritableDatabase();
创建表
public void onCreate(SQLiteDatabase db) { //第一次创建数据库时调用 //创建时的回调 Log.d(TAG,"创建数据库...."); //创建数据库表 String sql ="create table " Constants.TABLE_NAME "(_id integer,name varchar,age integer,salary integer)"; db.execSQL(sql); }
创建数据库后的文件结构
将zzw.db文件导出,并用SQLiteExpert打开
与我们要创建的字段数一致
二、数据库的升级
对数据库进行升级
修改版本号为2
//Ctrl Shift U:大写 public static final String DATABASE_NAME ="zzw.db"; public static final int VERSION_CODE =2; public static final String TABLE_NAME ="employee";
升级数据库
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //升级数据库时的回调 Log.d(TAG,"升级数据库...."); // String sql; switch (oldVersion){ case 1: sql ="alter table " Constants.TABLE_NAME " add phone integer"; db.execSQL(sql); break; case 2: break; } }
成功添加了phone字段
三、Dao的增删改查(SQL语句实现)
进行测试
MainActivity.java
package com.example.databasedemo; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //创建数据库 DatabaseHelper helper =new DatabaseHelper(this); helper.getWritableDatabase(); Dao dao =new Dao(getApplicationContext()); // dao.insert(); // dao.update(); // dao.query(); dao.delete(); } }
Dao.java
package com.example.databasedemo; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.util.Log; /** * 这个类用于对数据库的增删改查 */ public class Dao { private final DatabaseHelper mHelper; private static final String TAG="Dao"; public Dao(Context context){ //创建数据库 //Ctrl Alt F,之后加回车,创建成员变量 mHelper = new DatabaseHelper(context); } public void insert(){ SQLiteDatabase db = mHelper.getWritableDatabase(); String sql="insert into " Constants.TABLE_NAME "(_id,name,age,salary,phone) values(?,?,?,?,?)"; Object []obj={1,"zzw",19,2,1234567890}; db.execSQL(sql,obj); db.close(); } public void delete(){ SQLiteDatabase db = mHelper.getWritableDatabase(); String sql="delete from " Constants.TABLE_NAME " where name = ?"; Object []obj={"zzw"}; db.execSQL(sql,obj); db.close(); } public void update(){ SQLiteDatabase db = mHelper.getWritableDatabase(); String sql="update " Constants.TABLE_NAME " set salary= ? where name = ?"; Object []obj={3,"zzw"}; db.execSQL(sql,obj); db.close(); } public void query(){ SQLiteDatabase db = mHelper.getWritableDatabase(); String sql="select * from " Constants.TABLE_NAME " where name = ?"; String []obj={"zzw"}; Cursor cursor = db.rawQuery(sql, obj); while (cursor.moveToNext()){ String name = cursor.getString(cursor.getColumnIndex("name")); String age = cursor.getString(cursor.getColumnIndex("age")); Log.d(TAG,"名字:" name "年龄:" age); } cursor.close(); db.close(); } }
插入数据
修改数据
查看数据
删除数据