android Sqlite基本操作

Posted endian11

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 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基本操作的主要内容,如果未能解决你的问题,请参考以下文章

java.lang.NullPointerException: Attempt to invoke virtual method ‘int android.database.sqlite异常(代码片段

如何在片段中访问 SQLite 数据到 ListView

当sqlite android片段中的数据更改或删除时如何刷新recyclerview?

如何在片段内的 recylerview 列表中显示 SQLite 数据库数据?

Android SQLite 更新行不起作用并且没有显示错误

如何从android studio中的recycler视图将数据添加到SQLite数据库中