使用ContentValues for insertOrThrow获取引用的字符串
•浏览 1
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/);INSERT INTO tbl_ending (fotonormal, foto, link) VALUES ("https://example.com/fotonormal.jpg","https://example.com/foto.jpg", "https://example.com/");map.put("foto", (String) getValue(elm,"foto"));
map.put("fotonormal", (String) getValue(elm,"foto-normal"));
map.put("link", (String) getValue(elm,"link"));return (String) getElementValue(n.item(0));
这给了我这个错误
//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/);INSERT INTO tbl_ending (fotonormal, foto, link) VALUES ("https://example.com/fotonormal.jpg","https://example.com/foto.jpg", "https://example.com/");map.put("foto", (String) getValue(elm,"foto"));
map.put("fotonormal", (String) getValue(elm,"foto-normal"));
map.put("link", (String) getValue(elm,"link"));return (String) getElementValue(n.item(0));
我认为 SQL 语句应该是
//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/);INSERT INTO tbl_ending (fotonormal, foto, link) VALUES ("https://example.com/fotonormal.jpg","https://example.com/foto.jpg", "https://example.com/");map.put("foto", (String) getValue(elm,"foto"));
map.put("fotonormal", (String) getValue(elm,"foto-normal"));
map.put("link", (String) getValue(elm,"link"));return (String) getElementValue(n.item(0));
我怎样才能得到这个输出呢?
这当然是很奇怪的行为。您的 map.put 语句中基本上有弱类型,这无济于事。我倾向于显式转换 getValue 参数,只是为了看看是否能解决问题,即:
//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/);INSERT INTO tbl_ending (fotonormal, foto, link) VALUES ("https://example.com/fotonormal.jpg","https://example.com/foto.jpg", "https://example.com/");map.put("foto", (String) getValue(elm,"foto"));
map.put("fotonormal", (String) getValue(elm,"foto-normal"));
map.put("link", (String) getValue(elm,"link"));return (String) getElementValue(n.item(0));
菲利普的回答让我走上了正确的道路,我将函数 getValue 的返回值转换为字符串
//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/);INSERT INTO tbl_ending (fotonormal, foto, link) VALUES ("https://example.com/fotonormal.jpg","https://example.com/foto.jpg", "https://example.com/");map.put("foto", (String) getValue(elm,"foto"));
map.put("fotonormal", (String) getValue(elm,"foto-normal"));
map.put("link", (String) getValue(elm,"link"));return (String) getElementValue(n.item(0));
现在我可以成功插入了。