標籤:重寫 tab void 不同 方法 ext incr EDA div
建立一個類繼承SQLiteOpenHelper,重寫他的構造方法、onCreate()、onUpgrade()
構建出SQLiteOpenHelper執行個體後,再調用他的getReadableDatabase()貨getWritableDatabase()方法就能建立資料庫
getReadableDatabase()和getWritableDatabase()兩個方法都可建立或開啟一個現有的資料庫
不同的是,當資料庫不可寫入的時候,getReadableDatabase()方法返回的對象將以唯讀方式開啟資料庫,而getWritableDatabase()方法將出現異常
放在/data/data/<package name>/databases/目錄下
public class FeedReaderDbHelper extends SQLiteOpenHelper { // If you change the database schema, you must increment the database version. public static final int DATABASE_VERSION = 1; public static final String DATABASE_NAME = "FeedReader.db"; public FeedReaderDbHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } public void onCreate(SQLiteDatabase db) { db.execSQL(SQL_CREATE_ENTRIES); } public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // This database is only a cache for online data, so its upgrade policy is // to simply to discard the data and start over db.execSQL(SQL_DELETE_ENTRIES); onCreate(db); } public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { onUpgrade(db, oldVersion, newVersion); }}
用ContentValues對象用來更新資料、插入資料
Cursor對象用來查詢
android 使用SQLite儲存資料