Android数据库错误语法

时间:2015-04-01 20:32:04

标签: android sql database

我的查询是

return database.rawQuery(“SELECT * FROM”+ TABLE_NAME +“WHERE”+ HISTORY_COLUMN_NAME +“=”+ pref.getString(“KEY”,null),null);

错误是

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sset.jibin.wakemethere/com.sset.jibin.wakemethere.History}: android.database.sqlite.SQLiteException: near "=": syntax error (code 1): , while compiling: SELECT * FROM history WHERE name =

使用query()修复它。

1 个答案:

答案 0 :(得分:1)

您需要转义参数,如下所示:

return database.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + HISTORY_COLUMN_NAME + " = '" + pref.getString("KEY", null) + "'", null);  

这将导致SELECT * FROM <table> WHERE <column> = '<value>'

等查询

使用query()代替rawQuery()让SDK逃脱您的参数会更明智。您的查询中没有任何内容对query()来说太难了。