【发布时间】:2015-05-14 18:09:26
【问题描述】:
您好,谁能帮我解决 android sqlite 中的以下错误?真的很感激!
原因:android.database.sqlite.SQLiteException:没有这样的列:House(代码1):,编译时:select * from category where category =House
下面是我在表格中插入“House”的部分代码
public void onCreate(SQLiteDatabase db) {
String CREATE_CATEGORY_TABLE = "CREATE TABLE category( " +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"category TEXT UNIQUE)";
db.execSQL(CREATE_CATEGORY_TABLE);
}
public void addCategory(String name){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("category", name);
db.insert(CATEGORY_TABLE, // table
null, //nullColumnHack
cv); // key/value -> keys = column names/ values = column values
db.close();}
public List getCategory(){
List<String> list=new LinkedList();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor =
db.rawQuery("select * from category where category =house" , null);
// 3. if we got results get the first one
if (cursor != null)
cursor.moveToFirst();
do {
String s = (cursor.getString(1));
list.add(s);
}while (cursor.moveToNext());
return list;
}
【问题讨论】:
-
正如他在回答中所提到的,
house必须用单引号括起来,因为它是一个字符串值。 -
select * from category where category = 'house'