赞
踩
SQLiteOpenHelper是一个抽象类,并且是一个孤立的抽象类,通过创建一个子类继承SQLiteOpenHelper类,并实现其中的一些方法来对数据库进行操作。
public class DatabaseHelper extends SQLiteOpenHelper { private static final String TAG="DatabaseHelper"; private SQLiteDatabase sqLiteDatabase; /** * * @ context 上下文 * @ name 数据库名称 * @ factory 游标 * @ version 版本号 */ public DatabaseHelper(Context context) { super(context, Database.DATABASE_NAME, null, Database.VERSION_CODE); } @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { //OnCreate 方法;创建时回调 Log.d(TAG,"创建数据库"); //创建字段 这种方法是通过写好sql语句,然后交给execSQL()方法执行,来创建; //sql : create table table_name(id integer,GradeUnit integer,Unit integer,English varchar, Chinese varchar) //Database.TABLE_NAME = "vocabulary" 是在常量文件中写好的 String sql ="create table "+Database.TABLE_NAME+" (id integer,GradeUnit integer,Unit integer,English varchar,Chinese varchar)"; sqLiteDatabase.execSQL(sql); } /** * * @param sqLiteDatabase 资源器 * @param i :oldversion 旧的版本号 * @param i1:newversion 新的版本号 */ @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { //OnUpdate方法 更新数据库时,回调 Log.d(TAG,"更新数据库"); //添加字段 //alter table table_name add UNIT integer; String sql; //判断要采用的版本,进行修改 switch (i){ case 1: //版本一 则需要添加GradeUnit 和 Unit 字段 //注意sql语句不要写错,注意sql语法 sql = "alter table " + Database.TABLE_NAME + " add GradeUnit integer"; sqLiteDatabase.execSQL(sql); sql = "alter table " + Database.TABLE_NAME + " add Unit integer"; sqLiteDatabase.execSQL(sql); break; case 2: //版本2 添加Unit这个字段 sql = "alter table " + Database.TABLE_NAME + " add Unit varchar(10)"; sqLiteDatabase.execSQL(sql); break; case 3: break; } } }
创建一个Dao类,实现对数据库的增删改查
/** * @类名 : Dao * @作者 : SUNX * @描述 :数据库的增删改查 */ public class Dao { private static final String TAG = "Dao" ; private final DatabaseHelper mhelper; public Dao(Context context){ //创建数据库 mhelper = new DatabaseHelper(context); } public void insert(int id,int GradeUnit,int Unit,String En,String Ch){ SQLiteDatabase db = mhelper.getWritableDatabase(); //添加数据 //1.写好sql语句 insert into table_name (id,GradeUnit,Unit,English,Chinese) //2.将sql语句交给SQLiteDatabase.execSQL()执行 //3.将SQLiteDatabase资源关闭 //String sql = "insert into "+ Database.TABLE_NAME + " (id,GradeUnit,Unit,English,Chinese) values (?,?,?,?,?)"; //db.execSQL(sql,new Object[]{id,GradeUnit,Unit,En,Ch}); //API插入数据 ContentValues values = new ContentValues(); values.put("id",id); values.put("GradeUnit",GradeUnit); values.put("Unit",Unit); values.put("English",En); values.put("Chinese",Ch); db.insert(Database.TABLE_NAME,null,values); db.close(); } public void delete(int id,int GradeUnit,int Unit,String En,String Ch){ SQLiteDatabase db = mhelper.getWritableDatabase(); //删除数据 //1.写好sql语句 delete from table_name where conditions //2.将sql语句交给SQLiteDatabase.execSQL()执行 //3.将SQLiteDatabase资源关闭 String sql = "delete from " + Database.TABLE_NAME +" where English = '" +En + "'"; db.execSQL(sql); db.close(); } public void delete(int GradeUnit,int Unit,String English,String Chinese){ SQLiteDatabase db = mhelper.getWritableDatabase(); //删除数据 //1.写好sql语句 delete from table_name where conditions //2.将sql语句交给SQLiteDatabase.execSQL()执行 //3.将SQLiteDatabase资源关闭 String sql = "delete from " + Database.TABLE_NAME +" where GradeUnit =" + GradeUnit; db.execSQL(sql); db.close(); } public void update(int id,int GradeUnit,int Unit,String En,String Ch){ SQLiteDatabase db = mhelper.getWritableDatabase(); //更改数据 //1.写好sql语句 update table_name set *** where conditions //2.将sql语句交给SQLiteDatabase.execSQL()执行 //3.将SQLiteDatabase资源关闭 //String sql = "update " + Database.TABLE_NAME + " set Chinese = '"+ Ch+"' where English = '"+En+"'" ; //db.execSQL(sql); /** * SQLiteDatabase.update() * @param table : 表名 * @param values : 修改的值 类型为 ContentValues 通过put方法加入 * @param whereClause 修改的条件key "English = ?" * @param whereArgs 修改该条件的条件值value String[] args = "ear" * whereClause + whereArgs 等同于 where English = 'ear' */ ContentValues values = new ContentValues(); values.put("Chinese",Ch); String condition ="English = ?"; String[] args = {String.valueOf("ear")}; db.update(Database.TABLE_NAME,values,condition,args); db.close(); } public void query(int GradeUnit){ SQLiteDatabase db = mhelper.getReadableDatabase(); //查询数据 //1.写好sql语句 select * from table_name where conditions //2.将sql语句交给SQLiteDatabase.execSQL()执行 //3.将SQLiteDatabase资源关闭 /*String sql = "select * from " + Database.TABLE_NAME + " where GradeUnit ="+ GradeUnit; Cursor cursor = db.rawQuery(sql,null); while (cursor.moveToNext()){ int index = cursor.getColumnIndex("English"); String name = cursor.getString(index); Log.d( TAG,"English ==" + name); } cursor.close(); */ /** * @param String[] columns 要提取的关键字键值key * @param String selection 查询条件的键值key * @param String[] selectionArgs 查询条件的value值 * 相当于select String[] columns from table_name where String selection = "selectinoArgs" */ String[] columns = new String[]{"English"}; String selection = "GradeUnit = ?"; String[] selectionArgs = {String.valueOf(33)}; db.query(Database.TABLE_NAME,columns,selection,selectionArgs,null,null,null); db.close(); } }
初次接触Android studio中数据库方面的事务,总结一些方法和例子。希望能给你们提供一些想法和思路。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。