The creation of Android SQLite database and two ways to add, delete, modify and check

1. Introduction to SQLite

It also uses SQL statements to operate the database; it is also a kind of relational database; features: fast calculation speed, less resources;

Second, create SQLiteOpenHelper

Android uses SQLite database, it must inherit SQLiteOpenHelper, and must implement three methods, these three methods are indispensable, is a fixed form; one is the constructor (usually a four-parameter structure), onCreate, onUpgrade;

public class DBHelper extends SQLiteOpenHelper {
    public DBHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //创建表的SQL语句
        String table = "create table user(id int ,name varchar(20))";
        db.execSQL(table);
        Log.d("","数据库创建了");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.d("","数据库升级 了");
    }
}

The four parameters of the constructor:

  • Context: Context, I believe you all know this
  • name: is the name of the database to be created;
  • CursorFactory: The cursor factory, which is equivalent to the result set; null is usually passed in when passing parameters, and the system uses the default Cursor by default;
  • version: version number, when the version changes, the onUpgrade method will be called to upgrade; note that the version number cannot be rolled back;

Three, create a database

Create database objects;

public class TestDB extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //创建数据库  名字可以不带.db,为了增加可读性,最好加上.db
        DBHelper helper = new DBHelper(this, "login.db", null, 1);

        //如果数据库不存在,则创建数据库,然后获取可读可写的数据库对象,如果存在,直接打开;
        SQLiteDatabase writableDatabase = helper.getWritableDatabase();
        //如果存储空间满了,获取一个只读数据库对象;
        SQLiteDatabase readableDatabase = helper.getReadableDatabase();
    }
}

Fourth, create a table

Generally, a table is created when the database is created, and the onCreate method will not be called once the database is created successfully; therefore, the table is generally created in the onCreate method;

Five, CRUD

Direct SQL statements to perform additions, deletions, and changes; SQL statements are not clear, you can view this article, which introduces the use of CRUD in detail https://blog.csdn.net/ezconn/article/details/103840699

Increase (insert data)

public void insert() {
    DBHelper helper = new DBHelper(this, "user.db", null, 1);
    //如果数据不存在,则创建数据,然后获取可读可写的数据库对象,如果存在,直接打开;
    SQLiteDatabase writableDatabase = helper.getWritableDatabase();

    writableDatabase.execSQL("insert into user(id,name,phone) values(?,?,?)", new Object[]{1, "藏三","13888"});
    writableDatabase.execSQL("insert into user(id,name,phone) values(?,?,?)", new Object[]{2, "李四","15888"});
    writableDatabase.execSQL("insert into user(id,name,phone) values(?,?,?)", new Object[]{3, "王五","16888"});
    writableDatabase.execSQL("insert into user(id,name,phone) values(?,?,?)", new Object[]{4, "赵六","17888"});
    writableDatabase.close();
}

delete data 

private void delete() {
    DBHelper helper = new DBHelper(this, "user.db", null, 1);
    //如果数据不存在,则创建数据,然后获取可读可写的数据库对象,如果存在,直接打开;
    SQLiteDatabase writableDatabase = helper.getWritableDatabase();

    writableDatabase.execSQL("delete from user where name = ?", new Object[]{"藏三"});
    writableDatabase.close();
}

 change the data

private void update() {
    DBHelper helper = new DBHelper(this, "user.db", null, 1);
    //如果数据不存在,则创建数据,然后获取可读可写的数据库对象,如果存在,直接打开;
    SQLiteDatabase writableDatabase = helper.getWritableDatabase();

    writableDatabase.execSQL("update user set id = ? where name = ?", new Object[]{6,"王五"});
    writableDatabase.close();
}

 Query data

private void select() {
    DBHelper helper = new DBHelper(this, "user.db", null, 1);
    //如果数据不存在,则创建数据,然后获取可读可写的数据库对象,如果存在,直接打开;
    SQLiteDatabase writableDatabase = helper.getWritableDatabase();
    Cursor cursor = writableDatabase.rawQuery("select name from user",null);
    while (cursor.moveToNext()) {
        String name = cursor.getString(cursor.getColumnIndex("name"));
        Log.d("查询name的值:", name);
    }
    writableDatabase.close();
}

SQLite Android API operations add, delete, modify and check (CRUD)

Increase data

    public void insertApi() {
        DBHelper helper = new DBHelper(this, "user.db", null, 1);
        //如果数据库不存在,则创建数据,然后获取可读可写的数据库对象,如果存在,直接打开;
        SQLiteDatabase db = helper.getWritableDatabase();
        //把插入的数据全部封装到ContentValues对象中
        ContentValues values = new ContentValues();
        values.put("id",10);
        values.put("name","阿龙");
        values.put("phone","11112233");
        db.insert("user", null, values);
    }

delete data

private void deleteApi() {
        DBHelper helper = new DBHelper(this, "user.db", null, 1);
        //如果数据不存在,则创建数据,然后获取可读可写的数据库对象,如果存在,直接打开;
        SQLiteDatabase db = helper.getWritableDatabase();
        
        //删除name=阿龙 id=10的记录
        db.delete("user","name =? and id = ?",new String[]{"阿龙","10"});
}

change the data

private void updateApi() {
     DBHelper helper = new DBHelper(this, "user.db", null, 1);
     //如果数据不存在,则创建数据,然后获取可读可写的数据库对象,如果存在,直接打开;
     SQLiteDatabase db = helper.getWritableDatabase();
     ContentValues values = new ContentValues();
     values.put("name", "张三丰");
     //修改id=1的记录中的name = 张三丰
     db.update("user", values, "id = ?", new String[]{"1"});
 }

Query data

Query all data in the table

private void selectApi() {
     DBHelper helper = new DBHelper(this, "user.db", null, 1);
     //如果数据不存在,则创建数据,然后获取可读可写的数据库对象,如果存在,直接打开;
     SQLiteDatabase db = helper.getWritableDatabase();

     //查询数据所有数据
     Cursor cursor = db.query("user", null, null, null, null, null, null,null);

     while (cursor.moveToNext()) {
          String name = cursor.getString(cursor.getColumnIndex("name"));
          String phone = cursor.getString(cursor.getColumnIndex("phone"));
          Log.d("查询name的值:", name + " phone: " + phone);
     }
}

Method and parameter description

public  Cursor query(String table,String[] columns,String selection,String[]  selectionArgs,String groupBy,String having,String orderBy,String limit);

Explanation of the meaning of each parameter:
table: table name
columns: column name array
selection: conditional statement, equivalent to where
selectionArgs: conditional statement, parameter array
groupBy: grouping column
having: grouping condition
orderBy: sorting sequence
limit: paging query limit
Cursor: return Value, equivalent to the result set ResultSet

Affairs

SQLite also supports transactions

 

to sum up

SQLite does not check the data type, it is stored in the database in the form of a string; there will be hidden dangers, and the data type must be standardized during the use;

 

Guess you like

Origin blog.csdn.net/ezconn/article/details/108655624