【发布时间】:2018-07-11 08:39:51
【问题描述】:
所以,我想在我的数据库中搜索一个值(android 上的 sqlite),为此我使用了这种方法:
public Stuff search(Stuff stuff) {
String sql = "SELECT * FROM " + TABLE_STUFF + " WHERE NOME = " + stuff.getNome();
Cursor cursor = this.database.rawQuery(sql, null);
Stuff tempStuff = new Stuff(
//cursor.getLong(0),
cursor.getString(1)
);
if (tempStuff == null)
return null;
else
return tempStuff;
}
我的 stuff.getNome() 是“运动”。当我运行查询时,我收到此错误:
07-11 08:34:42.099 26595-26595/? E/SQLiteLog: (1) no such column: sports
07-11 08:34:42.101 26595-26595/? D/AndroidRuntime: Shutting down VM
07-11 08:34:42.108 26595-26595/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.nelsu.idk, PID: 26595
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nelsu.idk/com.example.nelsu.idk.MainActivity}: android.database.sqlite.SQLiteException: no such column: Sports(code 1): , while compiling: SELECT * FROM STUFF WHERE NOME = Sports
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: android.database.sqlite.SQLiteException: no such column: Sports(code 1): , while compiling: SELECT * FROM STUFF WHERE NOME = Sports
它是按列名搜索,而不是按其中的数据搜索,对吧?为什么? 谢谢!
【问题讨论】:
-
你试过
WHERE NOME = "Sports"吗? -
你能告诉我们创建查询吗?
-
@B001ᛦ 不,但我希望它是通用的,我不想只搜索一个值,我想搜索来自 .getNome() 的任何内容;
-
我不想只搜索一个值.. 我知道...我的意思是使用双引号
""
标签: java android sql database sqlite