Getting quoted string using ContentValues for insertOrThrow
如何在 ContentValues 对象中获取带引号的字符串以在 sqlite 插入中使用? SQLite 似乎对我的价值
感到窒息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | //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(); } |
这给了我这个错误
1 | 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 语句应该是
1 | INSERT INTO tbl_ending (fotonormal, foto, link) VALUES ("https://example.com/fotonormal.jpg","https://example.com/foto.jpg", "https://example.com/"); |
我怎样才能得到这个输出呢?
这当然是很奇怪的行为。您的 map.put 语句中基本上有弱类型,这无济于事。我倾向于显式转换 getValue 参数,只是为了看看是否能解决问题,即:
1 2 3 | map.put("foto", (String) getValue(elm,"foto")); map.put("fotonormal", (String) getValue(elm,"foto-normal")); map.put("link", (String) getValue(elm,"link")); |
菲利普的回答让我走上了正确的道路,我将函数 getValue 的返回值转换为字符串
1 | return (String) getElementValue(n.item(0)); |
现在我可以成功插入了。