如何更正此查询?

时间:2013-05-29 12:07:55

标签: java android sql database

我有一个查询,使用此col创建一个内连接2表:     单词表:wid,wname,wsyn,wlevel     wordchoice表:wid,ch1,ch2,ch3 但是当我想在下面使用这个查询时,在log cat中我收到错误!

words table : wid, wname, wsyn, wlevel
wordchoice table :wid, ch1, ch2, ch3

public Cursor getText2() {
SQLiteDatabase db = getReadableDatabase();
String mySQL1= "select w.wid,w.wname,w.wsyn,c.ch1,c.ch2,c.ch3"
                + " from words w INNER JOIN wordchoice c"
                +"  ON w.wid=c.wid"
                + " where wid=?"
                + " and  wlevel=?"
                ;

String[] args= {"7", "1"};      
Cursor c2 = db.rawQuery(mySQL1,args);
c2.moveToFirst();
return c2;
}   

问题是什么?

logcat:

FATAL EXCEPTION: main
 java.lang.RuntimeException: Unable to start activity ComponentInfo{ir.project.g5/ir.project.g5.test}: android.database.sqlite.SQLiteException: ambiguous column name: wid: , while compiling: select w.wid,w.wname,w.wsyn,c.ch1,c.ch2,c.ch3 from words w INNER JOIN wordchoice c  ON w.wid=c.wid where wid=? and  wlevel=?
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
at android.app.ActivityThread.access$600(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: ambiguous column name: wid: , while compiling: select w.wid,w.wname,w.wsyn,c.ch1,c.ch2,c.ch3 from words w INNER JOIN wordchoice c  ON w.wid=c.wid where wid=? and  wlevel=?
at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java)
at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java)
at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java)
at ir.project.g5.DBHelper.getText2(DBHelper.java:79)
at ir.project.g5.test.onCreate(test.java:141)
at android.app.Activity.performCreate(Activity.java)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
... 12 more

2 个答案:

答案 0 :(得分:6)

where wid = ?:由于两个表中都有相同的字段,因此不明确。您应该在前面加上c或w:where c.wid = ?

您添加到初始帖子的日志确认了歧义。

答案 1 :(得分:5)

这只是一个假设:

+ " where w.wid=?"
            + " and  w.wlevel=?"