如何在Android平台下查询@capacitor-community/sqlite中的blob列

ewm0tg9j  于 2023-06-23  发布在  SQLite
关注(0)|答案(1)|浏览(290)

我有一个预填充的测试sqlite数据库,其中有一个blob列的表,其中保存png图像。我在Android手机上运行@capacitor-community/sqlite平台的ionic/angular/capacitor中尝试查询。

private getConnection = () => {
    return new SQLiteConnection(CapacitorSQLite);
};
private getDb = async (conn: SQLiteConnection, dbName: string): Promise<SQLiteDBConnection> => {
    const db: SQLiteDBConnection = await conn.createConnection(
      dbName, false, 'no-encryption', 1, false
    );
    await db.open();
    return db;
};
private readTable = async (db: SQLiteDBConnection): Promise<DBSQLiteValues> => {
    console.log('home: readTable: entered');

    // returns "[B@e44e523" for img column instead of "iVBORw0..." or "data:image/png;base64,iVBORw0..." or somthing like that
    const stmt = `SELECT id, name, img FROM teach;`;
    console.log('home: readTable: stmt=', stmt);

    const ret: DBSQLiteValues = await db.query(stmt, []);
    console.log('home: readTable: ret=', ret);

    return ret;
};
async ngOnInit() {
    this.conn = this.getConnection();
    console.log('home: ngOnInit: conn=', this.conn);

    this.db = await this.getDb(this.conn, 'testdb');
    console.log('home: ngOnInit: db=', this.db);

    const rows: DBSQLiteValues = await this.readTable(this.db);
    console.log('home: ngOnInit: rows=', rows);
}

我也尝试了一下查询:

const stmt = `SELECT id, name, BASE64(img) AS img FROM teach;`;

但它返回错误:

Query: in selectSQL cursor no such function: BASE64: , while compiling: SELECT id, name, BASE64(img) AS img FROM teach;
  • 顺便说一句,预填充的sqlite数据库不是使用@capacitor-community/sqlite创建的。
  • 使用Android Studio手动复制到/data/data/io.ionic.starter/databases
  • 此外,目前我正在Android 12手机上进行独家测试,没有其他平台/版本。
  • 重现问题的参考示例项目为here,包括env设置。
    更新

我还在插件的java层添加了调试消息,发现它从Android API接收blob正确(在logcat中)。
com.getcapacitor.community.database.sqlite.SQLite.Database#selectSQL()

case FIELD_TYPE_BLOB:
    row.put(colName, c.getBlob(index));
    byte[] bArray = c.getBlob(index);
    Log.v(TAG, "=====>> BLOB: index=" + index + ", blob=" + bArray + ", len=" + bArray.length);
    Log.v(TAG, "=====>> BLOB: [0]=" + bArray[0]);
    Log.v(TAG, "=====>> BLOB: [1]=" + bArray[1]);
    Log.v(TAG, "=====>> BLOB: [2]=" + bArray[2]);
    Log.v(TAG, "=====>> BLOB: [3]=" + bArray[3]);
    Log.v(TAG, "=====>> BLOB: [4]=" + bArray[4]);
    Log.v(TAG, "=====>> BLOB: [5]=" + bArray[5]);
    Log.v(TAG, "=====>> BLOB: [6]=" + bArray[6]);
    Log.v(TAG, "=====>> BLOB: [7]=" + bArray[7]);
    break;
06-05 18:13:07.200 26234 26234 I Capacitor/Console: File: https://localhost/6997.a86a1992d81b27be.js - Line 1 - Msg: home: readTable: stmt= SELECT id, name, img FROM teach;
06-05 18:13:07.201 26234 26234 V Capacitor/Plugin: To native (Capacitor plugin): callbackId: 69203466, pluginId: CapacitorSQLite, methodName: query
06-05 18:13:07.201 26234 26234 V Capacitor: callback: 69203466, pluginId: CapacitorSQLite, methodName: query, methodData: {"database":"testdb","statement":"SELECT id, name, img FROM teach;","values":[],"readonly":false}
06-05 18:13:07.203 26234 26322 V com.getcapacitor.community.database.sqlite.SQLite.Database: =====>> INT: index=0, int=1
06-05 18:13:07.203 26234 26322 V com.getcapacitor.community.database.sqlite.SQLite.Database: =====>> STR: index=1, str=11:41:10 AM
06-05 18:13:07.203 26234 26322 V com.getcapacitor.community.database.sqlite.SQLite.Database: =====>> BLOB: index=2, blob=[B@61b1fd9, len=1741
06-05 18:13:07.203 26234 26322 V com.getcapacitor.community.database.sqlite.SQLite.Database: =====>> BLOB: [0]=-119
06-05 18:13:07.203 26234 26322 V com.getcapacitor.community.database.sqlite.SQLite.Database: =====>> BLOB: [1]=80
06-05 18:13:07.203 26234 26322 V com.getcapacitor.community.database.sqlite.SQLite.Database: =====>> BLOB: [2]=78
06-05 18:13:07.203 26234 26322 V com.getcapacitor.community.database.sqlite.SQLite.Database: =====>> BLOB: [3]=71
06-05 18:13:07.203 26234 26322 V com.getcapacitor.community.database.sqlite.SQLite.Database: =====>> BLOB: [4]=13
06-05 18:13:07.203 26234 26322 V com.getcapacitor.community.database.sqlite.SQLite.Database: =====>> BLOB: [5]=10
06-05 18:13:07.203 26234 26322 V com.getcapacitor.community.database.sqlite.SQLite.Database: =====>> BLOB: [6]=26
06-05 18:13:07.203 26234 26322 V com.getcapacitor.community.database.sqlite.SQLite.Database: =====>> BLOB: [7]=10

我还在com.getcapacitor.community.database.sqlite.CapacitorSQLite#query()中添加了调试消息,但由于某些原因,这不会出现在logcat中。

ArrayList<Object> arrValues = uSqlite.objectJSArrayToArrayList(values);
res = db.selectSQL(statement, arrValues);
Log.v(TAG, "----->> res=" + res.toString());

//logcat中没有来自www.example.com的调试消息CapacitorSQLite.java
我还在com.getcapacitor.community.database.sqlite.CapacitorSQLitePlugin#query()中添加了调试消息。在这里,我可以正确地看到INTEGERTEXT列的值,但不能看到logcat中的BLOB列。

06-05 18:13:07.203 26234 26322 V com.getcapacitor.community.database.sqlite.CapacitorSQLitePlugin: .....>> res={"id":1,"name":"11:41:10 AM","img":"[B@816dd9e"}
06-05 18:13:07.204 26234 26322 V com.getcapacitor.community.database.sqlite.CapacitorSQLitePlugin: .....>> res={"id":1,"name":"11:41:10 AM","img":"[B@816dd9e"}
06-05 18:13:07.204 26234 26322 V com.getcapacitor.community.database.sqlite.CapacitorSQLitePlugin: .....>> class =com.getcapacitor.JSObject
06-05 18:13:07.204 26234 26322 V com.getcapacitor.community.database.sqlite.CapacitorSQLitePlugin: .....>> id=1
06-05 18:13:07.204 26234 26322 V com.getcapacitor.community.database.sqlite.CapacitorSQLitePlugin: .....>> name=11:41:10 AM
06-05 18:13:07.204 26234 26322 V com.getcapacitor.community.database.sqlite.CapacitorSQLitePlugin: .....>> img=null

预期"img":"iVBORw0..."(或类似的东西,取决于png图像)
实际"img":"[B@816dd9e"

wfsdck30

wfsdck301#

我建议将img保存为base64编码字符串,以便从sqlite中检索数据。
但是如果你想使用CRU(d)blob -尝试使用这个插件版本- cordova-sqlite-ext(它在文档中提到)。
据我所知,capacitor-community/sqlite不支持blob。
其他选项-保存img为base64。
我也在使用它(capacitor-community/sqlite)--我通过保存数组缓冲区而不是base64文件。
希望它能帮助你;)

相关问题