使用ContentValues为insertOrThrow获取引用的字符串

时间:2011-07-03 10:11:27

标签: android insert sqlite

如何在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/");

我怎样才能获得此输出?

2 个答案:

答案 0 :(得分:1)

这当然是非常奇怪的行为。你在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"));

答案 1 :(得分:0)

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

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

现在我可以成功插入。