好吧,说是推荐,其实这是我写的一个数据库组件,介绍给你们。这是初版,但愿你们在使用的同时可以给提出意见,或者提出需求。java
这个数据库的组件解决的痛点主要有如下几类:sql
只须要依赖数据库
compile 'com.deep:deepsqllib:1.1'
复制代码
便可。 下面介绍一下使用方式:json
DeepSQL.getInstance().init(getApplication(),"demo.db",1);
复制代码
第一个参数是Application 第二个参数为数据库名字 第三个参数为版本号。bash
咱们常常会将数据库内的数据转成一个modal类型,若是可使用这个类来建表,岂不是很方便。 例如,咱们有一个类:ide
public class Person implements Serializable{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
复制代码
那么根据这个类建表可使用:this
DeepSQL.getInstance().create(Person.class);
复制代码
其中表名会使用类名spa
也有时咱们须要根据一个map建表:3d
HashMap<String,Object> map = new HashMap<String, Object>();
map.put("name","dog");
map.put("age",16);
DeepSQL.getInstance().create("animal",map);
复制代码
其中第一个参数为表名code
若是以上方式都不须要,也可使用asset中json建表的方式: 在assets文件夹中放一个json文件
{
"name1":"String",
"name2":"int",
"name3":"boolean",
"name4":"float",
"name5":"double",
"name6":"long"
}
复制代码
而后调用:
DeepSQL.getInstance().create(MainActivity.this, "names.json");
复制代码
表的名为会以json的文件名命名
若是不习惯使用assets中的这种json建表方式,也能够直接使用json:
DeepSQL.getInstance().create(MainActivity.this, json);
复制代码
DeepSQL.getInstance().insert("person",jsonObject);
复制代码
DeepSQL.getInstance().insert("animal",map);
复制代码
Person person = new Person();
person.setName("john");
person.setAge(age);
DeepSQL.getInstance().insert(person);
复制代码
ArrayList<Object> list = DeepSQL.getInstance().selectObjects(Person.class,"person");
复制代码
JSONArray array = DeepSQL.getInstance().selectJsonArryBySQL("select * from person where id = 5");
复制代码
ArrayList<Object> list = DeepSQL.getInstance().selectObjectsBySQL(Person.class,"select * from person where id = 5");
复制代码
DeepSQL.getInstance().update("person","id=?",new String[]{"5"},jsonObject);
复制代码
DeepSQL.getInstance().update("person","id=?",new String[]{"5"},person);
复制代码
DeepSQL.getInstance().del("person","id=?",new String[]{"6"});
复制代码
DeepSQL.getInstance().dropTable("person");
复制代码
DeepSQL.getInstance().exec("sql");
复制代码
Cursor c =DeepSQL.getInstance().queryBySQL("sql");
复制代码
DeepSQL.getInstance().sqlInterface = new SqlInterface() {
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
Logger.single(C.E,"onUpgrade myself");
}
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Logger.single(C.E,"onUpgrade myself");
}
};
复制代码
请注意该方法须要在init以前调用。
第一次写开源库,能力有限,欢迎你们多多提出意见。 也欢迎关注个人公众号,以后会推荐更多好用的组件库。