答案 0 :(得分:5)
打开pubspec.yaml
,然后在“依赖项”部分中添加以下行:
sqflite: ^1.0.0
path_provider: ^0.4.1
sqflite
当然是SQFlite插件,而path_provider
将帮助我们在Android和iPhone上获取用户目录。
我在单例类中保持对数据库的全局引用。这将防止并发问题和数据泄漏(这是我所听到的,但请告诉我是否错误)。您还可以在此处添加用于访问数据库的辅助方法(例如update)。
创建一个名为 database_helper.dart 的新文件,并粘贴以下代码:
import 'dart:io' show Directory;
import 'package:path/path.dart' show join;
import 'package:sqflite/sqflite.dart';
import 'package:path_provider/path_provider.dart' show getApplicationDocumentsDirectory;
class DatabaseHelper {
static final _databaseName = "MyDatabase.db";
static final _databaseVersion = 1;
static final table = 'my_table';
static final columnId = '_id';
static final columnName = 'name';
static final columnAge = 'age';
// make this a singleton class
DatabaseHelper._privateConstructor();
static final DatabaseHelper instance = DatabaseHelper._privateConstructor();
// only have a single app-wide reference to the database
static Database _database;
Future<Database> get database async {
if (_database != null) return _database;
// lazily instantiate the db the first time it is accessed
_database = await _initDatabase();
return _database;
}
// this opens the database (and creates it if it doesn't exist)
_initDatabase() async {
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, _databaseName);
return await openDatabase(path,
version: _databaseVersion,
onCreate: _onCreate);
}
// SQL code to create the database table
Future _onCreate(Database db, int version) async {
await db.execute('''
CREATE TABLE $table (
$columnId INTEGER PRIMARY KEY,
$columnName TEXT NOT NULL,
$columnAge INTEGER NOT NULL
)
''');
}
}
首先让insert a row,以便我们进行一些更新:
_insert() async {
Database db = await DatabaseHelper.instance.database;
Map<String, dynamic> row = {
DatabaseHelper.columnName : 'Bob',
DatabaseHelper.columnAge : 23
};
int id = await db.insert(DatabaseHelper.table, row);
print(await db.query(DatabaseHelper.table));
}
然后这是更新方法:
_update() async {
// get a reference to the database
// because this is an expensive operation we use async and await
Database db = await DatabaseHelper.instance.database;
// row to update
Map<String, dynamic> row = {
DatabaseHelper.columnName : 'Mary',
DatabaseHelper.columnAge : 32
};
// We'll update the first row just as an example
int id = 1;
// do the update and get the number of affected rows
int updateCount = await db.update(
DatabaseHelper.table,
row,
where: '${DatabaseHelper.columnId} = ?',
whereArgs: [id]);
// show the results: print all rows in the db
print(await db.query(DatabaseHelper.table));
}
注释
DatabaseHelper
类和sqflite
。Map<String, dynamic>
将列名映射到每一行中的数据。 SQFlite还支持进行原始更新。这意味着您可以使用SQL字符串。让我们再次使用rawUpdate()
更新同一行。
int updateCount = await db.rawUpdate('''
UPDATE my_table
SET name = ?, age = ?
WHERE _id = ?
''',
['Susan', 13, 1]);
最后方括号中的项目绑定到SQL字符串中的?
问号。您可以使用插值来填充表和列的名称,但由于存在SQL注入攻击的危险,因此不应对值使用插值。
int updateCount = await db.rawUpdate('''
UPDATE ${DatabaseHelper.table}
SET ${DatabaseHelper.columnName} = ?, ${DatabaseHelper.columnAge} = ?
WHERE ${DatabaseHelper.columnId} = ?
''',
['Susan', 13, 1]);
答案 1 :(得分:0)
请试试这个
await db.update(TABLE_NAME, data, where: 'column = ?', whereArgs: [value]);