0

我无法升级我的 android 数据库:

public class database extends SQLiteOpenHelper {

// Database creation sql statement
private static final String DATABASE_NAME = "myDatabase";
private static final String DATABASE_CREATE_login = "CREATE TABLE login (userJoomla INTEGER, idUSER INTEGER PRIMARY KEY, name varchar(20), email varchar(50))";
private static final String DATABASE_CREATE_position = "CREATE TABLE position(latitude VARCHAR(20), longitude VARCHAR(20), address VARCHAR(100), lastUpdate DATETIME)";
private static final String DATABASE_CREATE_favorites = "CREATE TABLE favorites(_id INTEGER PRIMARY KEY AUTOINCREMENT, idSPB INTEGER UNIQUE NOT NULL)";
private static final int DATABASE_VERSION = 2;


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

@Override
public void onCreate(SQLiteDatabase db) {
    // Create the table
    db.execSQL(DATABASE_CREATE_login);      
    db.execSQL(DATABASE_CREATE_position);   
    db.execSQL(DATABASE_CREATE_favorites);  

}


// Method is called during an upgrade of the database, e.g. if you increase
// the database version
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion,int newVersion) {
    Log.w(database.class.getName(),"Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
    database.execSQL("DROP TABLE IF EXISTS " + DATABASE_NAME);
    onCreate(database);
}

}

我发现的例外是:

 Can't upgrade read-only database from version 1 to 2:

我在这里读到它应该解决;

    db = fav.getWritableDatabase();

但现在我有这个错误:

table login already exists.

我该怎么办?

先感谢您

4

2 回答 2

2

您正在删除数据库而不是表。

database.execSQL("DROP TABLE IF EXISTS " + DATABASE_NAME);
于 2012-05-14T10:14:07.133 回答
2

你正在做database.execSQL("DROP TABLE IF EXISTS " + DATABASE_NAME);,即:

DROP TABLE IF EXISTS myDatabase

改为将其更改为正确的表名。

DROP TABLE IF EXISTS login
于 2012-05-14T10:15:09.987 回答