android 使用SQLite存储数据

创建一个类继承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对象用来查询

posted @ 2018-08-01 23:45  chenzquan  阅读(215)  评论(0编辑  收藏  举报