表“Tablename”没有名为“columnname”的列

时间:2015-06-12 13:47:15

标签: android mysql

我用这个创建一个表,而“KEY_IMAGE_PATH”是“image”

private class foodieItemDBOpenHelper extends SQLiteOpenHelper {
    private static final String DATABASE_CREATE = "create table "
            + DATABASE_TABLE + "(" + KEY_ID
            + " integer primary key autoincrement, " +KEY_IMAGE_PATH +"         text no null, "
            + KEY_RATING + " text, " + KEY_TITLE + " text);";

    public foodieItemDBOpenHelper(Context c, String dbname,
                            SQLiteDatabase.CursorFactory factory, int version) {
        super(c, dbname, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE);
    }

在尝试向数据库添加数据时会抛出此错误:

 E/SQLiteLog﹕ (1) table foodieItems has no column named image
06-12 13:40:34.469  12377-12377/mi.ur.de.foodieappstarterproject E/SQLiteDatabase﹕ Error inserting rating=3 title=null image=/storage/sdcard/Pictures/FoodieApp/IMG_20150612_134034.jpg
    android.database.sqlite.SQLiteException: table foodieItems has no column named image (code 1): , while compiling: INSERT INTO foodieItems(rating,title,image) VALUES (?,?,?)

我的数据库设置有什么问题吗?

编辑:插入查询

public long addFoodieItem(FoodieItem item) {
    ContentValues newFoodieValues = new ContentValues();


    newFoodieValues.put(KEY_IMAGE_PATH, item.getImagePath());
    newFoodieValues.put(KEY_RATING, 3);
    newFoodieValues.put(KEY_TITLE, item.getTitle());

    return db.insert(DATABASE_TABLE,null,  newFoodieValues);
}

2 个答案:

答案 0 :(得分:0)

在你的create table语句中," text no null"是无效的DDL。它应该是"文本不为空"如果你想让字段没有空值。

答案 1 :(得分:0)

我建议您将查询更改为:

private static final String DATABASE_CREATE = "CREATE TABLE "
        + DATABASE_TABLE + "(" + KEY_ID
        + " INTEGER primary key autoincrement, " +KEY_IMAGE_PATH +" TEXT NOT null, "
        + KEY_RATING + " TEXT, " + KEY_TITLE + " TEXT);";

必须是TEXT NOT null,这就是KEY_IMAGE_PATH中定义的字段无法创建的原因!

在此更改之后,您可能需要卸载应用程序,因为表的结构已更改!