我正在同时学习android
编程和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);
答案 0 :(得分:2)
从移动设备卸载您的应用,然后重新运行。 我希望如果代码没有问题,它会起作用。