Android SQLITE-设置新数据库的版本
我使用这个示例创建了一个数据库,在新安装时复制该数据库 我现在遇到的问题是,我已经对数据库做了一些更新,并且我还对构建中的主文件进行了更新,这样用户在新安装时总能获得最新的版本 我有一个不同的类来处理所有的db调用查询语句 我已经定好了这条线Android SQLITE-设置新数据库的版本,android,sqlite,Android,Sqlite,我使用这个示例创建了一个数据库,在新安装时复制该数据库 我现在遇到的问题是,我已经对数据库做了一些更新,并且我还对构建中的主文件进行了更新,这样用户在新安装时总能获得最新的版本 我有一个不同的类来处理所有的db调用查询语句 我已经定好了这条线 private static final int DATABASE_VERSION = 5; 在新安装时,现在已正确复制数据库,但当查询类再次调用databasehelper时,将调用onupgrade()方法并尝试更新到最新版本,应用程序在尝试执行无
private static final int DATABASE_VERSION = 5;
在新安装时,现在已正确复制数据库,但当查询类再次调用databasehelper时,将调用onupgrade()方法并尝试更新到最新版本,应用程序在尝试执行无法完成的升级时崩溃
我的理解是,以下命令将数据库版本设置为新安装的版本,或者这是错误的。如果是,如何设置新安装的数据库版本
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DATABASE_VERSION);
this.context = context;
}
为了完整起见,这里有一个onupgrade()的示例
问题:新安装的新数据库设置为什么版本?如果不是版本5,如何覆盖它
谢谢它得到了一个名为
onUpgrade(SQLITEDABASE数据库、int旧版本、int新版本){
在这里执行升级操作。
}
如果您使用的是SQLiteOpenHelper类,则只有您可以重写此方法。这是正确的:
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DATABASE_VERSION);
/* ... */
}
我将getWritableDatabase()
源代码放在下面,如您所见,它不会调用onUpgrade()
,除非db文件中的版本不等于您在构造函数中传递的当前新版本(version!=mNewVersion
行)。因此,要么您的数据库文件未被覆盖,要么新数据库中的版本号错误。请检查一下
/**
* Create and/or open a database that will be used for reading and writing.
* Once opened successfully, the database is cached, so you can call this
* method every time you need to write to the database. Make sure to call
* {@link #close} when you no longer need it.
*
* <p>Errors such as bad permissions or a full disk may cause this operation
* to fail, but future attempts may succeed if the problem is fixed.</p>
*
* @throws SQLiteException if the database cannot be opened for writing
* @return a read/write database object valid until {@link #close} is called
*/
public synchronized SQLiteDatabase getWritableDatabase() {
if (mDatabase != null && mDatabase.isOpen() && !mDatabase.isReadOnly()) {
return mDatabase; // The database is already open for business
}
if (mIsInitializing) {
throw new IllegalStateException("getWritableDatabase called recursively");
}
// If we have a read-only database open, someone could be using it
// (though they shouldn't), which would cause a lock to be held on
// the file, and our attempts to open the database read-write would
// fail waiting for the file lock. To prevent that, we acquire the
// lock on the read-only database, which shuts out other users.
boolean success = false;
SQLiteDatabase db = null;
if (mDatabase != null) mDatabase.lock();
try {
mIsInitializing = true;
if (mName == null) {
db = SQLiteDatabase.create(null);
} else {
db = mContext.openOrCreateDatabase(mName, 0, mFactory);
}
int version = db.getVersion();
if (version != mNewVersion) {
db.beginTransaction();
try {
if (version == 0) {
onCreate(db);
} else {
onUpgrade(db, version, mNewVersion);
}
db.setVersion(mNewVersion);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
onOpen(db);
success = true;
return db;
} finally {
mIsInitializing = false;
if (success) {
if (mDatabase != null) {
try { mDatabase.close(); } catch (Exception e) { }
mDatabase.unlock();
}
mDatabase = db;
} else {
if (mDatabase != null) mDatabase.unlock();
if (db != null) db.close();
}
}
}
在您的情况下,它应该返回5
。只需创建一个
@Override
public void onCreate(SQLiteDatabase db) {
onUpgrade(db, 0, DATABASE_VERSION);
如果oldversion==1等,我已经有了这一点。根据数据库的版本,我在onupgrade中调用了4个方法,但所有这些都是在新安装时调用的。您所做的是正确的。执行
PRAGMA用户\u版本
在SQqlite浏览器中,您得到了什么?很抱歉,当涉及到SQLITE时,您仍然非常不高兴。你是什么意思?运行那个查询。在日志语句中或其他地方。谢谢如果你没有得到5
,那么就这样做:PRAGMA user\u version=5代码>所以让我把这个弄对。我复制的数据库文件上有一个主版本。我没有改变这一点,所以它始终是版本1。所以我需要运行PRAGMA user_version=5;并保存该文件,然后将该文件重新应用于生成。谢谢,谢谢。现在似乎可以工作了。非常感谢,我不知道我必须在文件本身上设置数据库版本
PRAGMA user_version;
@Override
public void onCreate(SQLiteDatabase db) {
onUpgrade(db, 0, DATABASE_VERSION);