2012-01-04 54 views
0

我有一个数据库与外键我使用SELECT * FROM语句,但当我尝试查看数据库时不断收到错误。错误SELECT * FROM语句android SQLite数据库

SELECT * FROM语句

private final String MY_QUERY = "SELECT * FROM wwTable mLat INNER JOIN wwTimeTable _id ON wwTable._id=wwTimeTable.mName WHERE Table._id=wwTimetabel.mForeign"; 

我的钥匙

public static final String KEY_ROWID = "_id"; 
public static final String KEY_ROWIDLL = "_id"; 
public static final String KEY_ROWIDTIME = "_id"; 
public static final String KEY_NAME = "mName"; 
public static final String KEY_LAT = "mLat"; 
public static final String KEY_LON = "mLon"; 
public static final String KEY_HOUR = "mHour"; 
public static final String KEY_MINUT = "mMinut"; 
public static final String KEY_DAY = "mDay"; 
public static final String KEY_MONTH = "mMonth"; 
public static final String KEY_YEAR = "mYear"; 
public static final String KEY_FOREIGN = "mForeign"; 

的onCreate EXECSQL

@Override 
     public void onCreate(SQLiteDatabase db) { 
      // TODO Auto-generated method stub 
      db.execSQL("CREATE TABLE " + WW_TABLE + 
        " (" + KEY_ROWIDLL + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
        KEY_LAT + " TEXT NOT NULL, " + 
        KEY_LON + " TEXT NOT NULL);"); 
      db.execSQL("CREATE TABLE " + WW_TIMETABLE + 
        " (" + KEY_ROWIDTIME + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
        KEY_HOUR + " TEXT NOT NULL, " + 
        KEY_FOREIGN + " INTEGER," + 
        " FOREIGN KEY ("+KEY_FOREIGN+") REFERENCES "+WW_TABLE+" ("+KEY_ROWIDLL+") ON DELETE CASCADE);"); 

CURSOR RAWQUERY

public Cursor fetchTime() { 
     // TODO Auto-generated method stub 
     //return ourdb.query(WW_TIMETABLE, new String[] {KEY_ROWIDTIME, KEY_HOUR, KEY_FOREIGN}, null, null, null, null, null); 
     return ourdb.rawQuery(MY_QUERY, new String[]{String.valueOf(KEY_FOREIGN)}); 

    } 

我怎么查看所有IT内部的LISTACTIVITY

private void fillData() { 
    // Get all of the rows from the database and create the item list 

    mTimeNotesCursor = mDbHelper.fetchTime(); 
    startManagingCursor(mTimeNotesCursor); 
    // startManagingCursor(mNotesCursor); 

    // Create an array to specify the fields we want to display in the list (only TITLE) 
    String[] from = new String[]{WWDatabase.KEY_HOUR,WWDatabase.KEY_FOREIGN}; 

    //String[] fromTime = new String[]{WWDatabase.KEY_HOUR}; 


    // and an array of the fields we want to bind those fields to (in this case just text1) 
    int[] to = new int[]{R.id.textView2, R.id.textView3}; 

    //int[] toTime = new int[]{R.id.textView4}; 

    // Now create a simple cursor adapter and set it to display 
    SimpleCursorAdapter notes = 
     new SimpleCursorAdapter(this, R.layout.time_text_row, mTimeNotesCursor, from, to); 

    setListAdapter(notes); 
    // SimpleCursorAdapter notesTime = 

     //new SimpleCursorAdapter(this, R.layout.time_text_row, mTimeNotesCursor, fromTime, toTime); 
     //setListAdapter(notesTime); 
} 

堆栈跟踪

01-04 09:47:30.260: E/AndroidRuntime(273): FATAL EXCEPTION: main 
01-04 09:47:30.260: E/AndroidRuntime(273): java.lang.RuntimeException: Unable to start activity ComponentInfo{www.freshapp.com.wherewhen.html/www.freshapp.com.wherewhen.html.TimeViewer}: android.database.sqlite.SQLiteException: near "Table": syntax error: , while compiling: SELECT * FROM wwTable mLat INNER JOIN wwTimeTable _id ON wwTable._id=wwTimeTable.mName WHERE Table._id=wwTimetabel.mForeign 
01-04 09:47:30.260: E/AndroidRuntime(273): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
01-04 09:47:30.260: E/AndroidRuntime(273): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
01-04 09:47:30.260: E/AndroidRuntime(273): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
01-04 09:47:30.260: E/AndroidRuntime(273): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
01-04 09:47:30.260: E/AndroidRuntime(273): at android.os.Handler.dispatchMessage(Handler.java:99) 
01-04 09:47:30.260: E/AndroidRuntime(273): at android.os.Looper.loop(Looper.java:123) 
01-04 09:47:30.260: E/AndroidRuntime(273): at android.app.ActivityThread.main(ActivityThread.java:4627) 
01-04 09:47:30.260: E/AndroidRuntime(273): at java.lang.reflect.Method.invokeNative(Native Method) 
01-04 09:47:30.260: E/AndroidRuntime(273): at java.lang.reflect.Method.invoke(Method.java:521) 
01-04 09:47:30.260: E/AndroidRuntime(273): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
01-04 09:47:30.260: E/AndroidRuntime(273): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
01-04 09:47:30.260: E/AndroidRuntime(273): at dalvik.system.NativeStart.main(Native Method) 
01-04 09:47:30.260: E/AndroidRuntime(273): Caused by: android.database.sqlite.SQLiteException: near "Table": syntax error: , while compiling: SELECT * FROM wwTable mLat INNER JOIN wwTimeTable _id ON wwTable._id=wwTimeTable.mName WHERE Table._id=wwTimetabel.mForeign 
01-04 09:47:30.260: E/AndroidRuntime(273): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method) 
01-04 09:47:30.260: E/AndroidRuntime(273): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91) 
01-04 09:47:30.260: E/AndroidRuntime(273): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64) 
01-04 09:47:30.260: E/AndroidRuntime(273): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80) 
01-04 09:47:30.260: E/AndroidRuntime(273): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46) 
01-04 09:47:30.260: E/AndroidRuntime(273): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42) 
01-04 09:47:30.260: E/AndroidRuntime(273): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345) 
01-04 09:47:30.260: E/AndroidRuntime(273): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1315) 
01-04 09:47:30.260: E/AndroidRuntime(273): at www.freshapp.com.wherewhen.html.WWDatabase.fetchTime(WWDatabase.java:143) 
01-04 09:47:30.260: E/AndroidRuntime(273): at www.freshapp.com.wherewhen.html.TimeViewer.fillData(TimeViewer.java:57) 
01-04 09:47:30.260: E/AndroidRuntime(273): at www.freshapp.com.wherewhen.html.TimeViewer.onCreate(TimeViewer.java:41) 
01-04 09:47:30.260: E/AndroidRuntime(273): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
01-04 09:47:30.260: E/AndroidRuntime(273): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
01-04 09:47:30.260: E/AndroidRuntime(273): ... 11 more 

我很想有这方面的帮助。我一直在寻找一段时间。

+2

什么是错误您收到?你可以添加堆栈跟踪吗? – elijah 2012-01-04 18:07:09

+0

@elijah,我添加了堆栈跟踪。 – Christian 2012-01-04 18:24:57

+1

StevieG和dmon都发现您的查询存在问题;您的堆栈追踪正在抱怨StevieG所识别的一个。 – elijah 2012-01-04 18:27:11

回答

2

你的where子句中引用所谓Table一个对象,你有没有在你的FROM子句了。此外,还有没有,如果你不使用别名,别名你的表多点..

我猜应该是:

private final String MY_QUERY = "SELECT * FROM wwTable INNER JOIN wwTimeTable ON wwTable._id=wwTimeTable.mName WHERE wwTable._id=wwTimetable.mForeign"; 
+0

它已经停止给我我得到的错误。谢谢。但是,我得到'无法启动'错误返回ourdb.rawQuery(MY_QUERY,新的字符串[] {String.valueOf(KEY_FOREIGN)});' – Christian 2012-01-04 18:37:49

+0

不知道为什么你想要传递一个参数到查询没有任何定义。尝试:'返回ourdb.rawQuery(MY_QUERY,null);' – StevieG 2012-01-04 18:50:55

+0

我没有尝试null没有任何东西出现在ListActivity中 – Christian 2012-01-04 18:54:24

1

在from语句中删除wwTable之后的mLat

正确的语法为:select * from table inner join other_table ...