android Sqlite基本操作

【android Sqlite基本操作】得意犹堪夸世俗,诏黄新湿字如鸦。这篇文章主要讲述android Sqlite基本操作相关的知识,希望能为你提供帮助。
新建一个类(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 staticUserDBHelper 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;
    }

    }


























































































































    推荐阅读