繁体   English   中英

更新数据库sqlite android设备

[英]update database sqlite android device

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

我该怎么办?

先感谢您

您要删除数据库而不是表。

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

您正在执行database.execSQL("DROP TABLE IF EXISTS " + DATABASE_NAME); ,即:

DROP TABLE IF EXISTS myDatabase

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

DROP TABLE IF EXISTS login

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM