我正在學習android
編程和sqlite
在同一時間。我創建像一個表:SQLite沒有這樣的列,即使一切拼寫正確,並且有正確的間距
private static final String DATABASE_CREATE = "CREATE TABLE " + BOOK_TABLE + " ("
+ BookContract._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ BookContract.TITLE + " TEXT NOT NULL, "
+ BookContract.AUTHORS + " TEXT NOT NULL, "
+ BookContract.ISBN + " TEXT NOT NULL, "
+ BookContract.PRICE + " TEXT NOT NULL);";
在我的功能是獲取所有書籍在數據庫中,我的查詢是:
String query = "SELECT " +
BOOK_TABLE + "." + BookContract._ID + ", " +
BookContract.TITLE + ", " +
BookContract.PRICE + ", " +
BookContract.ISBN + ", " +
"GROUP_CONCAT(" + AuthorContract.NAME + ", ',') as " + BookContract.AUTHORS +
" FROM " + BOOK_TABLE + " LEFT OUTER JOIN " + AUTHOR_TABLE + " ON " +
BOOK_TABLE + "." + BookContract._ID + "=" +
AUTHOR_TABLE + "." + AuthorContract.BOOK_FOREIGN_KEY +
" GROUP BY " + BOOK_TABLE + "." + BookContract._ID + ", " +
BookContract.TITLE + ", " + BookContract.PRICE + ", " + BookContract.ISBN;
這裏是字符串實際上看起來像在創建表:
CREATE TABLE bookTable (_id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, authors TEXT NOT NULL, isbn TEXT NOT NULL, price TEXT NOT NULL);
和查詢:
SELECT bookTable._id, title, price, isbn, GROUP_CONCAT(name, ',') as authors FROM bookTable LEFT OUTER JOIN authorTable ON bookTable._id = authorTable.book_fk GROUP BY bookTable._id, title, price, isbn
我發誓我瘋了,因爲類似的stackOverflow
職位要求類似的問題有輕微的拼寫錯誤或缺失的空間,但我的網上似乎工作正常sql simulator
!
當我做一個database.rawQuery(query, null)
,錯誤說,there is no such column: price appears in the log
。
編輯:
有是有在評論中提到失蹤的名字列另一位作者表,在這裏是爲了清晰:
String AUTHOR_TABLE_CREATE = "CREATE TABLE " + AUTHOR_TABLE + " ("
+ AuthorContract._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ AuthorContract.BOOK_FOREIGN_KEY + " INTEGER NOT NULL, "
+ AuthorContract.NAME + " TEXT NOT NULL, "
+ "FOREIGN KEY ("+ AuthorContract.BOOK_FOREIGN_KEY+") " +
"REFERENCES "+BOOK_TABLE+"("+BookContract._ID+") ON DELETE CASCADE);";
這給字符串:
CREATE TABLE authorTable (_id INTEGER PRIMARY KEY AUTOINCREMENT, book_fk INTEGER NOT NULL, name TEXT NOT NULL, FOREIGN KEY (book_fk) REFERENCES bookTable(_id) ON DELETE CASCADE);
你是否首先創建數據庫比執行上面的查詢..我也陷入了同樣的問題,以前...我還沒有創建數據庫,並啓動查詢...先檢查它,先... ... 。您正在創建數據庫,並執行查詢 –
'GROUP_CONCAT(name,',')'?沒有名稱列 –
@ cricket_007似乎來自表'authorTable' –