android - 在 ContentValues 中查询

标签 android performance sqlite cursor content-values

我必须在我的 SQLite 数据库 和一些表中插入很多行 必须使用其他表的值将每一行的特定值转换为其他值。 实际上,我有这个功能运行良好:

public void myFunction( String tableName, String attrName, ContentValues currentVal ) { 

    SQLiteDatabase database = this.getReadableDatabase();
    Cursor c = database.rawQuery("SELECT colName FROM "+tableName+" WHERE "+tableName+".otherColName = " + currentVal.getAsString(attrName), null);

    Long realValue = null;
    if( c.moveToFirst() ) { 
        realValue = c.getLong(0);
    }
    if( c != null ) c.close();

    currentVal.put( attrName, realValue );

}

但是,因为这部分,这真的很耗时:

if( c.moveToFirst() ) { 
    realValue = c.getLong(0);
}

所以我想知道是否有一种方法可以像这样直接在 ContantValue 的值中设置查询:

currentVal.put( attrName, "SELECT colName FROM "+tableName+" WHERE "+tableName+".otherColName = " + currentVal.getAsString(attrName));

并且查询将与插入查询同时执行以替换值。

先谢谢你!

最佳答案

不,ContentValues 只是数据 HashMap 的包装器。可以尝试使用索引来加速查询。

关于android - 在 ContentValues 中查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34786833/

相关文章:

android - Sencha Touch 2.3/Cordova : Build run directly on device

Cocoa SQLite何时关闭数据库?

Python SQLite3 SQL注入(inject)漏洞代码

java - 如何在android中提取hashmap的arraylist列表并循环遍历它?

android - 需要从 GCM 迁移到 FCM?

java - CWAC-相机在触摸屏上拍照

javascript - 通过 ajax 响应获取变量或使用 .load() 加载 php 页面是否更快

jquery - 显示然后隐藏文本 JS Jquery

performance - 查找 2D 平面上的 2 个对象是否会发生碰撞的算法

javascript - 从 SQLite 中提取数据