我是使用SQLite的新手,因此构建select查询就是我认为的问题。
无论如何,我正在尝试准备一份声明,它将根据当天的情况(在MainActivity
中确定)给出模块名称,模块演讲时间和模块研讨会时间
以下是返回列表的方法 的 getModuleTime
public List<tableModules> getModuleTime(String module1, String module2, String module3, String day) {
List<tableModules> moduleList = new ArrayList<tableModules>();
// Select All Query
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT modulename, modulelecturetime, moduleseminartime FROM " + Table2 + " WHERE modulename=? OR modulename=? OR modulename=? AND WHERE modulelecturedate=? OR moduleseminardate=?", new String[]{module1, module2, module3, day, day});
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
tableModules module = new tableModules();
module.modulename = cursor.getString(1);
module.modulelecturedate = cursor.getString(2);
module.moduleseminardate = cursor.getString(3);
moduleList.add(module);
} while (cursor.moveToNext());
}
// return contact list
return moduleList;
}
这是logcat:
02-23 01:18:27.177: E/SQLiteLog(3050): (1) near "WHERE": syntax error
02-23 01:18:27.192: D/AndroidRuntime(3050): Shutting down VM
02-23 01:18:27.217: E/AndroidRuntime(3050): FATAL EXCEPTION: main
02-23 01:18:27.217: E/AndroidRuntime(3050): Process: com.example.project, PID: 3050
02-23 01:18:27.217: E/AndroidRuntime(3050): java.lang.RuntimeException: Unable to resume activity {com.example.project/com.example.project.MainActivity}: android.database.sqlite.SQLiteException: near "WHERE": syntax error (code 1): , while compiling: SELECT modulename, modulelecturetime, moduleseminartime FROM Modules WHERE modulename=? OR modulename=? OR modulename=? AND WHERE modulelecturedate=? OR moduleseminardate=?
02-23 01:18:27.217: E/AndroidRuntime(3050): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2989)
02-23 01:18:27.217: E/AndroidRuntime(3050): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3020)
02-23 01:18:27.217: E/AndroidRuntime(3050): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
02-23 01:18:27.217: E/AndroidRuntime(3050): at android.app.ActivityThread.access$800(ActivityThread.java:151)
02-23 01:18:27.217: E/AndroidRuntime(3050): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
02-23 01:18:27.217: E/AndroidRuntime(3050): at android.os.Handler.dispatchMessage(Handler.java:102)
02-23 01:18:27.217: E/AndroidRuntime(3050): at android.os.Looper.loop(Looper.java:135)
02-23 01:18:27.217: E/AndroidRuntime(3050): at android.app.ActivityThread.main(ActivityThread.java:5257)
02-23 01:18:27.217: E/AndroidRuntime(3050): at java.lang.reflect.Method.invoke(Native Method)
02-23 01:18:27.217: E/AndroidRuntime(3050): at java.lang.reflect.Method.invoke(Method.java:372)
02-23 01:18:27.217: E/AndroidRuntime(3050): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
02-23 01:18:27.217: E/AndroidRuntime(3050): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
02-23 01:18:27.217: E/AndroidRuntime(3050): Caused by: android.database.sqlite.SQLiteException: near "WHERE": syntax error (code 1): , while compiling: SELECT modulename, modulelecturetime, moduleseminartime FROM Modules WHERE modulename=? OR modulename=? OR modulename=? AND WHERE modulelecturedate=? OR moduleseminardate=?
02-23 01:18:27.217: E/AndroidRuntime(3050): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-23 01:18:27.217: E/AndroidRuntime(3050): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
02-23 01:18:27.217: E/AndroidRuntime(3050): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
02-23 01:18:27.217: E/AndroidRuntime(3050): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
02-23 01:18:27.217: E/AndroidRuntime(3050): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
02-23 01:18:27.217: E/AndroidRuntime(3050): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
02-23 01:18:27.217: E/AndroidRuntime(3050): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
02-23 01:18:27.217: E/AndroidRuntime(3050): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
02-23 01:18:27.217: E/AndroidRuntime(3050): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)
02-23 01:18:27.217: E/AndroidRuntime(3050): at com.example.project.DB.getModuleTime(DB.java:134)
02-23 01:18:27.217: E/AndroidRuntime(3050): at com.example.project.MainActivity.onResume(MainActivity.java:106)
02-23 01:18:27.217: E/AndroidRuntime(3050): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1257)
02-23 01:18:27.217: E/AndroidRuntime(3050): at android.app.Activity.performResume(Activity.java:6076)
02-23 01:18:27.217: E/AndroidRuntime(3050): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2978)
02-23 01:18:27.217: E/AndroidRuntime(3050): ... 11 more
答案 0 :(得分:2)
您的SQL有3个错误。
1)你需要把你的字符串常量放在引号中 - 'DIGITAL SECURITY'
2)你的where子句错了。它不是“WHERE x = 7 OR 10 OR 11”。它的“WHERE x = 7 OR x = 10 OR x = 11”。
3)下半场也是错的。你不能去“foo OR bar = 7”你需要去“foo = 7 OR bar = 7”