最新出现了一个新的数据库 ,GreenDAO团队出了一个基于对象的数据库处理库ObjectBox,不再是sqlite数据库,速度快,使用方便,不用数据库版本更新。新项目应该拥抱ObjectBox,旧项目的greendao也可以转objectbox。可以去官网看使用文档,当你掉进坑里的时候,可以看一下我另一篇文章:Android ObjectBox 数据库避坑Duplicate files copied in APK lib/armeabi-v7a/libobjectbox.so_Android_周志豪378623234的博客-CSDN博客
package com.zhouzhihao.test.database;
import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Build;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
/**
* description:
*
* @author zhouzhihao
* @time 2020/12/4 0004
*/
public class DbHelper extends SQLiteOpenHelper {
public DbHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
public DbHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version, @Nullable DatabaseErrorHandler errorHandler) {
super(context, name, factory, version, errorHandler);
}
@RequiresApi(api = Build.VERSION_CODES.P)
public DbHelper(@Nullable Context context, @Nullable String name, int version, @NonNull SQLiteDatabase.OpenParams openParams) {
super(context, name, version, openParams);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql="create table user (id integer primary key autoincrement, name text(4),address text(5))";
db.execSQL(sql);
//版本4改动
String sql1="create table testid (_id integer primary key autoincrement, name text(4))";
db.execSQL(sql1);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
switch (newVersion){
case 2:
//版本2改动,没有break 所以版本4改动也会一层层执行
case 4:
//版本4改动
String sql1="create table testid (_id integer primary key autoincrement, name text(4))";
db.execSQL(sql1);
}
}
}
dBhelper=new DbHelper(this,"test",null,4);
SQLiteDatabase dataBase = dBhelper.getWritableDatabase();
try {
//创建存放数据的ContentValues对象
ContentValues values = new ContentValues();
values.put("name","zhou");
//数据库执行插入命令
dataBase.insert("user", null, values);
String name="lili",address="广东省广州市";
String sql="insert into user (name,address) values ('"+name+"','"+address+"')";
dataBase.execSQL(sql);
//删除
//方式二
dataBase.delete("user", "name = ? or address = ?", new String[]{"zhouzhihao"});
String sql1="delete from user where name='lili'";
dataBase.execSQL(sql1);
//修改
ContentValues contentValues=new ContentValues();
contentValues.put("address","河南焦作");
dataBase.update("user",contentValues, "name=?",new String[]{"zhou"});
//方式二
String sql2="update user set address='广东' where name='zhou'";
dataBase.execSQL(sql2);
//查看
Cursor cursor=dataBase.rawQuery("select * from user",null);
//方式二
cursor=dataBase.query("user",new String[]{"id","name","address"},"id=?",new String[]{"11"},null,null,null,null);
ArrayList dats = new ArrayList<>();
while (cursor.moveToNext()) {
User user = new User();
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name_temp = cursor.getString(cursor.getColumnIndex("name"));
String address_temp = cursor.getString(cursor.getColumnIndex("address"));
user.setId(id);
user.setName(name_temp);
user.setAddress(address_temp);
dats.add(user);
}
cursor.close();
@Override
public void onCreate(SQLiteDatabase db) {
String sql="create table user (id integer primary key autoincrement, name text(4),address text(5))";
db.execSQL(sql);
//版本4改动
String sql1="create table testid (_id integer primary key autoincrement, name text(4))";
db.execSQL(sql1);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
switch (newVersion){
case 2:
//版本2改动,没有break 所以版本4改动也会一层层执行
case 4:
//版本4改动
String sql1="create table testid (_id integer primary key autoincrement, name text(4))";
db.execSQL(sql1);
}
}