好吧,說是推薦,其實這是我寫的一個數據庫組件,介紹給你們。這是初版,但願你們在使用的同時可以給提出意見,或者提出需求。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以前調用。
第一次寫開源庫,能力有限,歡迎你們多多提出意見。 也歡迎關注個人公衆號,以後會推薦更多好用的組件庫。