今天学了SQLite数据库的相关内容,花费了三个小时
1.创建sqliteOpenHelper工具类
package com.example.databasedemo; public class Constants { //数据库名称是常量 //大写ctrl+shift u public static final String DATABASE_NAME="testdb"; public static final int VERSION_CODE=3; public static final String TABLE_NAME="employee"; }
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"; //上下文,数据库名称,游标工厂,版本号 public DatabaseHelper(@Nullable Context context) { super(context, Constants.DATABASE_NAME, null, Constants.VERSION_CODE); } @Override public void onCreate(SQLiteDatabase db) { //创建时的回调 //第一次创建被调用 Log.d(TAG,"创建数据库..."); //创建字段 //create table table_name(_id integer,name varchar,age Integer,salary integer) //注意空格,生成正确的sql语句 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; //db.execSQL(sql); switch (oldVersion) { case 1: //添加两个字段 sql = "alter table " + Constants.TABLE_NAME + " add phone integer,address varchar "; db.execSQL(sql); break; case 2: //添加address sql = "alter table " + Constants.TABLE_NAME + " add address varchar"; db.execSQL(sql); break; case 3: sql = "alter table " + Constants.TABLE_NAME + " add phone integer"; db.execSQL(sql); break; } } }
2.创建dao类,对模拟数据进行操作(增删改查)
这里提供数据库的增删改查操作的方法,要用的时候直接调用就可以了,具体需要怎么查怎么插怎么删怎么改,自己加对应的参数就好了
package com.example.databasedemo; import android.content.ContentValues; 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 con) { mHelper = new DatabaseHelper(con); } public void insert() { SQLiteDatabase db=mHelper.getWritableDatabase(); /* String sql="insert into "+Constants.TABLE_NAME+" (_id,name,age,salary,phone,address) values (?,?,?,?,?,?)"; db.execSQL(sql,new Object[]{1,"Bill",60,100,110,"USA"});*/ //利用api ContentValues values=new ContentValues(); values.put("_id",2); values.put("name","piggo"); values.put("age",50); values.put("salary" ,1); values.put("phone",1290); values.put("address","USA"); db.insert(Constants.TABLE_NAME,null,values); db.close(); } public void delete() { SQLiteDatabase db=mHelper.getWritableDatabase(); /* String sql="delete from "+Constants.TABLE_NAME+" where age = 100"; db.execSQL(sql);*/ int result= db.delete(Constants.TABLE_NAME,null,null); Log.d(TAG,"-------"+result); db.close(); } public void update() { SQLiteDatabase db=mHelper.getWritableDatabase(); /* String sql="update "+Constants.TABLE_NAME+" set salary = 2 where age = 100"; db.execSQL(sql);*/ ContentValues values=new ContentValues(); //放入要修改的数 values.put("phone",122456); //修改所有的 db.update(Constants.TABLE_NAME,values,null,null); db.close(); } public void query() { SQLiteDatabase db=mHelper.getWritableDatabase(); /* String sql="select *from "+Constants.TABLE_NAME; //游标 Cursor cusor=db.rawQuery(sql,null); //遍历 while(cusor.moveToNext()) { int index=cusor.getColumnIndex("name"); String name=cusor.getString(index); Log.d(TAG,"name==="+name); } cusor.close();*/ // Cursor cusor=db.query(Constants.TABLE_NAME,null,null,null,null,null,null); while(cusor.moveToNext()) { int id=cusor.getInt(0); String name=cusor.getString(1); Log.d(TAG,"id==="+id+name); } cusor.close(); db.close(); } }