android Sqlite基本操作

來源:互聯網
上載者:User

標籤:read   return   param   基本操作   sqlite   pac   content   log   turn   

  建立一個類(ex:UserDBHelper)繼承SQLiteOpenHelper

  • 建構函式裡面建資料庫
      public UserDBHelper(Context context, int version) {        super(context, DB_NAME,null,version);    }
  • onCreate方法裡面建表
       /**     * 只在資料庫產生的時候調用,一般我們會在     * 這個方法裡建表     * @param sqLiteDatabase     */    @Override    public void onCreate(SQLiteDatabase sqLiteDatabase) {        Log.d(TAG, "onCreate: ");        String drop_table = "DROP TABLE IF EXISTS " + TABLE_NAME + ";";        Log.d(TAG, "onCreate: drop_table_sql "+ drop_table);        sqLiteDatabase.execSQL(drop_table);        String create_sql = "CREATE TABLE IF NOT EXISTS "+ TABLE_NAME + " (" +                "_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +                "name VARCHAR NOT NULL," + "age INTEGER NOT NULL," +                "height LONG NOT NULL," + "weight FLOAT NOT NULL," +                "married INTEGER NOT NULL," + "update_time VARCHAR NOT NULL" +                 ",phone VARCHAR ,password VARCHAR"+                ");";        Log.d(TAG, "crate_table sql: "+create_sql);        sqLiteDatabase.execSQL(create_sql);    }
  • onUpgrade方法裡面是升級資料庫表
    @Override    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {        Log.d(TAG, "onUpgrade: oldVersion="+oldVersion+" newVersion:"+newVersion);        if (newVersion > 1){            String alertSql = "ALTER TABLE " + TABLE_NAME + " ADD COLUMN phone VARCHAR;";            Log.d(TAG, "onUpgrade: alertSQL="+alertSql);            sqLiteDatabase.execSQL(alertSql);            alertSql = "ALTER TABLE " + TABLE_NAME + " ADD COLUMN password VARCHAR;";            Log.d(TAG, "onUpgrade: alertSQL="+alertSql);            sqLiteDatabase.execSQL(alertSql);        }    }


    package com.safeluck.floatwindow.db;

    import android.content.ContentValues;
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.util.Log;

    import java.util.List;

    /**
    * aaa
    * Created by lzw on 2018/8/23. 15:28:33
    * 郵箱:[email protected]
    * All Rights Saved! Chongqing AnYun Tech co. LTD
    */
    public class UserDBHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "userDB";
    private static final int DB_VERSION = 1;
    private static UserDBHelper mHelper ;
    private static final String TABLE_NAME = "user";
    private static final String TAG = "UserDbHelper";
    private SQLiteDatabase DB = null;
    public UserDBHelper(Context context, int version) {
    super(context, DB_NAME,null,version);
    }

    public static UserDBHelper getInstance(Context context,int version){
    if (version>0 && mHelper == null){
    mHelper = new UserDBHelper(context,version);
    }else if (mHelper == null){
    mHelper = new UserDBHelper(context,DB_VERSION);
    }
    return mHelper;
    }


    /**
    * 擷取唯讀資料庫
    * @return
    */
    public SQLiteDatabase getReadLink() {
    DB = mHelper.getReadableDatabase();
    return DB;
    }

    public SQLiteDatabase getWriteDB() {
    DB = mHelper.getWritableDatabase();
    return DB;
    }

    /**
    * 只在資料庫產生的時候調用,一般我們會在
    * 這個方法裡建表
    * @param sqLiteDatabase
    */
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
    Log.d(TAG, "onCreate: ");
    String drop_table = "DROP TABLE IF EXISTS " + TABLE_NAME + ";";
    Log.d(TAG, "onCreate: drop_table_sql "+ drop_table);
    sqLiteDatabase.execSQL(drop_table);

    String create_sql = "CREATE TABLE IF NOT EXISTS "+ TABLE_NAME + " (" +
    "_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
    "name VARCHAR NOT NULL," + "age INTEGER NOT NULL," +
    "height LONG NOT NULL," + "weight FLOAT NOT NULL," +
    "married INTEGER NOT NULL," + "update_time VARCHAR NOT NULL" +
    ",phone VARCHAR ,password VARCHAR"+
    ");";
    Log.d(TAG, "crate_table sql: "+create_sql);
    sqLiteDatabase.execSQL(create_sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
    Log.d(TAG, "onUpgrade: oldVersion="+oldVersion+" newVersion:"+newVersion);
    if (newVersion > 1){
    String alertSql = "ALTER TABLE " + TABLE_NAME + " ADD COLUMN phone VARCHAR;";
    Log.d(TAG, "onUpgrade: alertSQL="+alertSql);
    sqLiteDatabase.execSQL(alertSql);
    alertSql = "ALTER TABLE " + TABLE_NAME + " ADD COLUMN password VARCHAR;";
    Log.d(TAG, "onUpgrade: alertSQL="+alertSql);
    sqLiteDatabase.execSQL(alertSql);

    }

    }

    /**
    * 刪除一行資料
    * @param condition
    */
    public void deleteRow(String condition){
    DB.delete(TABLE_NAME,condition,null);
    }


    public long addUser(List<UserInfo> userInfos){
    long reuslt = -1;
    for (int i = 0; i < userInfos.size(); i++) {
    UserInfo info = userInfos.get(i);
    ContentValues cv = new ContentValues();
    cv.put("name", info.name);
    cv.put("age", info.age);
    cv.put("height", info.height);
    cv.put("weight", info.weight);
    cv.put("married", info.married);
    cv.put("update_time", info.update_time);
    cv.put("phone", info.phone);
    cv.put("password", info.password);
    reuslt = DB.insert(TABLE_NAME,"",cv);
    }
    Log.d(TAG, "addUser: result="+reuslt);
    return reuslt;
    }

    }

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.