android 使用SQLite儲存資料

來源:互聯網
上載者:User

標籤:重寫   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儲存資料

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.