我可以从这样的2个表中查询吗?
public Cursor getSen_List(long id_thsen ) {
String strTmp = "select "
+SENS_TABLE+"."+KEY_SEN_ID+","
+SENS_TABLE+"."+KEY_SEN_ID_TH_SEN+","
+SENS_TABLE+"."+KEY_SEN_ID_LANG+","
+SENS_TABLE+"."+KEY_SEN+","
+SENS_TABLE+"."+KEY_SEN_READING+","
+LANGS_TABLE+"."+KEY_LANG
+" from "+SENS_TABLE+ ","+LANGS_TABLE
+" where "+SENS_TABLE+"."+KEY_SEN_ID_TH_SEN + "=" + id_thsen+ " and "
+SENS_TABLE+"."+KEY_LANG+"= " +("select"
+LANGS_TABLE+"."+KEY_LANG
+"from"+LANGS_TABLE
+"where"+LANGS_TABLE+"."+KEY_ACT+"=1");
return db.rawQuery(strTmp,null);}
这是我的logcat
02-11 02:45:02.814: ERROR/AndroidRuntime(625): FATAL EXCEPTION: main
02-11 02:45:02.814: ERROR/AndroidRuntime(625): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.proj/com.android.proj.Sens_Show}: android.database.sqlite.SQLiteException: no such column: Sentences.lang: , while compiling: select Sentences._id,Sentences.id_thsen,Sentences.id_lang,Sentences.sen,Sentences.reading,Languages.lang from Sentences,Languages where Sentences.id_thsen=1 and Sentences.lang= (select Languages.lang from Languages where Languages.active_status=1)
02-11 02:45:02.814: ERROR/AndroidRuntime(625): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-11 02:45:02.814: ERROR/AndroidRuntime(625): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-11 02:45:02.814: ERROR/AndroidRuntime(625): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-11 02:45:02.814: ERROR/AndroidRuntime(625): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-11 02:45:02.814: ERROR/AndroidRuntime(625): at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 02:45:02.814: ERROR/AndroidRuntime(625): at android.os.Looper.loop(Looper.java:123)
02-11 02:45:02.814: ERROR/AndroidRuntime(625): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-11 02:45:02.814: ERROR/AndroidRuntime(625): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 02:45:02.814: ERROR/AndroidRuntime(625): at java.lang.reflect.Method.invoke(Method.java:521)
02-11 02:45:02.814: ERROR/AndroidRuntime(625): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-11 02:45:02.814: ERROR/AndroidRuntime(625): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-11 02:45:02.814: ERROR/AndroidRuntime(625): at dalvik.system.NativeStart.main(Native Method)
02-11 02:45:02.814: ERROR/AndroidRuntime(625): Caused by: android.database.sqlite.SQLiteException: no such column: Sentences.lang: , while compiling: select Sentences._id,Sentences.id_thsen,Sentences.id_lang,Sentences.sen,Sentences.reading,Languages.lang from Sentences,Languages where Sentences.id_thsen=1 and Sentences.lang= (select Languages.lang from Languages where Languages.active_status=1)
02-11 02:45:02.814: ERROR/AndroidRuntime(625): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
02-11 02:45:02.814: ERROR/AndroidRuntime(625): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
02-11 02:45:02.814: ERROR/AndroidRuntime(625): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
02-11 02:45:02.814: ERROR/AndroidRuntime(625): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
02-11 02:45:02.814: ERROR/AndroidRuntime(625): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46)
02-11 02:45:02.814: ERROR/AndroidRuntime(625): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
02-11 02:45:02.814: ERROR/AndroidRuntime(625): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
02-11 02:45:02.814: ERROR/AndroidRuntime(625): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1315)
02-11 02:45:02.814: ERROR/AndroidRuntime(625): at com.android.proj.DBAdapter.getSen_List(DBAdapter.java:881)
02-11 02:45:02.814: ERROR/AndroidRuntime(625): at com.android.proj.Sens_Show.listData(Sens_Show.java:89)
02-11 02:45:02.814: ERROR/AndroidRuntime(625): at com.android.proj.Sens_Show.onCreate(Sens_Show.java:64)
02-11 02:45:02.814: ERROR/AndroidRuntime(625): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-11 02:45:02.814: ERROR/AndroidRuntime(625): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-11 02:45:02.814: ERROR/AndroidRuntime(625): ... 11 more
句子表
private static final String CREATE_TABLE_7 =
" create table " + SENS_TABLE +
" (_id integer primary key autoincrement," +
" id_thsen integer REFERENCES Thai_sentences(_id)," +
" id_lang integer REFERENCES Languages(_id)," +
" sen text not null," +
" reading text not null);";
语言表`
private static final String CREATE_TABLE_3 =
" create table " + LANGS_TABLE +
" (_id integer primary key autoincrement," +
" lang text not null," +
" active_status integer not null);";
正确的语言,但发生5次像语言数量
public Cursor getSen_List(long id_thsen ) {
String strTmp = "select "
+SENS_TABLE+"."+KEY_SEN_ID+","
+SENS_TABLE+"."+KEY_SEN_ID_TH_SEN+","
+SENS_TABLE+"."+KEY_SEN_ID_LANG+","
+SENS_TABLE+"."+KEY_SEN+","
+SENS_TABLE+"."+KEY_SEN_READING+","
+LANGS_TABLE+"."+KEY_LANG
+" from "+SENS_TABLE+ ","+LANGS_TABLE
+" where "+SENS_TABLE+"."+KEY_SEN_ID_TH_SEN + "=" + id_thsen+ " and "
+SENS_TABLE+"."+KEY_LANG_ID+"= " +("(select "
+LANGS_TABLE+"."+KEY_LANG_ID
+" from "+LANGS_TABLE
+" where "+LANGS_TABLE+"."+KEY_ACT+"=1)");
return db.rawQuery(strTmp,null);}
答案 0 :(得分:3)
您的子查询中需要select
,from
和where
周围的空格。您可能也应该将该子查询括在()
中。
public Cursor getSen_List(long id_thsen ) {
String strTmp = "select "
+SENS_TABLE+"."+KEY_SEN_ID+","
+SENS_TABLE+"."+KEY_SEN_ID_TH_SEN+","
+SENS_TABLE+"."+KEY_SEN_ID_LANG+","
+SENS_TABLE+"."+KEY_SEN+","
+SENS_TABLE+"."+KEY_SEN_READING+","
+LANGS_TABLE+"."+KEY_LANG
+" from "+SENS_TABLE+ ","+LANGS_TABLE
+" where "+SENS_TABLE+"."+KEY_SEN_ID_TH_SEN + "=" + id_thsen+ " and "
+SENS_TABLE+"."+KEY_LANG+"= " +("(select "
+LANGS_TABLE+"."+KEY_LANG
+" from "+LANGS_TABLE
+" where "+LANGS_TABLE+"."+KEY_ACT+"=1)");
return db.rawQuery(strTmp,null);}
修改强>
现在您已经发布了可以看到此行的日志
Caused by: android.database.sqlite.SQLiteException: no such column: Sentences.lang:
指向错误。 Sentences表中没有列lang