使用[var db = await openDatabase()function]打开基础时:PlatformException (PlatformException(sqlite_error, Cannot perform this operation because there is no current transaction., {arguments: [], sql: null}))
。因此,我想问一下如何在事务中传递此函数,我还要注意,尽管我每次都继续执行onCreate的功能,但继续执行时。
并继续执行,一切顺利,在发行版中我遇到此错误:
DatabaseException(database is locked (code 5): , while compiling: PRAGMA journal_mode)
这是ma代码:
import 'dart:io';
import 'package:ecom_app/models/favorites.dart';
import 'package:ecom_app/models/user_local.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
class DBhelper {
static final DBhelper _instance = new DBhelper.internal();
factory DBhelper() => _instance;
static Database _db;
DBhelper.internal();
final String tableUser = 'users';
final String columnId = 'id';
final String columnFId = 'fid';
final String columnFUserId = 'fuserid';
final String columnfullname = 'fullname';
final String columnnumber = 'number';
final String columnimgurl = 'imgurl';
final String columnimglocal = 'imglocal';
final String columnrole = 'role';
final String columntoken = 'token';
final String table1 = 'favorites';
final String column1Id = 'id';
final String column1ItemId = 'itemId';
final String column1Name = 'name';
final String column1Lat = 'lat';
final String column1Long = 'long';
final String column1Desc = 'desc';
dynamic initScript = [];
static FavoritesMarkerItem _fromMap(Map<dynamic, dynamic> data) => new FavoritesMarkerItem.fromMap(data);
Future<Database> get db async {
initScript.addAll([
'CREATE TABLE $tableUser($columnId INTEGER PRIMARY KEY, $columnFId TEXT, $columnFUserId TEXT, $columnfullname TEXT, $columnnumber TEXT, $columnimgurl TEXT, $columnimglocal TEXT, $columnrole TEXT, $columntoken TEXT)',
'CREATE TABLE $table1($column1Id INTEGER PRIMARY KEY, $column1ItemId TEXT, $column1Name TEXT, $column1Lat REAL NOT NULL, $column1Long REAL NOT NULL)',
]);
if (_db != null)
return _db;
_db = await initDb();
return _db;
}
Future<Database> initDb() async {
String databasesPath = await getDatabasesPath();
String path = join(databasesPath, 'gbameh.db');
Database db = await openDatabase(path,
version: 2,
onCreate: _onCreate,
onUpgrade: _onUpgrade,
);
return db;
}
_onCreate(Database db, int newVersion) async {
await db.transaction((trx) async{
initScript.forEach((script) async=> await trx.execute(script));
});
}
_onUpgrade(Database db, int oldVersion, int newVersion) async {
initScript = [
'ALTER TABLE $table1 ADD COLUMN $column1Desc TEXT',
];
await db.transaction((trx) async{
initScript.forEach((script) async=> await trx.execute(script));
});
}
Future<int> saveUser(UserLocal user) async {
var dbClient = await db;
var res = await dbClient.insert(tableUser, user.toMap(), conflictAlgorithm: ConflictAlgorithm.replace);
return res;
}
Future<UserLocal> getUser() async {
var dbClient = await db;
List<Map> res = await dbClient.query(tableUser,
columns: [columnId, columnFId, columnFUserId, columnfullname, columnnumber, columnimgurl, columnimglocal, columnrole, columntoken],
where: '$columnId = ?',
whereArgs: [1]
);
if (res.length > 0) {
return new UserLocal.fromMap(res.first);
}
return null;
}
Future<int> update(UserLocal user) async {
var dbClient = await db;
return await dbClient.update(tableUser, user.toMap(), where: "$columnId = ?", whereArgs: [1]);
}
Future<int> deleteUser() async {
var dbClient = await db;
return await dbClient.delete(tableUser);
}
// Favorite
Future<int> save(FavoritesItem item) async {
var dbClient = await db;
var res = await dbClient.insert(table1, item.toMap());
return res;
}
Future<List<FavoritesItem>> getAll() async {
var dbClient = await db;
List<Map> res = await dbClient.query(
table1,
columns: [column1Id, column1ItemId, column1Name, column1Lat, column1Long, column1Desc],
orderBy: column1Id
);
return res.length > 0 ? res.map(_fromMap).toList() : null;
}
Future<List<FavoritesItem>> getItem(String id) async {
var dbClient = await db;
List<Map> res = await dbClient.query(
table1,
columns: [column1Id, column1ItemId, column1Name, column1Lat, column1Long, column1Desc],
where: '$column1ItemId = ?',
whereArgs: [id],
);
return res.length > 0 ? res.map(_fromMap).toList() : null;
}
Future close() async {
var dbClient = await db;
return dbClient?.close();
}
}
DBhelper dbHelper = new DBhelper();