android - ContentValues 条目正在转换为 NULL

标签 android sqlite

我在将 ContentValues 输入到我的程序中的数据库时遇到了一些问题,事实证明这很难理解。我有一个 ContentValues 对象,我向其中添加了大约 50 个条目并且一切正常,除了一个存储为“null”的条目。根据 LogCat 输出,我知道进入 ContentValues 的数据不为空。

相关代码:

if (database.isOpen() && !database.isReadOnly()) { //Check that the database is open and not read only
    Log.d(TAG, "Entry string: " + data.getEntryString());
    Log.d(TAG, "Key: " + Helper.COLUMN_ENTRY_STRING);

    ContentValues values = new ContentValues(); //Create a new ContentValues object to store key value pairs for insertion
    //Various values.put() calls...
    values.put(Helper.COLUMN_ENTRY_STRING, data.getEntryString()); //Should put the String "Something"

    Log.d(TAG, "Entry string from values: " + values.getAsString(Helper.COLUMN_ENTRY_STRING));
    //Proceed with database insert here
}

LogCat 响应:

07-27 08:58:47.909: D/Handler(18020): Entry string: Something 
07-27 08:58:47.909: D/Handler(18020): Key: Entry_String 
07-27 08:58:47.909: D/Handler(18020): Entry string from values: null

我已经多次检查代码,但找不到任何解释。任何帮助将不胜感激。

最佳答案

我也遇到过这个问题。

我的情况是因为我在单元测试中使用了 ContentValueContentValue是一个android组件,应该在android环境下使用,但是我在没有android实现的单元测试中使用了它。因此,当我调用 contentValue.put(key, value)contentValue.get(key) 时,由于实现为空,所以什么也没有发生。

我不确定您是否在单元测试或其他没有 android 实现的情况下运行您的代码。也许我的解决方案可以给你一些想法。

关于android - ContentValues 条目正在转换为 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11691642/

相关文章:

android - ViewPager 中带有 WebView 的进度条

android - 以编程方式设置 ListView 标题大小

android - 一个 SQLite 数据库中的多个表

ios - Firebase 离线存储高级 - 手动同步和进度信息

android - 使用 Firebase 从列表中查询每个节点的键

android - 在圆形表面上显示相机预览(舷窗效果)

android - 通过 Volley throw 错误上传图像。具有图像数组和文本数组

python - 在 SQLite3 更新中使用实例变量?

java - Android sqlite 日志报告和 db.close()

python - SQL(ite)快速检索较大字符串(基因组)的多个子字符串