更新数据库sqlite android设备

时间:2012-05-14 10:08:56

标签: android sqlite

我无法升级我的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.

我该怎么办?

提前谢谢

2 个答案:

答案 0 :(得分:2)

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

database.execSQL("DROP TABLE IF EXISTS " + DATABASE_NAME);

答案 1 :(得分:2)

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

DROP TABLE IF EXISTS myDatabase

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

DROP TABLE IF EXISTS login