【问题标题】:SQLite no such column errorSQLite没有这样的列错误
【发布时间】: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'

标签: android sqlite


【解决方案1】:

你需要用单引号包裹房子

db.rawQuery("select * from category where category = 'house'"  , null);

【讨论】:

  • 当类别名称包含单引号时会爆炸;更好的使用参数。
【解决方案2】:

在我的情况下,错误是在我在表插入和更新时写的触发器中

【讨论】:

    猜你喜欢
    • 2017-02-09
    • 2015-09-07
    • 1970-01-01
    • 2022-01-19
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-29
    相关资源
    最近更新 更多