如何使用 get readable 读取值并在同一函数中使用 set writeable 插入另一个 table

How to read value with get readable and insert into another table with set writeable in the same function

我需要使用 get readable 读取值并插入另一个 table 并在同一函数中设置 writeable。

public void Check(String msg){

    //getdata from database
    String query="SELECT "+ CUL_ID +" FROM  " + STUDENT_TABLE + " WHERE "+ CUL_QRCODE +" like "+ msg +" ";
    SQLiteDatabase db1=this.getReadableDatabase();
    db1.execSQL(query);
    db1.close();
// I need to return this query in string variable value and put it into the queryString

    String value = "";
    String queryString ="INSERT INTO " + TABLE_PRESENCE_TABLE + " VALUES (" + value + " ,( SELECT count( "+CUL_ID_PRESENCE+" ) FROM  " + TABLE_PRESENCE_TABLE + " WHERE "+ CUL_ID_PRESENCE+ " = ( SELECT "+ CUL_ID +" FROM "+ STUDENT_TABLE +" WHERE "+ CUL_QRCODE + " = "+ msg +"))+"+1+" , datetime('now'));";
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL(queryString);
    db.close();

}

I need to read value with get readable and insert into another table with set writeable in the same function.

不需要。只要你有 used/coded 第二列的子查询 :-

( SELECT count( "+CUL_ID_PRESENCE+" ) FROM  " + TABLE_PRESENCE_TABLE + " WHERE "+ CUL_ID_PRESENCE+ " = ( SELECT "+ CUL_ID +" FROM "+ STUDENT_TABLE +" WHERE "+ CUL_QRCODE + " = "+ msg +"))+"+1+" 

您可以为第一个值编写子查询,即将 value 替换为获得 value 的查询。

例如您可以在没有前面的查询的情况下进行单个插入,按照 :-

INSERT INTO presence VALUES
    (
        coalesce((SELECT cul_id FROM student WHERE cul_qrcode = 'message'),-99),
        (SELECT count(cul_id_presence) FROM  presence WHERE CUL_ID_PRESENCE = (SELECT CUL_ID FROM STUDENT WHERE CUL_QRCODE = 'message')+1),
        datetime('now')
    )
;
  • 请注意,合并函数会将 NULL(如果没有 cul_qrcode LIKE 消息)转换为另一个值(在上述情况下为 -99)。

  • table 和列名已被猜测,因此它们可能与您实际拥有的不匹配。

  • 否则,要实际接收查询结果,您需要在 Cursor BUT you do not use execSQL, you use rawQuery or the query 便捷方法中捕获结果。

  • execSQL Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.


How can i Receive the value of getReadableDatabase() in variable string that's it

要使用 query 然后使用 insert 你会得到类似 :-

Cursor csr = db1.rawQuery(query,null); 
String value = "-99"; /* assume that if nothing is returned then use -99 */
if(csr.moveToFirst) {
    value = csr.getString(0);
}
csr.close(); /* SHOULD ALWAYS CLOSE CURSORS WHEN FINISHED WITH THEM */

但是,将参数连接到 SQL 是错误的做法,因为这被认为是 SQL 注入的可能性。相反,您应该绑定参数(这将克服一个主要缺陷,因为您似乎可能没有将 msg 括在单引号中,绑定参数会将参数括在单引号中)。所以上面应该是:-

public void Check(String msg){

    //getdata from database
    String query="SELECT "+ CUL_ID +" FROM  " + STUDENT_TABLE + " WHERE "+ CUL_QRCODE +" like ?"; //<<<<< use ? to allow bind of msg
    SQLiteDatabase db1=this.getReadableDatabase();
    Cursor csr = db1.rawQuery(query,new String[]{msg}); 
    String value = "-99"; /* assume that if nothing is returned then use -99 */
    if(csr.moveToFirst) {
        value = csr.getString(0);
    }
    csr.close(); /* SHOULD ALWAYS CLOSE CURSORS WHEN FINISHED WITH THEM */
    ....

此外

无需关闭数据库然后重新打开它就可以明显地在可读和可写之间切换table。也就是说 getReadableDatabase 如果可能的话 return 将是一个可写的数据库,除非有某种原因导致数据库无法作为 writable 打开。如果只读数据库被 returned 那么随后的 getWritableDatabase 很可能会失败,除非以某种方式解决了潜在的问题。

根据:-

Create and/or open a database. This will be the same object returned by getWritableDatabase() unless some problem, such as a full disk, requires the database to be opened read-only. In that case, a read-only database object will be returned. If the problem is fixed, a future call to getWritableDatabase() may succeed, in which case the read-only database object will be closed and the read/write object will be returned in the future.