繁体   English   中英

SQLiteDatabase并发便利性更新,查询未更新android

[英]SQLiteDatabase concurrent convenience update, query not updated android

我正在尝试使用db更新SQLiteDatabase条目的字段。 update(...)方法,但似乎未存储该值。 我已经尝试了便利数据库。 在执行update方法之后立即执行query(...)方法,发现该条目仍像update之前一样被存储。

在查询之前,我是否需要等待某些后台工作,或者我在哪里出错? 我正在按照从多个线程访问的SQLite DB中的建议使用单例扩展的SQLiteOpenHelperdbHelper ),甚至尝试在新线程中从帮助程序获取db的新可读实例,如以下代码所示:

        ContentValues deviceListEntry = new ContentValues();
        deviceListEntry.put(DeviceListDBEntry.NODE_ID, nodeID);
...

...

        String WHERE = DeviceListDBEntry.NODE_ID + " = ?";
        final String[] WHERE_ARG = {String.valueOf(nodeID)};
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        int listings = 0;
        try {
            //Update the device in the database DeviceList table
            listings = db.update(
                    DeviceListDBEntry.TABLE_NAME,
                    deviceListEntry,
                    WHERE,
                    WHERE_ARG
            );
        } catch (Exception e) {
            throw new ApiHandlerException("db.update(DeviceList, node " + nodeID + ")", e);
        }
        Log.e("updateDBdevice", " node " + device.getNodeID() + " listening = " + device.isListening());


        final String[] TABLE_COLUMNS = {
                DeviceListDBEntry.DEVICE_TYPE,
                DeviceListDBEntry.INTERVIEWED,
                DeviceListDBEntry.DEVICE_JSON
        };

        final String where = WHERE;
        new Thread(new Runnable() {
            @Override
            public void run() {
                SQLiteDatabase db2 = dbHelper.getReadableDatabase();

                Cursor deviceEntry = db2.query(
                        DeviceListDBEntry.TABLE_NAME,   //FROM      DeviceList  Table
                        TABLE_COLUMNS,                  //SELECT    *           columns
                        where,                          //WHERE     nodeID =
                        WHERE_ARG,                      //args      nodeID
                        null,
                        null,
                        null
                );

                if (!deviceEntry.moveToFirst()) throw new ApiHandlerException("DeviceListDB no entry found - WHERE nodeID = " + nodeID);
                if (deviceEntry.getCount() > 1) throw new ApiHandlerException("DeviceListDB duplicate entries - WHERE nodeID = " + nodeID);

                String deviceJson = deviceEntry.getString(deviceEntry.getColumnIndexOrThrow(DeviceListDBEntry.DEVICE_JSON));

                Log.e("updateDBdevice retreive", " node " + nodeID + " JSON : " + deviceJson);
            }
        }).start();

我正在使用Gson对象将device类解析为存储在数据库中的JSON对象。 我知道这在使用数据库时有效。 insert(...)方法。

这里的查询仅用于查看更新是否成功,因为我发现使用其他延迟线程(使用对象锁和相同的SQLiteOpenHelper同步)的显式查询返回的值未更新。

我是否有明显的遗漏,还是应该考虑使用db上的原始SQL命令?

我的错误是我发现实际上没有将更新的JSON对象添加到新条目中。 随后, deviceJson列未更新,但更新了db。 update()已执行...

如果“ WHERE”子句具有“文本”列比较,则在值周围使用单引号。 在您的情况下,请尝试以下行(请注意在单引号附近?

String WHERE = DeviceListDBEntry.NODE_ID + " = '?'";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM