标签:read return param 基本操作 sqlite pac content log turn
新建一个类(ex:UserDBHelper)继承SQLiteOpenHelper
public UserDBHelper(Context context, int version) { super(context, DB_NAME,null,version); }
/** * 只在数据库生成的时候调用,一般我们会在 * 这个方法里建表 * @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); } }
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;
}
}
标签:read return param 基本操作 sqlite pac content log turn
原文地址:https://www.cnblogs.com/endian11/p/9540755.html