Getting quoted string using ContentValues for insertOrThrow
How can i get q开发者_如何学运维uoted strings in a ContentValues object to be used in a sqlite insert? SQLite seems to be choking on the : of my value
//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();
}
which gives me this error
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/);
i believe the SQL statement should be
INSERT INTO tbl_ending (fotonormal, foto, link) VALUES ("https://example.com/fotonormal.jpg", "https://example.com/foto.jpg", "https://example.com/");
How can I get this output instead?
It is certainly very strange behavior. You have basically got weak typing going on in your map.put statements, which does not help. I would be inclined to explicitly cast the getValue parameters, just to see if that fixes the problem, viz:
map.put("foto", (String) getValue(elm, "foto"));
map.put("fotonormal", (String) getValue(elm, "foto-normal"));
map.put("link", (String) getValue(elm, "link"));
Phillip's answer put me on the right path, I cast the return value of the function getValue as a string
return (String) getElementValue(n.item(0));
and now i can successfully insert.
精彩评论