日期:2014-05-16 浏览次数:20561 次
android系统内嵌了SQLite数据库,优点是处理速度快,占用内存少
在android应用中,围绕着数据库的操作主要是通过SQLiteDatabase来完成的,该类封装了以下方法:
方法 | 用途 |
execSQL(sql) | 执行sql脚本,通常用来创建或管理数据库表格 |
rawQuery(sql,selectionArgs) | 执行select查询操作(sql语句的方式) |
query(..) | 执行select查询操作(api的方式) |
insert(table,nullColumnHack,values) | 向指定table中新增记录 |
update(table,values,whereClause,whereArgs) | 更新指定table中的指定记录 |
delete(table,whereClause,whereArgs) | 删除指定table中的指定记录 |
SQLiteDatabase是通过SQLiteOpenHelper来获取的,该类是一个抽象类,开发基于数据库存储的android应用时,需要继承该类,构建出自己的SQLiteOpenHelper实例并覆盖其超类所声明的抽象方法:
onCreate(SQLiteDatabase db):用于创建数据库表格
onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion):用于更新数据库表结构
有了SQLiteOpenHelper之后,便可通过其getWritableDatabase()方法获取SQLiteDatabase对象,用以完成对数据库的CURD操作。
public class WordSQLiteHelper extends SQLiteOpenHelper{ public static final String TABLE_WORDS = "words"; public static final String COLUMN_ID = "_id"; public static final String COLUMN_ENGLISH = "english"; public static final String COLUMN_CHINESE = "chinese"; public static final String COLUMN_PHRASE = "phrase"; public static final String COLUMN_TRANSLATION = "translation"; public static final String COLUMN_LEVEL = "level"; private static final String DATABASE_NAME = "words.db"; private static final int DATABASE_VERSION = 1; public WordSQLiteHelper(Context context){ super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { StringBuilder sql=new StringBuilder("create table ").append(TABLE_WORDS).append("(") .append(COLUMN_ID).append(" integer primary key autoincrement,") .append(COLUMN_ENGLISH).append(" text not null,") .append(COLUMN_CHINESE).append(" text not null,") .append(COLUMN_PHRASE).append(" text not null,") .append(COLUMN_LEVEL).append(" real not null,") .append(COLUMN_TRANSLATION).append(" text not null);"); db.execSQL(sql.toString()); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w(WordSQLiteHelper.class.getName(),"Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data"); db.execSQL("DROP TABLE IF EXISTS "+TABLE_WORDS); onCreate(db); } }
在做web应用时,为了代码层次能够更加清晰,通常将其划分为3层结构(entity、dao和service)
dao的创建使得Service的编写变得更加容易,android应用可基于SQLiteDatabase对象构建出自己的Dao实例
public class WordDao implements IBaseDao<Word>{ private SQLiteDatabase database; private WordSQLiteHelper dbHelper; private String[] allColumns={WordSQLiteHelper.COLUMN_ID,WordSQLiteHelper.COLUMN_ENGLISH, WordSQLiteHelper.COLUMN_CHINESE,WordSQLiteHelper.COLUMN_PHRASE,WordSQLiteHelper.COLUMN_TRANSLATION,WordSQLiteHelper.COLUMN_LEVEL}; public WordDao(Context context){ dbHelper=new WordSQLiteHelper(context); } public void open(){ database=dbHelper.getWritableDatabase(); } public void close(){ dbHelper.close(); } @Override public void create(Word word){ ContentValues values = new ContentValues(); values.put(WordSQLiteHelper.COLUMN_ENGLISH,word.getEnglish()); values.put(WordSQLiteHelper.COLUMN_CHINESE,word.getChinese()); values.put(WordSQLiteHelper.COLUMN_PHRASE,word.getPhrase()); value