标签:
Android开发中,数据存储主要有五种:网络、数据库、SharePreferences、文件以及Content Provider.
. 数据库
Android中的数据库最常用的是Sqlite. 使用Sqlite进行数据存储,可分为以下几步:
. 继承SqliteOpenHelper
. 整理4个构造方法
. 重写onCreate与onUpgrade
public class DownDBHelper extends SQLiteOpenHelper { public static final String DATABASE_NAME = "download.db"; public static final int DATABASE_VERSION = 1; public static final String TABLE_NAME = "down_info"; // /////////////////// TABLE COLUMN public static final String COLUMN_ID = "_id"; public static final String COLUMN_THREAD_ID = "thread_id"; public static final String COLUMN_URL = "url"; public static final String COLUMN_START = "start"; public static final String COLUMN_END = "end"; public static final String COLUMN_PROGRESS = "progress"; // ///////////////////// CONTRUCTOR public DownDBHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } public DownDBHelper(Context context, String name, int version) { this(context, name, null, version); } public DownDBHelper(Context context, String name) { this(context, name, DATABASE_VERSION); } public DownDBHelper(Context context) { this(context, DATABASE_NAME); } // ////////////// OVERRIDE @Override public void onCreate(SQLiteDatabase db) { String creSQL = "CREATE TABLE IF NOT EXISTS" + TABLE_NAME + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_THREAD_ID + "INTEGER," + COLUMN_URL + " CHAR," + COLUMN_START + " INTEGER," + COLUMN_END + "INTEGER," + COLUMN_PROGRESS + "INTEGER)"; db.execSQL(creSQL); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { String dropSQL = "DROP TABLE IF EXISTS " + TABLE_NAME; db.execSQL(dropSQL); this.onCreate(db); } }
标签:
原文地址:http://www.cnblogs.com/kipMeister/p/4300872.html