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

android Sqlite基本操作

时间:2018-08-27 11:18:43      阅读:177      评论:0      收藏:0      [点我收藏+]

标签: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
    * 邮箱:632393724@qq.com
    * 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基本操作

标签:read   return   param   基本操作   sqlite   pac   content   log   turn   

原文地址:https://www.cnblogs.com/endian11/p/9540755.html

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