【问题标题】:fast read of blobs within SQLite using simpleQueryForBlobFileDescriptor?使用 simpleQueryForBlobFileDescriptor 在 SQLite 中快速读取 blob?
【发布时间】:2014-03-21 20:28:43
【问题描述】:

我正在使用以下代码从我的 Android 应用程序中的 SQlite 读取大小在 100kb 和 1000kb 之间的 blob:

public Object getCachefromDb(String sIdentifier){

    String sSQL = " Select cache from cachtable where identifier='" + sIdentifier + "'";

    Cursor c = null;
    try {
        c = connection_chronica.rawQuery(sSQL, null);
    } catch (SQLiteException e) {
        Log.v("SQLite Excetion", e.getMessage());
    }


    c.moveToFirst();
    Log.v("DEBUG load Cache","sIdentifier : " + sIdentifier);
    byte[] bData=null;
    try{
        bData = c.getBlob(0);
    }catch(Exception e){
        e.printStackTrace();
    }
    Object o = null;

    if (bData!=null){
        ByteArrayInputStream bos = new ByteArrayInputStream(bData);
        ObjectInputStream ois;
        try {
            ois = new ObjectInputStream(bos);
            o=ois.readObject();

        } catch (StreamCorruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    c.close();
    return o;

}

我想优化阅读速度,发现有文章提到 simpleQueryForBlobFileDescriptor。

我的问题:这有助于我更快地阅读 BLOBS 吗?如果是这样,我该如何使用它?

来自其他帖子的示例:

SQLiteStatement get = mDb.compileStatement(
    "SELECT blobColumn" + 
    " FROM tableName" +
    " WHERE _id = 1" +
    " LIMIT 1"
);

ParcelFileDescriptor result = get.simpleQueryForBlobFileDescriptor();
FileInputStream fis = new FileInputStream(result.getFileDescriptor()); // read like any other

【问题讨论】:

    标签: android sqlite blob


    【解决方案1】:

    我的测试结果显示速度较慢。

    经过长时间的测试,我发现使用 simpleQueryForBlobFileDescriptor 比较慢。请参阅以下代码。例如,我的旧代码在 390 毫秒内读取一个 blob,而使用 simpleQueryForBlobFileDescriptor 的新代码在 805 毫秒内读取相同的 blob。我在某处读到 simpleQueryForBlobFileDescriptor 对于 blob 读数应该非常快,但这似乎不在我的测试中。也许我做得不好? ( 但愿如此 )。任何其他提示。

    public Object getCachefromDb_old(String sIdentifier){
    
        Log.v("DEBUG LOAD BLOB","Start : " + sIdentifier);
        Object o = null;
        try {
            // Erstelle ein SQLStatemant mit einer InClause
            String sSQL = " Select cache from cachetable where identifier='" + sIdentifier + "'";
            SQLiteStatement get = connection_chronica.compileStatement(sSQL);
            ParcelFileDescriptor result = get.simpleQueryForBlobFileDescriptor();
            FileInputStream fis = new FileInputStream(result.getFileDescriptor());
            ObjectInputStream inStream = new ObjectInputStream(fis);
            o=inStream.readObject();
    
        } catch (StreamCorruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Log.v("DEBUG LOAD BLOB","End : " + sIdentifier);
    
        return o;
    
    }
    

    【讨论】:

      猜你喜欢
      • 2019-10-22
      • 2014-07-23
      • 2013-09-20
      • 1970-01-01
      • 2012-08-07
      • 2016-03-02
      • 1970-01-01
      • 2015-08-15
      • 1970-01-01
      相关资源
      最近更新 更多