SQLiteOpenHelper operation database

One: Create a new class to inherit SQLiteOpenHelper

public class MyOpenHelper extends SQLiteOpenHelper {
	 /*
    * 构造方法中
    * 参数context表示用来打开或创建数据据的上下文;
    * name指定了数据库文件;factory用来创建cursor对象,
    * 如果使用默认的factory,则将该参数设置为null;
    * version表示数据库的版本号,该版本号从1开始依次递增。
    * */
	public MyOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }
	/*
	* 在派生的自类中需要重写onCreate()方法。
	* 当数据库文件被首次创建时会调用该函数,
	* 也就是在调用调用SQLiteOpenHelper类的getWritableDatabase()或者getReadableDatabase()时会调用该方法。
	* 在该方法中主要完成数据库表的初始化(创建和记录的添加)。 
	* */
      @Override
    public void onCreate(SQLiteDatabase db) {
        System.out.println(123456789);
    }

	/*
	* 当数据库需要更新的时候调用该方法。可以通过该方法实现删除表、添加表或者更新表。
	* */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        System.out.println(123);
        db.execSQL("CREATE table student(id Interger primary key autoincrement,name varchar(20),age int)");
    }
}

Two: in Activity

 MyOpenHelper  myOpenHelper = new MyOpenHelper(getApplicationContext());
 SQLiteDatabase writableDatabase = myOpenHelper.getWritableDatabase();
 writableDatabase.execSQL();
 writableDatabase.close();

2.2 Create
a record The method for creating a record is similar to the method for creating a table. The SQL statement is first constructed and then executed.

String insertSql = “insert into user(username, password) values(?,?)”;
DB.execSQL(insertSql, new String[]{“yang”, “123”});

The function of the above code is to insert a record in the "user" table, the value of the username field of the record is "yang", and the value of the "password" field is "123".

2.3 删除记录
String deleteSql = “delete from user where username=?”;
DB.execSQL(deleteSql, new String[]{“yang”});

The role of the above code is to delete the record named "yang" in the user table.

2.4 修改记录
String updateSql = “update user set username=?,password=? where _id=?”;
DB.execSQL(updateSql, new String[]{“yang”, “456”, “0”});

The function of the above code is to set the value of the "username" field of the record with ID 0 in the user table to "yang", and the value of the "password" field to "456".

2.5 Query Records
The method used in the above operation records is different. When querying records, the rawQuery () method of SQLiteDatabase is used. The format of this method is

public Cursor rawQuery(String sql,
String[] selectionArgs)

Among them, the parameter sql specifies the SQL statement of the query record; selectionArgs specifies the value represented by? Contained in sql. If sql does not contain?, Then this parameter can be set to null. The return value of the rawQuery () method is the result of the query, and its type is Cursor.

String selectSql = “select _id, username, password from user”;
Cursor cursor = DB.rawQuery(selectSql, null);

The function of the above code is to query all records in the table user and save the records in the cursor.
2.6 Close the database
Call the close () method of the SQLiteDatabase class to close the open database.
DB.close ();

Published 16 original articles · praised 0 · visits 838

Guess you like

Origin blog.csdn.net/weixin_45830683/article/details/103323939