引起:android.database.sqlite.SQLiteException:没有这样的列:

时间:2016-02-20 18:57:58

标签: android sqlite android-sqlite create-table

我的应用程序有问题,因为andorid工作室说列不会退出。但我宣布了专栏,这是代码:

public class DatabaseCookBookHelper extends SQLiteOpenHelper {
/* Table Name */
public static final String TABLE_NAME = "COOKBOOK";

// Table columns
public static final String _ID = "_id";
public static final String RECIPE_SUBJECT = "subject";
public static final String RECIPE_DESC = "description";
public static final String RECIPE_INGREDIENTS = "ingredients";
public static final String RECIPE_TIME = "time";
public static final String RECIPE_RATING = "rating";

// Database Information
static final String DB_NAME = "COOKBOOK.DB";

// database version
static final int DB_VERSION = 1;

// Creating table query
private static final String CREATE_TABLE = "create table " + TABLE_NAME + "(" + _ID
        + " INTEGER PRIMARY KEY AUTOINCREMENT, " + RECIPE_SUBJECT + " TEXT NOT NULL, " + RECIPE_DESC + " TEXT NOT NULL, "
        + RECIPE_INGREDIENTS + " TEXT NOT NULL, " + RECIPE_TIME + " INT NOT NULL, " + RECIPE_RATING + " INT);";

public DatabaseCookBookHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
}

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

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}

问题出在这里。当我执行查询时,问题出现。该列对我来说存在,但是当我尝试执行它时,应用程序将会中断:

public class DBCookbookManager {

private DatabaseCookBookHelper dbHelper;

private Context context;

private SQLiteDatabase database;

public DBCookbookManager(Context c) {
    context = c;
}

public DBCookbookManager open() throws SQLException {
    dbHelper = new DatabaseCookBookHelper(context);
    database = dbHelper.getWritableDatabase();
    return this;
}

public void close() {
    dbHelper.close();
}

public void insert(String name, String desc, String ingredients, String time, String rating) {
    ContentValues contentValue = new ContentValues();
    contentValue.put(DatabaseCookBookHelper.RECIPE_SUBJECT, name);
    contentValue.put(DatabaseCookBookHelper.RECIPE_DESC, desc);
    contentValue.put(DatabaseCookBookHelper.RECIPE_INGREDIENTS, ingredients);
    contentValue.put(DatabaseCookBookHelper.RECIPE_TIME, time);
    contentValue.put(DatabaseCookBookHelper.RECIPE_RATING, rating);
    database.insert(DatabaseCookBookHelper.TABLE_NAME, null, contentValue);
}

public Cursor fetch() {
    String[] columns = new String[] { DatabaseCookBookHelper._ID, DatabaseCookBookHelper.RECIPE_SUBJECT,
            DatabaseCookBookHelper.RECIPE_DESC, DatabaseCookBookHelper.RECIPE_INGREDIENTS,
            DatabaseCookBookHelper.RECIPE_TIME, DatabaseCookBookHelper.RECIPE_RATING};
    Cursor cursor = database.query(DatabaseCookBookHelper.TABLE_NAME, columns, null, null, null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
    }
    return cursor;
}

public int update(long _id, String name, String desc, String ingredients, String time, String rating) {
    ContentValues contentValues = new ContentValues();
    contentValues.put(DatabaseCookBookHelper.RECIPE_SUBJECT, name);
    contentValues.put(DatabaseCookBookHelper.RECIPE_DESC, desc);
    contentValues.put(DatabaseCookBookHelper.RECIPE_INGREDIENTS, ingredients);
    contentValues.put(DatabaseCookBookHelper.RECIPE_TIME, time);
    contentValues.put(DatabaseCookBookHelper.RECIPE_RATING, rating);
    int i = database.update(DatabaseCookBookHelper.TABLE_NAME, contentValues, DatabaseCookBookHelper._ID + " = " + _id, null);
    return i;
}

public void delete(long _id) {
    database.delete(DatabaseCookBookHelper.TABLE_NAME, DatabaseCookBookHelper._ID + "=" + _id, null);
}

我不知道问题出在哪里。我希望在你的帮助下,非常感谢!

0 个答案:

没有答案