在sqlite数据库中搜索数据时出错

时间:2018-07-11 08:39:51

标签: java android sql database sqlite

所以,我想在我的数据库(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

它是按列名而不是其中的数据进行搜索的,对吗?为什么? 谢谢!

1 个答案:

答案 0 :(得分:3)

需要在双引号中放入一个值。将查询更改为

String sql = "SELECT * FROM " + TABLE_STUFF + " WHERE NOME = \"" + stuff.getNome() + "\"";