public String getDatatwo() {
// TODO Auto-generated method stub
String arg = "lol";
String query = "select * from DATABASE_TABLE where title=\""+ arg +"\"";
Cursor c = ourDatabase.rawQuery(query, null);
int iName = c.getColumnIndex(KEY_NAME);
String resultsearch = "";
resultsearch = resultsearch + c.getString(iName);
return resultsearch;
}
基本上,它在 DATABASE_TABLE 中搜索 arg("lol") 然后将其作为结果搜索返回,稍后会显示。我试图让它在 KEY_NAME 行中搜索整个数据库并返回包含名称“lol”的 ALL。
如果可能的话,我还希望能够输入一个编辑 arg 的 TextEdit,由于类扩展,我想我无法操作 arg。如果有人愿意提供帮助,如果有人可以看一下,我可以发送所有代码。
但是我得到一个错误。
02-18 12:16:02.201: D/gralloc_goldfish(645): Emulator without GPU emulation detected.
02-18 12:16:24.111: E/SQLiteLog(645): (1) no such table: DATABASE_TABLE
02-18 12:16:24.111: D/AndroidRuntime(645): Shutting down VM
02-18 12:16:24.111: W/dalvikvm(645): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
02-18 12:16:24.151: E/AndroidRuntime(645): FATAL EXCEPTION: main
02-18 12:16:24.151: E/AndroidRuntime(645): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sql/com.example.sql.SQLView}: android.database.sqlite.SQLiteException: no such table: DATABASE_TABLE (code 1): , while compiling: select * from DATABASE_TABLE where title="lol"
02-18 12:16:24.151: E/AndroidRuntime(645): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
02-18 12:16:24.151: E/AndroidRuntime(645): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-18 12:16:24.151: E/AndroidRuntime(645): at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-18 12:16:24.151: E/AndroidRuntime(645): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-18 12:16:24.151: E/AndroidRuntime(645): at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 12:16:24.151: E/AndroidRuntime(645): at android.os.Looper.loop(Looper.java:137)
02-18 12:16:24.151: E/AndroidRuntime(645): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-18 12:16:24.151: E/AndroidRuntime(645): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 12:16:24.151: E/AndroidRuntime(645): at java.lang.reflect.Method.invoke(Method.java:511)
02-18 12:16:24.151: E/AndroidRuntime(645): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-18 12:16:24.151: E/AndroidRuntime(645): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-18 12:16:24.151: E/AndroidRuntime(645): at dalvik.system.NativeStart.main(Native Method)
02-18 12:16:24.151: E/AndroidRuntime(645): Caused by: android.database.sqlite.SQLiteException: no such table: DATABASE_TABLE (code 1): , while compiling: select * from DATABASE_TABLE where title="lol"
02-18 12:16:24.151: E/AndroidRuntime(645): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-18 12:16:24.151: E/AndroidRuntime(645): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
02-18 12:16:24.151: E/AndroidRuntime(645): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
02-18 12:16:24.151: E/AndroidRuntime(645): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
02-18 12:16:24.151: E/AndroidRuntime(645): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
02-18 12:16:24.151: E/AndroidRuntime(645): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
02-18 12:16:24.151: E/AndroidRuntime(645): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
02-18 12:16:24.151: E/AndroidRuntime(645): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
02-18 12:16:24.151: E/AndroidRuntime(645): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
02-18 12:16:24.151: E/AndroidRuntime(645): at com.example.sql.DbMain.getDatatwo(DbMain.java:94)
02-18 12:16:24.151: E/AndroidRuntime(645): at com.example.sql.SQLView.onCreate(SQLView.java:19)
02-18 12:16:24.151: E/AndroidRuntime(645): at android.app.Activity.performCreate(Activity.java:5008)
02-18 12:16:24.151: E/AndroidRuntime(645): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-18 12:16:24.151: E/AndroidRuntime(645): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
02-18 12:16:24.151: E/AndroidRuntime(645): ... 11 more
02-18 12:16:24.381: D/dalvikvm(645): GC_CONCURRENT freed 196K, 9% free 7469K/8199K, paused 19ms+33ms, total 192ms
02-18 12:16:26.741: I/Process(645): Sending signal. PID: 645 SIG: 9
修改后的代码,仍然有错误。
public String getDatatwo() {
// TODO Auto-generated method stub
String arg = "lol";
Cursor c = ourDatabase.rawQuery("select * from " + DATABASE_TABLE + " where title=?"+ arg +"\"", new String[] { arg });
int iName = c.getColumnIndex(KEY_NAME);
String resulttwo = "";
resulttwo = resulttwo + c.getString(iName);
return resulttwo;
}