目录
创建表
创建表的语句create table 表名(字段名 类型,字段名 类型,…);
添加数据
ContentValue添加数据
sql语句添加数据
插入速度快
添加图片到数据库
删除数据
修改数据
查询数据
rawQuery查询
数据库 更新
如果是只是添加一个字段或几个,而不需要改变数据结构时, SQLite提供了ALTER TABLE命令,允许用户重命名或添加新的字段到已有表中,但是不能从表中删除字段。并且只能在表的末尾添加字段。
但是如果要改变数据结构,比如不要在末尾添加字段,而是在中间添加字段,比如要删除多个字段,添加多个字段。那就需要用到下面的方法。
创建表
创建表的语句 create table 表名(字段名 类型,字段名 类型,…);
public class MySqliteHelper extends SQLiteOpenHelper {
/*表名*/
private final String TABLE_NAME_PERSON = "person";
/*id字段*/
private final String VALUE_ID = "_id";
private final String VALUE_NAME = "name";
private final String VALUE_ISBOY = "isboy";
private final String VALUE_AGE = "age";
private final String VALUE_ADDRESS = "address";
/*头像字段*/
private final String VALUE_PIC = "pic";
/*创建表语句 语句对大小写不敏感 create table 表名(字段名 类型,字段名 类型,…)*/
private final String CREATE_PERSON = "create table " + TABLE_NAME_PERSON + "(" +
VALUE_ID + " integer primary key," +
VALUE_NAME + " text ," +
VALUE_ISBOY + " integer," +
VALUE_AGE + " ingeter," +
VALUE_ADDRESS + " text,"+
VALUE_PIC + " blob"+
")";
/**首次安装*/
public MySqliteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
//创建表
db.execSQL(CREATE_PERSON);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
添加数据
ContentValue添加数据
/**
* @param model 数据模型
* @return 返回添加数据有木有成功
*/
public boolean addPersonData(PersonModel model)
{
//把数据添加到ContentValues
ContentValues values = new ContentValues();
values.put(VALUE_NAME,model.getName());
values.put(VALUE_AGE,model.getAge());
values.put(VALUE_ISBOY,model.getIsBoy());
values.put(VALUE_ADDRESS,model.getAddress());
values.put(VALUE_PIC,model.getPic());//储存图片,这里是byte数据
//添加数据到数据库
long index = getWritableDatabase().insert(TABLE_NAME_PERSON,null,values);
//大于0表示添加成功
if(index > 0)
{
return true;
}else
{
return false;
}
}
sql语句添加数据
/**
* sql语句添加数据,比较麻烦
*/
public void addPersonDataSql(PersonModel model)
{
//格式: insert into 表名 (字段名,字段名,…)value('字段值','字段值','…')
//看着很多,其实就是这个 insert into person (name,age,isboy,address,pic) values('五天','3','0','上海市浦东新区x606','[B@5340395')
String insertSql = "insert into "+TABLE_NAME_PERSON+" ("+
VALUE_NAME + ","+
VALUE_AGE + ","+
VALUE_ISBOY + ","+
VALUE_ADDRESS + ","+
VALUE_PIC+")"+
" values"+"("+
"'"+model.getName()+"',"+
"'"+model.getAge()+"',"+
"'"+model.getIsBoy()+"',"+
"'"+model.getAddress()+"',"+
"'"+model.getPic()+"'"+
")";
Log.e(TAG, ""+insertSql);
getWritableDatabase().execSQL(insertSql);
}
插入速度快
/**
* 插入数据超快
*
* @return
*/
public boolean insertDXma(List<DaXiaoBean> list) {
if (list.size() == 0) {
return false;
}
long start = SystemClock.currentThreadTimeMillis();
SQLiteDatabase db = null;
try { //这里可以优化采用java7新特性 try-catch-resource。
db = dbHelper.getWritableDatabase();
String sql = "insert into " + TABLE_MA + "("
+ "dama,"// 包名
+ "xiaoma,"// 账号
+ "startTime,"
+ "endTime,"
+ "isUp,"
+ "isFull,"
+ "beizhu"
+ ") " + "values(?,?,?,?,?,?,?)";
SQLiteStatement stat = db.compileStatement(sql); //预编译Sql语句避免重复解析Sql语句
db.beginTransaction(); //开启事务
for (int i = 0; i < list.size(); i++) { // dbAdapter.addToDaxiMa(new DaXiaoBean(i+"211","1095292"+i,"2019-10-14 12:00:00"));
stat.bindString(1, list.get(i).getDama());
stat.bindString(2, list.get(i).getXiaoma());
stat.bindString(3, list.get(i).getStartTime());
stat.bindString(4, list.get(i).getEndTime());
stat.bindString(5, list.get(i).getIsUp());
stat.bindString(6, list.get(i).getIsFull());
stat.bindString(7, list.get(i).getBeizhu());
long result = stat.executeInsert();
log.e("添加--" + result);
if (result < 0) {
log.e("insert traceaction use time " + (SystemClock.currentThreadTimeMillis() - start));
return false;
}
}
db.setTransactionSuccessful(); //控制回滚,如果不设置此项自动回滚
} catch (Exception e) {
log.e("插入数据错误" + e.getMessage());
e.printStackTrace();
return false;
} finally {
try {
if (null != db) {
db.endTransaction(); //事务提交
db.close();
}
} catch (Exception e) {
e.printStackTrace();
log.e("finally错误" + e.getMessage());
}
}
return true;
}
添加图片到数据库
/**
* @param resourceID 图片资源id
* @return 将图片转化成byte
*/
private byte[] picTobyte(int resourceID)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = context.getResources().openRawResource(resourceID);
Bitmap bitmap = BitmapFactory.decodeStream(is);
//压缩图片,100代表不压缩(0~100)
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
//取出数据
//查询全部数据
Cursor cursor = getWritableDatabase().query(TABLE_NAME_PERSON, null, null, null, null, null, null, null);
byte pic[] = cursor.getBlob(cursor.getColumnIndex(VALUE_PIC));
Bitmap b = BitmapFactory.decodeByteArray(pic, 0, pic.length);
holder.pic.setImageBitmap(b);
删除数据
/**
* 方法删除数据库数据
*/
public void deletePersonData(PersonModel model)
{
//where后跟条件表达式 =,!=,>,<,>=,<=
//多条件 and or
//删除数据库里的model数据 因为_id具有唯一性。
getWritableDatabase().delete(TABLE_NAME_PERSON,VALUE_ID+"=?",new String[]{""+model.getId()});
//删除数据库里 _id = 1 的数据
getWritableDatabase().delete(TABLE_NAME_PERSON,VALUE_ID+"=?",new String[]{"1"});
//删除 age >= 18 的数据
getWritableDatabase().delete(TABLE_NAME_PERSON,VALUE_AGE+">=?",new String[]{"18"});
//删除 id > 5 && age <= 18 的数据
getWritableDatabase().delete(TABLE_NAME_PERSON,VALUE_ID+">?"+" and "+VALUE_AGE +"<=?",new String[]{"5","18"});
//删除 id > 5 || age <= 18 的数据
getWritableDatabase().delete(TABLE_NAME_PERSON,VALUE_ID+">?"+" or "+VALUE_AGE +"<=?",new String[]{"5","18"});
//删除数据库里 _id != 1 的数据
getWritableDatabase().delete(TABLE_NAME_PERSON,VALUE_ID+"!=?",new String[]{"1"});
//删除所有 _id >= 7 的男生
getWritableDatabase().delete(TABLE_NAME_PERSON,VALUE_ISBOY+"=?"+" and "+VALUE_ID+">=?",new String[]{"1","7"});
//删除所有 _id >= 7 和 _id = 3 的数据
getWritableDatabase().delete(TABLE_NAME_PERSON,VALUE_ID+">=?"+" or "+VALUE_ID+"=?",new String[]{"7","3"});
}
/**
* sql删除数据库数据
*/
public void deletePersonDataSql(PersonModel model)
{
//条件表达式 =,!=,>,<,>=,<=
//语法格式 delete from 表名 where 字段 条件表达式 '值'
//语法示例 delete from person where _id='2'
//多条件 delete from person where _id>'10' and age>'100'
//多条件 delete from person where _id>'10' or _id<'5'
//删除数据库里的model数据 因为_id具有唯一性。
String sql1 = "delete from "+TABLE_NAME_PERSON+" where "+
VALUE_ID+"="+"'"+model.getId()+"'";
//删除数据库里 _id = 1 的数据
String sql2 = "delete from "+TABLE_NAME_PERSON+" where "+
VALUE_ID+"="+"'"+1+"'";
//删除 age >= 18 的数据
String sql3 = "delete from "+TABLE_NAME_PERSON+" where "+
VALUE_AGE+">="+"'"+18+"'";
//删除 id > 5 && age <= 18 的数据
String sql4 = "delete from "+TABLE_NAME_PERSON+" where "+
VALUE_ID+">"+"'"+5+"'"+" and "+
VALUE_AGE+"<="+"'"+18+"'";
删除 id > 5 || age <= 18 的数据
String sql5 = "delete from "+TABLE_NAME_PERSON+" where "+
VALUE_ID+">"+"'"+5+"'"+" or "+
VALUE_AGE+"<="+"'"+18+"'";
//删除数据库里 _id != 1 的数据
String sql6 = "delete from "+TABLE_NAME_PERSON+" where "+
VALUE_ID+"!="+"'"+1+"'";
//删除所有 _id >= 7 的男生
String sql7 = "delete from "+TABLE_NAME_PERSON+" where "+
VALUE_ID+">="+"'"+7+"'"+" and "+
VALUE_ISBOY +"="+"'"+1+"'";
//删除所有 _id >= 7 和 _id = 3 的数据
String sql8 = "delete from "+TABLE_NAME_PERSON+" where "+
VALUE_ID+">="+"'"+7+"'"+" and "+
VALUE_ID+"="+"'"+3+"'";
Log.e(TAG, ""+sql7 );
getWritableDatabase().execSQL(sql7);
}
修改数据
/**
* 方法修改数据库数据
*/
public void updatePersonData(PersonModel model)
{
//条件表达式 =,!=,>,<,>=,<=
//多条件 and or and和or都可以无限连接
//多条件示例 _id>=? and _id<=?
//多条件示例 _id>=? or _id=? or _id=?
//将数据添加至ContentValues
ContentValues values = new ContentValues();
values.put(VALUE_NAME,model.getName());
values.put(VALUE_ADDRESS,model.getAddress());
values.put(VALUE_ISBOY,model.getIsBoy());
values.put(VALUE_AGE,model.getAge());
values.put(VALUE_PIC,model.getPic());
//修改model的数据
getWritableDatabase().update(TABLE_NAME_PERSON,values,VALUE_ID+"=?",new String[]{""+model.getId()});
//将 _id>20 的数据全部修改成model 适合重置数据
getWritableDatabase().update(TABLE_NAME_PERSON,values,VALUE_ID+">?",new String[]{"20"});
//将 _id>=30 && _id<=40 的数据全部修改成model 适合重置数据
getWritableDatabase().update(TABLE_NAME_PERSON,values,VALUE_ID+">=? and "+VALUE_ID+"<=?",new String[]{"30","40"});
//将 _id>=40 || _id=30 || _id=20的 age 修改成18 (需先将model的数据修成成18) 这里and 和 or 的效果时一样的 因为_id是唯一的
int count = getWritableDatabase().update(TABLE_NAME_PERSON,values,VALUE_ID+">=?"+" or "+VALUE_ID+"=?"+" or "+VALUE_ID+"=?",new String[]{"40","30","20"});
// count 返回被修改的条数 >0 表示修改成功
Log.e(TAG, ""+ VALUE_ID+">=? and "+VALUE_ID+"<=?");
Log.e(TAG, ""+ VALUE_ID+">=?"+" or "+VALUE_ID+"=?"+" or "+VALUE_ID+"=?");
}
/**
* sql修改数据库数据
*/
public void updatePersonDataSql(PersonModel model)
{
//条件表达式 =,!=,>,<,>=,<=
//多条件 and or and和or都可以无限连接
//修改格式 update 表名 set 字段='字段值', 字段='字段值',… where 字段='字段值'
//多条件示例 update person set name='钢铁侠',isboy='1' where _id='2'
//多条件示例 update person set name='天地',isboy='1',age='79',address='山东省青岛市开平路53号国棉四厂二宿舍1号楼2单元204户甲',pic='[B@266d768b' where _id>='30' and _id<='40'
//多条件示例 update person set name='小二',isboy='1',age='18',address='河南南阳市八一路272号特钢公司',pic='[B@17560c26' where _id>='40' or _id='30' or _id='20'
//修改model的数据
String update1 = "update "+TABLE_NAME_PERSON+" set "+
VALUE_NAME+"="+"'"+model.getName()+"',"+
VALUE_ISBOY+"="+"'"+model.getIsBoy()+"',"+
VALUE_AGE+"="+"'"+model.getAge()+"',"+
VALUE_ADDRESS+"="+"'"+model.getAddress()+"',"+
VALUE_PIC+"="+"'"+model.getPic()+"'" + " where "+
VALUE_ID +"="+"'"+model.getId()+"'";
//将 _id>20 的数据全部修改成model 适合重置数据
String update2 = "update "+TABLE_NAME_PERSON+" set "+
VALUE_NAME+"="+"'"+model.getName()+"',"+
VALUE_ISBOY+"="+"'"+model.getIsBoy()+"',"+
VALUE_AGE+"="+"'"+model.getAge()+"',"+
VALUE_ADDRESS+"="+"'"+model.getAddress()+"',"+
VALUE_PIC+"="+"'"+model.getPic()+"'" + " where "+
VALUE_ID + ">="+"'"+"20"+"'";
//将 _id>=30 && _id<=40 的数据全部修改成model 适合重置数据
String update3 = "update "+TABLE_NAME_PERSON+" set "+
VALUE_NAME+"="+"'"+model.getName()+"',"+
VALUE_ISBOY+"="+"'"+model.getIsBoy()+"',"+
VALUE_AGE+"="+"'"+model.getAge()+"',"+
VALUE_ADDRESS+"="+"'"+model.getAddress()+"',"+
VALUE_PIC+"="+"'"+model.getPic()+"'" + " where "+
VALUE_ID + ">="+"'"+"30"+"'"+" and "+
VALUE_ID + "<="+"'"+"40"+"'";
//将 _id>=40 || _id=30 || _id=20的 age 修改成18 (需先将model的数据修成成18)
String update4 = "update "+TABLE_NAME_PERSON+" set "+
VALUE_NAME+"="+"'"+model.getName()+"',"+
VALUE_ISBOY+"="+"'"+model.getIsBoy()+"',"+
VALUE_AGE+"="+"'"+"18"+"',"+
VALUE_ADDRESS+"="+"'"+model.getAddress()+"',"+
VALUE_PIC+"="+"'"+model.getPic()+"'" + " where "+
VALUE_ID + ">="+"'"+"40"+"'"+" or "+
VALUE_ID + "="+"'"+"30"+"'"+" or "+
VALUE_ID + "="+"'"+"20"+"'";
//其实前面set语句都一样,后面的where 不一样
getWritableDatabase().execSQL(update4);
}
查询数据
table 需要查询表名columns 需要查询的字段,全部查询就写null,部分就写new String[]{"id","name","sex"}selection 查询条件,where后的条件语句,不带内容 例如:id>? and name=?selectionArgs 查询条件对于的内容 例如:{"10","xiaoming"}groupBy 分组having 分组的过滤条件orderBy 排序,例如 "id asc"(desc-降序 asc-升序)limit 查询显示的条数(分页,可以不写,默认全部显示, 例如"100"(前100条数据), 也可以只取 前4条,例如"0,4"(从第1条数据开始取,取4条数据,下标从0开始)。 例如"3,4"(从第4条数据开始取,取4条数据))
/**
* 查询全部数据
*/
public List<PersonModel> queryAllPersonData(){
//查询全部数据
Cursor cursor = getWritableDatabase().query(TABLE_NAME_PERSON,null,null,null,null,null,null,null);
List<PersonModel> list = new ArrayList<>();
if(cursor.getCount() > 0)
{
//移动到首位
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
int id = cursor.getInt(cursor.getColumnIndex(VALUE_ID));
String name = cursor.getString(cursor.getColumnIndex(VALUE_NAME));
int isBoy = cursor.getInt(cursor.getColumnIndex(VALUE_ISBOY));
int age = cursor.getInt(cursor.getColumnIndex(VALUE_AGE));
String address = cursor.getString(cursor.getColumnIndex(VALUE_ADDRESS));
byte pic[] = cursor.getBlob(cursor.getColumnIndex(VALUE_PIC));
PersonModel model = new PersonModel();
model.setId(id);
model.setName(name);
model.setIsBoy(isBoy);
model.setAge(age);
model.setAddress(address);
model.setPic(pic);
list.add(model);
//移动到下一位
cursor.moveToNext();
}
}
cursor.close();
getWritableDatabase().close();
return list;
}
/**
* 一些查询用法
*/
public void queryPersonData()
{
//查询全部数据
getWritableDatabase().query(TABLE_NAME_PERSON,null,null,null,null,null,null);
//查询 _id = 1 的数据
getWritableDatabase().query(TABLE_NAME_PERSON,null,VALUE_ID+"=?",new String[]{"1"},null,null,null);
//查询 name = 张三 并且 age > 23 的数据
getWritableDatabase().query(TABLE_NAME_PERSON,null,VALUE_NAME+"=?"+" and "+VALUE_AGE+">?",new String[]{"张三","23"},null,null,null);
//查询 name = 张三 并且 age > 23 的数据 并按照id 降序排列
getWritableDatabase().query(TABLE_NAME_PERSON,null,VALUE_NAME+"=?"+" and "+VALUE_AGE+">?",new String[]{"张三","23"},null,null,VALUE_ID+" desc");
//查询数据按_id降序排列 并且只取前4条。
getWritableDatabase().query(TABLE_NAME_PERSON,null,null,null,null,null,VALUE_ID+" desc","0,4");
}
rawQuery查询
rawQuery()是自己要写sql语句查询,query()也是在源码里合成sql语句的。所以自己写sql语句更加高效。public Cursor rawQuery(String sql, String[] selectionArgs) rawQuery方法至少有两个参数。使用:
SQLiteDatabase db= ....;
Cursor cursor = db.rawQuery("select * from person",null);
...
cursor.close();
db.close();
//占位符
Cursor cursor = db.rawQuery("select * from person where name like ? and age=?", new String[]{"张三", "4"});
//例子
/**
* rawQuery()方法查询
*
* 一些查询用法
*
* 容易出错,万千注意。
*
* 注意空格、单引号、单词不要写错了。
*
*/
public Cursor rawQueryPersonData() {
Cursor cursor = null;
String rawQuerySql = null;
//查询全部数据 select * from person
rawQuerySql = "select * from "+TABLE_NAME_PERSON;
//查询_id = 1 的数据 select * from person where _id = 1
rawQuerySql = "select * from "+TABLE_NAME_PERSON+" where "+VALUE_ID +" = 1";
//查询 name = 张三 并且 age > 23 的数据 通配符? select * from person where name = ? and age > ?
rawQuerySql = "select * from "+TABLE_NAME_PERSON+" where "+VALUE_NAME +" = ?"+" and "+ VALUE_AGE +" > ?";
// cursor = getWritableDatabase().rawQuery(rawQuerySql,new String[]{"张三","23"});
//查询 name = 张三 并且 age >= 23 的数据 select * from person where name = '张三' and age >= '23'
rawQuerySql = "select * from "+TABLE_NAME_PERSON+" where "+VALUE_NAME +" = '张三'"+" and "+ VALUE_AGE +" >= '23'";
//查询 name = 张三 并且 age >= 23 的数据 并按照id 降序排列 select * from person where name = '张三' and age >= '23' order by _id desc
rawQuerySql = "select * from "+TABLE_NAME_PERSON+" where "+VALUE_NAME +" = '张三'"+" and "+ VALUE_AGE +" >= '23'"+" order by "+VALUE_ID +" desc";
//查询数据按_id降序排列 并且只取前4条。(测试下标是从0开始) select * from person order by _id desc limit 0, 4
rawQuerySql = "select * from "+TABLE_NAME_PERSON+" order by "+VALUE_ID +" desc"+" limit 0, 4";
//查询年龄在20岁以上或者是女生 的数据 select age,isboy from person where age > 20 or isboy != 1
rawQuerySql = "select "+VALUE_AGE+","+VALUE_ISBOY +" from " +TABLE_NAME_PERSON+" where "+VALUE_AGE+" > 20"+" or "+VALUE_ISBOY +" != 1";
//查询年龄小于等于20 或者 大于等于 80的数据 并且按年龄升序排列 select * from person where age <= 20 or age >=80 order by age asc
rawQuerySql = "select * from "+TABLE_NAME_PERSON+" where "+VALUE_AGE+" <= 20"+" or "+VALUE_AGE+" >=80"+" order by "+VALUE_AGE+" asc";
cursor = getWritableDatabase().rawQuery(rawQuerySql,null);
Log.e(TAG, rawQuerySql );
return cursor;
}
数据库 更新
如果是只是添加一个字段或几个,而不需要改变数据结构时, SQLite提供了ALTER TABLE命令,允许用户重命名或添加新的字段到已有表中,但是不能从表中删除字段。并且只能在表的末尾添加字段。
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.e(TAG, "-------> onUpgrade"+" oldVersion = "+oldVersion+" newVersion = "+newVersion);
if(oldVersion != newVersion)
{
switch (newVersion)
{
case 2:
//升级数据库,不改变表结构 注意空格
//添加列 addcol_goods2 , text 为字符串数据类型 ,person为表名
//alter table person add column addcol_goods2 text
//添加的列 添加优点 添加缺点
String addColGoods = "addcol_goods"+newVersion;
String addColBads = "addcol_bads"+newVersion;
//添加列的sql语句
String upgradeGoods = "alter table "+TABLE_NAME_PERSON + " add column "+ addColGoods+" text";
String upgradeBads = "alter table "+TABLE_NAME_PERSON+" add column "+addColBads+" text";
//执行sql语句 一次只能添加一个字段
db.execSQL(upgradeGoods);
db.execSQL(upgradeBads);
break;
}
}
}
但是如果要改变数据结构,比如不要在末尾添加字段,而是在中间添加字段,比如要删除多个字段,添加多个字段。那就需要用到下面的方法。
将要修改的表(person)重命名(为 temp_person)。重新创建(person)表。将(temp_person)表的数据导入到(person)表。将(temp_person)表删除。 public class MySqliteHelper extends SQLiteOpenHelper {
private String TAG = "MySqliteHelper";
/*表名*/
private final String TABLE_NAME_PERSON = "person";
/*临时表面*/
private final String TABLE_NAME_TEMP_PERSON = "temp_person";
/*id字段*/
private final String VALUE_ID = "_id";
private final String VALUE_NAME = "name";
private final String VALUE_ISBOY = "isboy";
private final String VALUE_AGE = "age";
private final String VALUE_ADDRESS = "address";
/*头像字段*/
private final String VALUE_PIC = "pic";
/*优点*/
private final String VALUE_GOODS = "goods";
/*创建表语句 语句对大小写不敏感 create table 表名(字段名 类型,字段名 类型,…)*/
private final String CREATE_PERSON = "create table " + TABLE_NAME_PERSON + "(" +
VALUE_ID + " integer primary key," +
VALUE_NAME + " text ," +
VALUE_ISBOY + " integer," +
VALUE_AGE + " ingeter," +
VALUE_ADDRESS + " text," +
VALUE_PIC + " blob" +
")";
//=====================更新语句==========================
//建立新的person表(加入了新字段 VALUE_GOODS )
private final String CREATE_NEW_TABLE = "create table " + TABLE_NAME_PERSON + "(" +
VALUE_ID + " integer primary key," +
VALUE_GOODS + " text ,"+
VALUE_NAME + " text ," +
VALUE_ISBOY + " integer," +
VALUE_AGE + " ingeter," +
VALUE_ADDRESS + " text," +
VALUE_PIC + " blob " +
")";
//重命名person表为temp_person表
private final String CREATE_TEMP_TABLE = "alter table "+TABLE_NAME_PERSON+" rename to "+TABLE_NAME_TEMP_PERSON;
//将temp_person表的数据导入新person表(优点是添加的默认数据)
//重点。格式不要错。删减字段都是这个样子。
private final String INSERT_DATA = "insert into "+TABLE_NAME_PERSON +" select "+ VALUE_ID +" , "+
" \" 优点\" "+" , "+
VALUE_NAME+" , "+
VALUE_ISBOY+" , "+
VALUE_AGE+" , "+
VALUE_ADDRESS+" , "+
VALUE_PIC+" "+
" from "+TABLE_NAME_TEMP_PERSON;
//删除temp_person表
private final String DROP_TEMP_TABLE = "drop table "+TABLE_NAME_TEMP_PERSON;
public MySqliteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
Log.e(TAG, "-------> MySqliteHelper");
}
@Override
public void onCreate(SQLiteDatabase db) {
//创建表
db.execSQL(CREATE_PERSON);
Log.e(TAG, "-------> onCreate");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.e(TAG, "-------> onUpgrade"+" oldVersion = "+oldVersion+" newVersion = "+newVersion);
if(oldVersion != newVersion)
{
switch (newVersion)
{
case 2:
//四大步骤。
db.execSQL(CREATE_TEMP_TABLE);
db.execSQL(CREATE_NEW_TABLE);
db.execSQL(INSERT_DATA);
db.execSQL(DROP_TEMP_TABLE);
break;
}
}
}
}