码迷,mamicode.com
首页 > 移动开发 > 详细

Android开发系列之数据存储(一)

时间:2015-02-26 13:08:24      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

  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);
    }

}
View Code

 

   

    

    

    

    

Android开发系列之数据存储(一)

标签:

原文地址:http://www.cnblogs.com/kipMeister/p/4300872.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!