use*_*937 2 java sqlite android
我得到unrecognized token error当我尝试包括Api_key列和我插入查询值,否则没有它,它工作正常.
这是代码:
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);
}
Run Code Online (Sandbox Code Playgroud)
这是我的logcat:
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)
Run Code Online (Sandbox Code Playgroud)
bcl*_*mer 11
你应该在非数字字符串周围加上刻度线.
String s="INSERT OR IGNORE INTO Results(AuditID,CriteriaID,ChapterID,Api_key) VALUES("+auditid+","+crit_id+","+current_chap+",`"+apikey+"`)";
Run Code Online (Sandbox Code Playgroud)
注意'标记'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);
}
Run Code Online (Sandbox Code Playgroud)
Apikey 是一个字符串,因此对于 sql,您需要将它放在引号内。
String s="INSERT OR IGNORE INTO Results(AuditID,CriteriaID,ChapterID,Api_key) VALUES("+auditid+","+crit_id+","+current_chap+",'"+apikey+"')";
| 归档时间: |
|
| 查看次数: |
27165 次 |
| 最近记录: |