删除表时的SqliteException(main.table)

时间:2015-12-29 06:06:27

标签: android sqlite

我已经在我的应用程序中使用了SQLite一段时间了。我没有遇到任何问题,但这是我的第一次升级。我增加了我的数据库版本,并实现了onUpgrade:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion < 2) {
        db.execSQL(CREATE_TABLE_LOCKED);
        Log.d(LOG_TAG, "Created table LOCKED_BUNCH_MEMBER");
        db.execSQL(CREATE_TABLE_HIDDEN);
        Log.d(LOG_TAG, "Created table HIDDEN_BUNCH_MEMBER");
    }
}

一切似乎都很好!但是在注销时,我会丢弃所有表格,当我遇到一个疯狂的奇怪错误时,那就是:

12-29 00:55:10.365: E/AndroidRuntime(1020): android.database.sqlite.SQLiteException: no such table: main.bunch (code 1): , while compiling: DROP TABLE IF EXISTS buddy

这到底是什么?我没有一个名为main的架构,我从来没有见过这个。这是LAST drop table语句,但它失败了。有谁知道为什么?

我做错了升级吗?

public static LBDatabaseHelper getInstance(Context context) {
    if (mInstance == null) {
        mInstance = new LBDatabaseHelper(context.getApplicationContext());
    }
    return mInstance;
}

private LBDatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.mContext = context;
}

@Override
public void onOpen(SQLiteDatabase db) {
    db.execSQL("PRAGMA foreign_keys=ON");
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("PRAGMA foreign_keys = ON");
    db.execSQL(CREATE_TABLE_BUDDY);
    db.execSQL(CREATE_TABLE_BUNCH);
    db.execSQL(CREATE_TABLE_BCAST_BUNCH);
    db.execSQL(CREATE_TABLE_BUNCH_MEMBER);
    db.execSQL(CREATE_TABLE_BCAST_BUNCH_MEMBER);
    db.execSQL(CREATE_TABLE_LOCKED_BUNCH_MEMBER);
    db.execSQL(CREATE_TABLE_HIDDEN_BUNCH_MEMBER);
    db.execSQL(CREATE_TABLE_THREAD);
    db.execSQL(CREATE_TABLE_THREAD_MEMBER);
    db.execSQL(CREATE_TABLE_THREAD_MESSAGE);
    db.execSQL(CREATE_TABLE_BUDDY_REQUEST);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion < 2) {
        db.execSQL(CREATE_TABLE_LOCKED_BUNCH_MEMBER);
        Log.d(LOG_TAG, "Created table LOCKED_BUNCH_MEMBER");
        db.execSQL(CREATE_TABLE_HIDDEN_BUNCH_MEMBER);
        Log.d(LOG_TAG, "Created table HIDDEN_BUNCH_MEMBER");
    }
}

public void dropAllTables() {
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL("DROP TABLE IF EXISTS " + THREAD_MESSAGE_TABLE);
    db.execSQL("DROP TABLE IF EXISTS " + THREAD_MEMBER_TABLE);
    db.execSQL("DROP TABLE IF EXISTS " + THREAD_TABLE);
    db.execSQL("DROP TABLE IF EXISTS " + LOCKED_BUNCH_MEMBER_TABLE);
    db.execSQL("DROP TABLE IF EXISTS " + HIDDEN_BUNCH_MEMBER_TABLE);
    db.execSQL("DROP TABLE IF EXISTS " + BCAST_BUNCH_MEMBER_TABLE);
    db.execSQL("DROP TABLE IF EXISTS " + BUNCH_MEMBER_TABLE);
    db.execSQL("DROP TABLE IF EXISTS " + BCAST_BUNCH_TABLE);
    db.execSQL("DROP TABLE IF EXISTS " + BUNCH_TABLE);
    db.execSQL("DROP TABLE IF EXISTS " + BUDDY_REQUEST_TABLE);
    db.execSQL("DROP TABLE IF EXISTS " + BUDDY_TABLE);
    onCreate(db);
    Log.d(LOG_TAG, "recreated tables");
}

编辑:添加了更新后创建的表格:

public static final String CREATE_TABLE_LOCKED_BUNCH_MEMBER  =
            "CREATE TABLE " + LOCKED_BUNCH_MEMBER_TABLE + "(" + BUNCH_MEMBER_BUNCH_ID + " INTEGER NOT NULL, "
                    + BUNCH_MEMBER_USERNAME + " VARCHAR(64) NOT NULL, "
                    + BUNCH_MEMBER_FIRST_NAME + " VARCHAR(64) NOT NULL, "
                    + BUNCH_MEMBER_LAST_NAME + " VARCHAR(64) NOT NULL, "
                    + BUNCH_MEMBER_VISIBILITY + " INTEGER NOT NULL DEFAULT 1, "
                    + "PRIMARY KEY (" + BUNCH_MEMBER_BUNCH_ID + ", " + BUNCH_MEMBER_USERNAME + "), "
                    + "FOREIGN KEY (" + BUNCH_MEMBER_BUNCH_ID + ") " +
                    " REFERENCES " + BUNCH_TABLE + "(" + BUNCH_ID + ")" +
                    " ON UPDATE CASCADE ON DELETE CASCADE, "
                    + "FOREIGN KEY (" + BUNCH_MEMBER_USERNAME + ") " +
                    " REFERENCES " + BUDDY_TABLE + "(" + BUDDY_USERNAME + ")" +
                    " ON UPDATE CASCADE ON DELETE CASCADE)";

    public static final String CREATE_TABLE_HIDDEN_BUNCH_MEMBER  =
            "CREATE TABLE " + HIDDEN_BUNCH_MEMBER_TABLE + "(" + BUNCH_MEMBER_BUNCH_ID + " INTEGER NOT NULL, "
                    + BUNCH_MEMBER_USERNAME + " VARCHAR(64) NOT NULL, "
                    + BUNCH_MEMBER_FIRST_NAME + " VARCHAR(64) NOT NULL, "
                    + BUNCH_MEMBER_LAST_NAME + " VARCHAR(64) NOT NULL, "
                    + BUNCH_MEMBER_VISIBILITY + " INTEGER NOT NULL DEFAULT 1, "
                    + "PRIMARY KEY (" + BUNCH_MEMBER_BUNCH_ID + ", " + BUNCH_MEMBER_USERNAME + "), "
                    + "FOREIGN KEY (" + BUNCH_MEMBER_BUNCH_ID + ") " +
                    " REFERENCES " + BUNCH_TABLE + "(" + BUNCH_ID + ")" +
                    " ON UPDATE CASCADE ON DELETE CASCADE, "
                    + "FOREIGN KEY (" + BUNCH_MEMBER_USERNAME + ") " +
                    " REFERENCES " + BUDDY_TABLE + "(" + BUDDY_USERNAME + ")" +
                    " ON UPDATE CASCADE ON DELETE CASCADE)";

3 个答案:

答案 0 :(得分:0)

public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
        // This database is only a cache for online data, so its upgrade policy is
        // to simply to discard the data and start over

        //Drop the main table or the database table 
        sqLiteDatabase.execSQL(//Drop the table statement));
        //reCreate database table 
        onCreate(sqLiteDatabase);
    }

在你的情况下你不需要重新创建数据库表,所以你在onCreate()Statemen中只有一个条件

答案 1 :(得分:0)

试试这个:

CreateDatabase.class:

public class CreateDatabase {
public static final String TAG = "DBAdapter";
private SQLiteDatabase sqliteDb;

private SQLiteOpenHelper dbHelper;
private static final String DATABASE_NAME = "Supporter.sqlite";
private static final int DATABASE_VERSION = 1;

public CreateDatabase(Context ctx) {
    this.dbHelper = new DBAdapterHelper(ctx);
}

public static CreateDatabase create(Context ctx) {
    return new CreateDatabase(ctx);

}

public synchronized SQLiteDatabase open() {
    if (sqliteDb == null || !sqliteDb.isOpen()) {
        try {
            sqliteDb = dbHelper.getWritableDatabase();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return sqliteDb;
}

public void close() {
    if (sqliteDb != null && sqliteDb.isOpen()) {
        sqliteDb.close();
    }

}

private static class DBAdapterHelper extends SQLiteOpenHelper {

    public DBAdapterHelper(Context context) {

        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            ItemUserSchema.onCreate(db);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

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

}}

ItemUserSchema.class

public class ItemUserSchema {


public static final String TABLE_ITEM_USER = "tbItemUser";

public static final String KEY_icon_color = "icon_color";

public static final String KEY_status_text = "status_text";

public static final String KEY_icon = "icon";
public static final String KEY_time = "time";
public static final String KEY_link_share = "link_share";
public static final String KEY_address = "address";
public static final String KEY_item_id = "item_id";
public static final String KEY_company = "company";
public static final String KEY_name = "name";
public static final String KEY_job_salary = "job_salary";
public static final String KEY_job_mobile = "job_mobile";
public static final String KEY_job_email = "job_email";
public static final String KEY_cate_name = "cate_name";


private static final String DATABASE_CREATE_APPARAM = "Create Table "
        + TABLE_ITEM_USER + "(" + KEY_icon_color + " text,"
        + KEY_status_text + " text,"
        + KEY_icon + " text,"
        + KEY_time + " text,"
        + KEY_link_share + " text,"
        + KEY_address + " text,"
        + KEY_item_id + " INTEGER,"
        + KEY_company + " text,"
        + KEY_name + " text,"
        + KEY_job_salary + " text,"
        + KEY_job_mobile + " text,"
        + KEY_job_email + " text,"
        + KEY_cate_name + " text)";

public static void onCreate(SQLiteDatabase db) {

    db.execSQL(DATABASE_CREATE_APPARAM);
    Log.e("TABLE_ITEM_USER", DATABASE_CREATE_APPARAM);
}}

ItemUserDao.class

public class ItemUserDao {

private final SQLiteDatabase db;

public ItemUserDao(SQLiteDatabase db) {
    super();
    this.db = db;
}

public long insertItemUser(ItemUserEntity itemUserEntity) {
    ContentValues contentValues = new ContentValues();

    contentValues.put(ItemUserSchema.KEY_icon_color, itemUserEntity.getIcon_color());
    contentValues.put(ItemUserSchema.KEY_status_text, itemUserEntity.getStatus_text());
    contentValues.put(ItemUserSchema.KEY_icon, itemUserEntity.getIcon());
    contentValues.put(ItemUserSchema.KEY_time, itemUserEntity.getTime());
    contentValues.put(ItemUserSchema.KEY_link_share, itemUserEntity.getLink_share());
    contentValues.put(ItemUserSchema.KEY_address, itemUserEntity.getAddress());
    contentValues.put(ItemUserSchema.KEY_item_id, itemUserEntity.getItem_id());
    contentValues.put(ItemUserSchema.KEY_company, itemUserEntity.getCompany());
    contentValues.put(ItemUserSchema.KEY_name, itemUserEntity.getName());
    contentValues.put(ItemUserSchema.KEY_job_salary, itemUserEntity.getJob_salary());
    contentValues.put(ItemUserSchema.KEY_job_mobile, itemUserEntity.getJob_mobile());
    contentValues.put(ItemUserSchema.KEY_job_email, itemUserEntity.getJob_email());
    contentValues.put(ItemUserSchema.KEY_cate_name, itemUserEntity.getCate_name());

    return db.insertWithOnConflict(ItemUserSchema.TABLE_ITEM_USER,
            null, contentValues, SQLiteDatabase.CONFLICT_IGNORE);
}}

答案 2 :(得分:0)

您应该尝试应用SQLiteDatabase Context onCreate, 你应该替换。

SQLiteDatabase db = this.getWritableDatabase();

SQLiteDatabase db = mCOntext.getWritableDatabase();

还有一件事是,在try{} catch{}中编写数据库查询是很容易调试的好习惯。