【问题标题】:SQLite unrecognized token exception in Insert插入中的 SQLite 无法识别令牌异常
【发布时间】:2013-10-11 17:25:37
【问题描述】:

当我尝试在我的插入查询中包含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)

【问题讨论】:

    标签: java android sqlite


    【解决方案1】:

    你应该在你的非数字字符串周围加上刻度线。

    String s="INSERT OR IGNORE INTO Results(AuditID,CriteriaID,ChapterID,Api_key) VALUES("+auditid+","+crit_id+","+current_chap+",`"+apikey+"`)";
    

    注意“apikey”周围的`标记

    SQLite 看到 - 并感到困惑,为什么它不在字符串中。

    【讨论】:

    • 您应该将答案标记为正确,以便问题进入已解决状态。
    • @LS_dev 我完全同意,我只是在回答他的问题。
    • 快速说明:问题不是-,而是字母数字。如果它是2345-2344,它将执行算术并使用结果。问题是解析3249f6dc:既不是数字也不是功能,...
    【解决方案2】:

    永远不要在 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);
    }
    

    【讨论】:

      【解决方案3】:

      Apikey 是一个字符串,因此对于 sql,您需要将其放在引号内。 String s="INSERT OR IGNORE INTO Results(AuditID,CriteriaID,ChapterID,Api_key) VALUES("+auditid+","+crit_id+","+current_chap+",'"+apikey+"')";

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多