我需要创建新表并删除本地数据库中的其他表,但是仅更改数据库版本,它不起作用,它不会创建我需要的新表。
以同样的方式我已经尝试过 onUpgrade 但它不起作用
Future<Database> get database async{
if( _database != null )return _database;
_database = await initDB();
return _database;
}
initDB()async {
Directory documentDirectory = await getApplicationDocumentsDirectory();
String path = join( documentDirectory.path, 'DB.db' );
var ouDb = await openDatabase(path, version: 2,onCreate: onCreate, onUpgrade: onUpgrade);
return ouDb;
}
FutureOr<void> onCreate(Database db, int version) async {
await db.execute('CREATE TABLE nameTable0...');
db.execute("DROP TABLE IF EXISTS NameTable1");
db.execute("DROP TABLE IF EXISTS NameTable2");
}
FutureOr<void> onUpgrade(Database db, int oldVersion, int newVersion) async{
if (oldVersion < newVersion) {
await db.execute('CREATE TABLE NameTable3 ...');
}
//onCreate(db, newVersion);
}
Run Code Online (Sandbox Code Playgroud)
你需要使用onUpgrade
initDb() async {
Directory documentDirectory = await getApplicationDocumentsDirectory();
String path = join(documentDirectory.path, 'maindb.db');
var ourDb = await openDatabase(path, version: 2, onCreate: _onCreate, onUpgrade: _onUpgrade);
return ourDb;
}
// UPGRADE DATABASE TABLES
void _onUpgrade(Database db, int oldVersion, int newVersion) {
if (oldVersion < newVersion) {
// you can execute drop table and create table
db.execute("ALTER TABLE tb_name ADD COLUMN newCol TEXT;");
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
我看到很多答案解释了如何执行以下签onUpgrade入openDatabase:
if (oldVersion < newVersion) {
await db.execute('CREATE TABLE CUSTOMER....');
}
Run Code Online (Sandbox Code Playgroud)
这在执行第一次数据库升级(即版本 1 到 2)时有效,但在执行第二次数据库升级(例如版本 2 到 3)时会失败,因为表“CUSTOMER”已经创建。所以你需要逻辑只调用一次升级。此外,您的数据库可能使用版本 10,而用户使用版本 5,因此您只需要处理 6、7、8、9 和 10 的升级。以下是我为处理这些场景而构建的逻辑:
static Future<Database?> getDatabase() async {
if (_dbInstance == null) {
var databasesPath = await getDatabasesPath();
String path = join(databasesPath, _databaseName);
_dbInstance = await openDatabase(path, version: _databaseVersion,
onUpgrade: (Database db, int oldVersion, int newVersion) async {
//
//Iterate from the current version to the latest version and execute SQL statements
for (int version = oldVersion; version < newVersion; version++) {
await _performDBUpgrade(db, version + 1);
}
},
onCreate: (Database db, int newVersion) async {
//
//Start from version 1 to current version and create DB
for (int version = 0; version < newVersion; version++) {
await _performDBUpgrade(db, version + 1);
}
});
}
return _dbInstance;
}
//Call to upgrade the database. [upgradeToVersion] is the version of SQL statements that should be
//executed. A version of 1 is the initial creation of the database. Anything higher would
//be an upgrade of the database. This function should be called once for every version upgrade.
//For example, if current version is 1 and user is now performing an update and new version is
//5, then this function should be called 4 times (from `onUpgrade`), where [upgradeToVersion] would be passed a 2, 3, 4 and 5.
static Future<void> _performDBUpgrade(Database db, int upgradeToVersion) async {
switch (upgradeToVersion) {
//Upgrade to V1 (initial creation)
case 1:
await _dbUpdatesVersion_1(db);
break;
//Upgrades for V2
case 2:
//await _dbUpdatesVersion_2(db);
break;
}
}
///Database updates for Version 1 (initial creation)
static Future<void> _dbUpdatesVersion_1(Database db) async {
await db.execute('CREATE TABLE Customer...)');
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6229 次 |
| 最近记录: |