Well, it’s a recommendation, but it’s actually a database component that I wrote to introduce to you. This is the first edition, we hope you can use it at the same time to give comments, or put forward requirements.


To solve the pain points

The database components address the following categories of pain points:

  • SQL statements are required when creating a table. If the table structure is complex, SQL statements are prone to write errors. Deepsql makes table building easier in three ways. Passing in the Model class directly, or JSON or Map, is more suitable for the actual use scenario.
  • When inserting data, you can directly insert data using model or JSON or map without writing SQL statements
  • Update and delete have encapsulation, operation is more simple.

use

Just rely on

compile 'com. Deep: deepsqllib: 1.1'
Copy the code

Can. Here’s how to use it:

Initialize the

DeepSQL.getInstance().init(getApplication(),"demo.db".1);
Copy the code

The first parameter is Application the second parameter is the database name and the third parameter is the version number.

Build table

Use classes to build tables

We often convert the data in the database to a modal type, if you can use this class to build a table, is not very convenient. For example, we have a class:

public class Person implements Serializable{
    private String name;
    private int age;

    public String getName(a) {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge(a) {
        return age;
    }

    public void setAge(int age) {
        this.age = age; }}Copy the code

Create a table based on this class:

 DeepSQL.getInstance().create(Person.class);
Copy the code

The table name will use the class name

The map building table

Sometimes we need to create a table based on a map:

HashMap<String,Object> map = new HashMap<String, Object>();
map.put("name"."dog");
map.put("age".16);
DeepSQL.getInstance().create("animal",map);
Copy the code

The first parameter is the table name

File built table

If you do not need any of the preceding methods, you can also use the JSON method in Asset to create a table: Place a JSON file in the Assets folder

{
  "name1":"String"."name2":"int"."name3":"boolean"."name4":"float"."name5":"double"."name6":"long"
}
Copy the code

Then call:

  DeepSQL.getInstance().create(MainActivity.this."names.json");
Copy the code

The table name is named after the JSON file name

Json building table

If you are not comfortable with the JSON form of assets, you can also use JSON:

  DeepSQL.getInstance().create(MainActivity.this, json);
Copy the code

insert

Json insert

  DeepSQL.getInstance().insert("person",jsonObject);
Copy the code
  • The first parameter is the table name
  • The second parameter is the inserted JSON

Insert a map

 DeepSQL.getInstance().insert("animal",map);
Copy the code
  • The first parameter is the table name
  • The second parameter is the inserted map

Instantiate insert

 Person person  = new Person();
person.setName("john");
person.setAge(age);
DeepSQL.getInstance().insert(person);
Copy the code

Database query

Query all

ArrayList<Object> list =  DeepSQL.getInstance().selectObjects(Person.class,"person");
Copy the code
  • The first parameter is the class
  • The second parameter is the table name

Returns Json based on the condition

 JSONArray array =  DeepSQL.getInstance().selectJsonArryBySQL("select * from person where id = 5");
Copy the code

Return Object based on the condition

  ArrayList<Object> list =  DeepSQL.getInstance().selectObjectsBySQL(Person.class,"select * from person where id = 5");

Copy the code

update

Json update

   DeepSQL.getInstance().update("person"."id=?",new String[]{"5"},jsonObject);
Copy the code

The object update

    DeepSQL.getInstance().update("person"."id=?",new String[]{"5"},person);
Copy the code

delete

 DeepSQL.getInstance().del("person"."id=?",new String[]{"6"});
Copy the code

Delete table

  DeepSQL.getInstance().dropTable("person");
Copy the code

Execute SQL statements directly

The query

DeepSQL.getInstance().exec("sql");
Copy the code
  • Parameter is SQL statement

The query

Cursor c =DeepSQL.getInstance().queryBySQL("sql");
Copy the code
  • Parameter is SQL statement

Database upgrade or degradation processing

 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"); }};Copy the code

Note that this method needs to be called before init.

Special instructions

Write open source library for the first time, ability is limited, welcome everyone to put forward opinions. Also welcome to pay attention to my public number, after will recommend more easy to use component library.