Android - 添加了新的合同列;需要重置表格

时间:2017-07-10 06:02:02

标签: android sqlite

我最初创建了一个表,一切都很好。然后我不得不添加一个表字段。现在,当我写信给它时,我得到的错误是新列不存在。如何在内容提供商中重新创建表格?

public static final class InventoryEntry implements BaseColumns {

    public static final Uri CONTENT_URI = Uri.withAppendedPath(InventoryContract.BASE_CONTENT_URI, InventoryEntry.TABLE_NAME);
    public static final int STOCK_MIN = 0;
    public static final String TABLE_NAME = "products"; // Same as PATH

    public static final String CONTENT_LIST_TYPE =
            ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + TABLE_NAME;

    public static final String CONTENT_ITEM_TYPE =
            ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + TABLE_NAME;

    public static final String COLUMN_PRODUCT_ID = "_id";
    public static final String COLUMN_PRODUCT_NAME = "product_name";
    public static final String COLUMN_PRODUCT_DESC = "product_description";
    public static final String COLUMN_PRODUCT_IMG_PATH = "product_img_path";
    public static final String COLUMN_PRODUCT_STOCK = "product_stock";

    public static final String CREATE_TABLE = "CREATE TABLE " + InventoryEntry.TABLE_NAME + " ("
            + InventoryEntry.COLUMN_PRODUCT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + InventoryEntry.COLUMN_PRODUCT_NAME + " TEXT NOT NULL UNIQUE, "
            + InventoryEntry.COLUMN_PRODUCT_DESC + " TEXT NOT NULL, "
            + InventoryEntry.COLUMN_PRODUCT_IMG_PATH + " TEXT DEFAULT '', "
            + InventoryEntry.COLUMN_PRODUCT_STOCK + " INTEGER NOT NULL CHECK (" + InventoryEntry.COLUMN_PRODUCT_STOCK + " >= " + STOCK_MIN + "));";
}

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "products.db";
public static final int DATABASE_VERSION = 1;

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    // Execute the SQL statement
    db.execSQL(InventoryContract.InventoryEntry.CREATE_TABLE);
}

// Called when database is upgraded.
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // The database is still at version 1, so there's nothing to do be done here.
}

}

1 个答案:

答案 0 :(得分:0)

将DATABASE_VERSION更改为2或从移动设备中删除应用并重新安装