我正在编写一个Android应用程序,我有一个DB Helper类来处理与SQLite DB相关的任何事情。该类的名称是" ResultsDbAdapter"。以下是关于DB的一些信息:
private static final String DATABASE_NAME = "PROJECT_RESULTS";
private static final String DATABASE_TABLE = "projectResults";
private static final int DATABASE_VERSION = 1;
//Fields(Columns) for Database
public static final String[] PROJECT_RESULTS_FIELDS = new String[] {
KEY_ROWID,
"title",
"interval",
"start",
"end",
"duration",
"totalHits",
"itemKeys",
"keyHits"
};
在ResultsDbAdapter类中,是一种通过搜索"标题"来查询数据库以查找特定条目的方法。柱。
public Cursor fetchByTitle(String pTitle) throws SQLException {
Cursor mCursor = mDb.query(DATABASE_TABLE, PROJECT_RESULTS_FIELDS, "title" + "=" + pTitle, null,
null, null, null, null);
return mCursor;
}
" fetchByTitle"然后在主活动中使用以下代码片段调用方法:
final ResultsDbAdapter db = new ResultsDbAdapter (this);
db.open(); //Open and Close methods are also written in the "ResultsDbAdapter" class
final Cursor cursor = db.fetchByTitle(mTitle); //mTitle is the title of the record needed
if (cursor.moveToFirst()){
// do something
} else {
//do something else
}
db.close();
首次在模拟器上运行时,应用程序会导致错误,我认为是因为没有条目。因此没有光标返回导致错误。以下是logcat的错误:
03-07 21:53:51.230: E/SQLiteLog(1158): (1) near "Title": syntax error
03-07 21:53:51.310: E/AndroidRuntime(1158): FATAL EXCEPTION: main
03-07 21:53:51.310: E/AndroidRuntime(1158): Process: com.jevans.surveybud, PID: 1158
03-07 21:53:51.310: E/AndroidRuntime(1158): android.database.sqlite.SQLiteException: near "Title": syntax error (code 1): , while compiling: SELECT _id, title, interval, start, end, duration, totalHits, itemKeys, keyHits FROM projectResults WHERE title=My Title
03-07 21:53:51.310: E/AndroidRuntime(1158): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
03-07 21:53:51.310: E/AndroidRuntime(1158): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
03-07 21:53:51.310: E/AndroidRuntime(1158): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
03-07 21:53:51.310: E/AndroidRuntime(1158): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
03-07 21:53:51.310: E/AndroidRuntime(1158): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
03-07 21:53:51.310: E/AndroidRuntime(1158): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
03-07 21:53:51.310: E/AndroidRuntime(1158): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
03-07 21:53:51.310: E/AndroidRuntime(1158): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
03-07 21:53:51.310: E/AndroidRuntime(1158): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
03-07 21:53:51.310: E/AndroidRuntime(1158): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
03-07 21:53:51.310: E/AndroidRuntime(1158): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1238)
03-07 21:53:51.310: E/AndroidRuntime(1158): at com.jevans.surveybud.ResultsDbAdapter.fetchByTitle(ResultsDbAdapter.java:141)
03-07 21:53:51.310: E/AndroidRuntime(1158): at com.jevans.surveybud.ResultsActivity.saveDb(ResultsActivity.java:163)
03-07 21:53:51.310: E/AndroidRuntime(1158): at com.jevans.surveybud.ResultsActivity$1.onClick(ResultsActivity.java:138)
03-07 21:53:51.310: E/AndroidRuntime(1158): at android.view.View.performClick(View.java:4438)
03-07 21:53:51.310: E/AndroidRuntime(1158): at android.view.View$PerformClick.run(View.java:18422)
03-07 21:53:51.310: E/AndroidRuntime(1158): at android.os.Handler.handleCallback(Handler.java:733)
03-07 21:53:51.310: E/AndroidRuntime(1158): at android.os.Handler.dispatchMessage(Handler.java:95)
03-07 21:53:51.310: E/AndroidRuntime(1158): at android.os.Looper.loop(Looper.java:136)
03-07 21:53:51.310: E/AndroidRuntime(1158): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-07 21:53:51.310: E/AndroidRuntime(1158): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 21:53:51.310: E/AndroidRuntime(1158): at java.lang.reflect.Method.invoke(Method.java:515)
03-07 21:53:51.310: E/AndroidRuntime(1158): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-07 21:53:51.310: E/AndroidRuntime(1158): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-07 21:53:51.310: E/AndroidRuntime(1158): at dalvik.system.NativeStart.main(Native Method)
我为这个看似根本的问题道歉,因为我是Android编程的新手。但是,我已经坚持了很长时间。有人可以帮忙解决这个错误吗?
答案 0 :(得分:2)
错误堆栈跟踪的这一部分表示生成的sql语句(SELECT _id, title, interval, start, end, duration, totalHits, itemKeys, keyHits FROM projectResults WHERE title=My Title
)导致SQLiteException
03-07 21:53:51.230: E/SQLiteLog(1158): (1) near "Title": syntax error
03-07 21:53:51.310: E/AndroidRuntime(1158): FATAL EXCEPTION: main
03-07 21:53:51.310: E/AndroidRuntime(1158): Process: com.jevans.surveybud, PID: 1158
03-07 21:53:51.310: E/AndroidRuntime(1158): android.database.sqlite.SQLiteException:
要修复尝试利用query()
方法参数的强大功能,以便正确绑定以进行函数sql调用:
selection
("title = ?"
)selectionArgs
(new String[]{ pTitle}
)总之它看起来像:
Cursor mCursor = mDb.query(DATABASE_TABLE,
PROJECT_RESULTS_FIELDS,
/* selection */ "title = ?",
/* selectionArgs */ new String[]{ pTitle},
null, null, null);
这应该解决你所拥有的SQLiteException
。
HTHS