組織contract類的一個好方法是在類的根層級定義一些全局變量,然後爲每一個table來創建內部類。
Note:通過實現 BaseColumns 的接口,內部類可以繼承到一個名爲_ID的主鍵,這個對於Android裏面的一些類似cursor adaptor類是很有必要的。這麼做不是必須的,但這樣能夠使得我們的DB與Android的framework能夠很好的相容。
例如,下面的例子定義了表名與該表的列名:
public final class FeedReaderContract { public FeedReaderContract(){} public static abstract class FeedEntry implements BaseColumns{ public static final String TABLE_NAME = "entry"; public static final String COLUMN_NAME_ENTRY_ID = "entryid"; public static final String COLUMN_NAME_TITLE = "title"; public static final String COLUMN_NAME_SUBTITLE = "subtitle"; } }
繼承SQLiteOpenHelp抽象類,藉助該類可以對數據庫進行創建和升級,SQLite中有兩個重要的方法,
getReadableDatabase()和getWriteableDatabase(),這兩個方法都可以打開或者創建數據庫(如果數據庫存在就打開,否則就創建新的數據庫)
public class MyDataBaseHelper extends SQLiteOpenHelper { private static final String TEXT_TYPE = "text"; private static final String SQL_CREATE_ENTRIES = "create table " + FeedReaderContract.FeedEntry.TABLE_NAME + "(" + FeedReaderContract.FeedEntry._ID + " integer primary key autoincrement," + FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID +" " +TEXT_TYPE + "," + FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE + " "+TEXT_TYPE + ")"; private static final String SQL_DELETE_ENTRIES="drop table is exists"+FeedReaderContract.FeedEntry.TABLE_NAME; public static final String DATABASE_NAME="FeedReader.db"; private static final int VERSION = 1; public MyDataBaseHelper(Context context) { super(context,DATABASE_NAME,null,VERSION); } @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { sqLiteDatabase.execSQL(SQL_CREATE_ENTRIES); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { sqLiteDatabase.execSQL(SQL_DELETE_ENTRIES); onCreate(sqLiteDatabase); } }
判斷數據庫是否創建
/* file = getExternalCacheDir();*///這句可以獲得數據庫,
file=getCacheDir();//
if (file != null && file.exists())
根據點擊的按鈕不同執行不同的操作
public void onClick(View view) { switch (view.getId()) { case R.id.create: //db爲SQLiteDatabase,藉助db可以對數據庫進行增刪改查操作 db = dataBaseHelper.getWritableDatabase(); /* file = getExternalCacheDir();*///這句可以獲得數據庫, file=getCacheDir(); if (file.exists()) { Toast.makeText(this, "create success", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "create failed", Toast.LENGTH_SHORT).show(); } break; case R.id.delete: if (file != null && file.exists()) { db.delete(FeedReaderContract.FeedEntry.TABLE_NAME, FeedReaderContract.FeedEntry._ID + "=?", new String[]{"1"}); Toast.makeText(this, "delete success", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "delete failed", Toast.LENGTH_SHORT).show(); } break; case R.id.query: if (file != null && file.exists()) { String[] columns = new String[]{ FeedReaderContract.FeedEntry._ID, FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID}; Cursor cursor = db.query(FeedReaderContract.FeedEntry.TABLE_NAME, columns, null, null, null, null, null); if (cursor.moveToFirst()) { do { int _id = cursor.getInt(cursor.getColumnIndex(FeedReaderContract.FeedEntry._ID)); String title = cursor.getString(cursor.getColumnIndex(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE)); String entryId = cursor.getString(cursor.getColumnIndex(FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID)); Log.d("test", "-id:" + _id + ",title:" + title + ",entryId:" + entryId); } while (cursor.moveToNext()); } } else { Toast.makeText(this, "query failed", Toast.LENGTH_SHORT).show(); } break; case R.id.insert: if (file != null && file.exists()) { ContentValues values = new ContentValues(); values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID,"li"); values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE,"花海"); db.insert(FeedReaderContract.FeedEntry.TABLE_NAME, null, values); values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID,"zhang"); values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, "超人不會飛"); db.insert(FeedReaderContract.FeedEntry.TABLE_NAME, null, values); values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID, "hua"); values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, "說好的幸福呢"); db.insert(FeedReaderContract.FeedEntry.TABLE_NAME, null, values); Toast.makeText(this,"insert success",Toast.LENGTH_SHORT).show(); }else { Toast.makeText(this,"insert failed",Toast.LENGTH_SHORT).show(); } break; } }