关于android:使用ContentValues for insertOrThrow获取引用的字符串

Getting quoted string using ContentValues for insertOrThrow

如何在 ContentValues 对象中获取带引号的字符串以在 sqlite 插入中使用? SQLite 似乎对我的价值

感到窒息

    //inside a for loop of a NodeList
    ContentValues map = new ContentValues();
    Element elm;
    elm = (Element) nodes.item(i);
    String elmname = elm.getTagName();
    map.put("foto", getValue(elm,"foto"));
    map.put("fotonormal", getValue(elm,"foto-normal"));
    map.put("link", getValue(elm,"link"));

    myDatabase.beginTransaction();
    try {
        myDatabase.insertOrThrow(TABLE_ENDING, null, map);
        Log.i(TAG,"Added");
        myDatabase.setTransactionSuccessful();
    } catch (Exception err) {
        Log.i(TAG,"Transaction failed. Exception:" + err.getMessage());
    } finally {
        myDatabase.endTransaction();
    }

这给了我这个错误

Transaction failed. Exception: unrecognized token:":": INSERT INTO tbl_ending (fotonormal, foto, link) VALUES (https://example.com/fotonormal.jpg, https://example.com/foto.jpg, https://example.com/);

我认为 SQL 语句应该是

INSERT INTO tbl_ending (fotonormal, foto, link) VALUES ("https://example.com/fotonormal.jpg","https://example.com/foto.jpg", "https://example.com/");

我怎样才能得到这个输出呢?


这当然是很奇怪的行为。您的 map.put 语句中基本上有弱类型,这无济于事。我倾向于显式转换 getValue 参数,只是为了看看是否能解决问题,即:

map.put("foto", (String) getValue(elm,"foto"));
map.put("fotonormal", (String) getValue(elm,"foto-normal"));
map.put("link", (String) getValue(elm,"link"));

相关讨论

  • 现在我收到错误交易失败。异常:错误代码 19:约束失败。我相信这与引用太多有关:stackoverflow.com/questions/5364576/...

菲利普的回答让我走上了正确的道路,我将函数 getValue 的返回值转换为字符串

return (String) getElementValue(n.item(0));

现在我可以成功插入了。


以上是关于android:使用ContentValues for insertOrThrow获取引用的字符串的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>