[英]Android SqLite Exception: unrecognized token in deleting from database
[英]SQLite unrecognized token exception in Insert
當我嘗試在我的插入查詢中包含Api_key
列及其值時, unrecognized token error
,否則,如果沒有它,它將正常工作。
這是代碼:
public void InsertResult(String apikey,String auditid,String crit_id, int current_chap)
{
String s="INSERT OR IGNORE INTO Results(AuditID,CriteriaID,ChapterID,Api_key) VALUES("+auditid+","+crit_id+","+current_chap+","+apikey+")";
sp.execSQL(s);
}
這是我的日志:
10-11 22:45:09.655: E/AndroidRuntime(8124): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.oxtro.trustea/com.oxtro.trustea.ChapterActivity}: android.database.sqlite.SQLiteException: unrecognized token: "3249f6dc" (code 1): , while compiling: INSERT OR IGNORE INTO Results(AuditID,CriteriaID,ChapterID,Api_key) VALUES(1,13,13,3249f6dc-c3ca-4c8d-a4de-df1834c579c4)
您應該在非數字字符串周圍打上勾號。
String s="INSERT OR IGNORE INTO Results(AuditID,CriteriaID,ChapterID,Api_key) VALUES("+auditid+","+crit_id+","+current_chap+",`"+apikey+"`)";
注意“ apikey”周圍的`標記
SQLite看到了-
並感到困惑,為什么它不在字符串中。
永遠不要在您的SQL語句中對字符串進行硬編碼。
用戶輸入的字符串會創建一個SQL注入漏洞。
需要從特殊字符解析任意字符串。
SQL API通常提供綁定方法,使您可以安全地在數據庫中插入任意數據。
在Android SQLite中,對於INSERT
您可以使用:
public void InsertResult(String apikey,String auditid,String crit_id, int current_chap)
{
ContentValues cv=new ContentValues();
cv.put("AuditID", auditid);
cv.put("CriteriaID", crit_id);
cv.put("ChapterID", current_chap);
cv.put("Api_key", apikey);
sp.insert("Results", null, cv);
}
Apikey是一個字符串,因此對於sql,您需要將其放在引號內。 String s="INSERT OR IGNORE INTO Results(AuditID,CriteriaID,ChapterID,Api_key) VALUES("+auditid+","+crit_id+","+current_chap+",'"+apikey+"')";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.