创建android sqlite表时出错。给出错误:缺少日期列

时间:2015-08-12 17:00:35

标签: android sqlite

在get-information方法中收到错误它会抛出一个错误,说错过列名称日期:请你告诉我这里我做错了什么。提前感谢您的帮助。谢谢

public class UserDbHandler extends SQLiteOpenHelper {
private static final String DATABASE_NAME="MEALINFO.DB";
private static final int DATABASE_VERSION=5;
private static final String CREATE_QUERY="CREATE TABLE "+ UserMeals.UserMealInfo.TABLE_NAME+"(id INTEGER, "+
        UserMeals.UserMealInfo.CAL_IN_TAKE+" TEXT, "+UserMeals.UserMealInfo.DATE+" TEXT, "+ UserMeals.UserMealInfo.MEAL_TYPE+" TEXT );";

public UserDbHandler(Context context){

    super(context,DATABASE_NAME,null,DATABASE_VERSION);
    Log.e("DATABASE OPERATION", "Database created / opened.....");

}



public Cursor getinformation(SQLiteDatabase db){
    Cursor cursor;
    String[] projections={UserMeals.UserMealInfo.CAL_IN_TAKE,UserMeals.UserMealInfo.DATE,UserMeals.UserMealInfo.MEAL_TYPE, };
    cursor= db.query(UserMeals.UserMealInfo.TABLE_NAME, projections, null, null, null, null, null);
    return cursor;
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

}

2 个答案:

答案 0 :(得分:1)

首先,我很想知道你怎么没有得到这样的表格错误"因为你永远不会在数据库上创建表。为此,重写onCreate(SQLiteDatabase db)方法,然后执行在DB上创建表所需的查询。

示例:

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

答案 1 :(得分:1)

正如Vlade087所说,你必须执行查询来创建从SQLiteOpenHelper覆盖onCreate方法的表。要在此表中添加新列,或在数据库模型中执行任何更改,您需要更改数据库版本(增加它)。它将调用onUpgrade方法,您可以在其中删除或更改此表。通常人们在onUpgrade中执行此操作:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + UserMeals.UserMealInfo.TABLE_NAME);

    // Create tables again
    onCreate(db);
}