我有一个警报框,其中有 3 个字段:标记名、模型名、类型 现在我已经创建了一个数据库和一个表。当我填写所有 3 个字段并单击提交按钮时,它应该使用这些新值更新现有表
这是我的班级设备:
class Device{
// final int id;
int id;
final String tagName;
final String model;
final String type;
Device(this.tagName, this.model, this.type,{this.id});
// Map from the object
Map<String, dynamic> toMap() {
return {
'id': id,
'tagName': tagName,
'model': model,
'type':type
};
}
}
这是我创建的数据库和表:
initDB() async {
print("Initiating database");
return await openDatabase(
// Set the path to the database. Note: Using the `join` function from the
// `path` package is best practice to ensure the path is correctly
// constructed for each platform.
join(await getDatabasesPath(), 'device_database.db'),
onCreate: (db, version) {
//this.id, this.tagName, this.model, this.type
return db.execute(
"CREATE TABLE device_allocation(id INTEGER PRIMARY KEY AUTOINCREMENT, deviceId INTEGER, empid INTEGER, updatedOn INTEGER, isActive INTEGER NOT NULL DEFAULT '1');"
"CREATE TABLE devices(id INTEGER PRIMARY KEY AUTOINCREMENT,tagName TEXT,model TEXT,type TEXT);"
"CREATE TABLE user(empId INTEGER PRIMARY KEY, name TEXT);"
);
},
version: 1);
}
这是我执行插入操作的地方:
Future<void> insert(Device device) async {
final db = await database;
await db.insert('devices', device.toMap(),conflictAlgorithm: ConflictAlgorithm.replace);
}
这是为了生成列表:
Future<List<Device>> getDevices() async {
final db = await database;
final List<Map<String,dynamic>> maps = await db.query('devices');
return List.generate(maps.length, (i){
return Device(
maps[i]['tagName'],
maps[i]['model'],
maps[i]['type'],
id: maps[i]['id']
);
});
}
}
单击此按钮时,新值应更新为现有表:
FlatButton(
padding: EdgeInsets.only(right: 20),
child: Text('SUBMIT',
style: TextStyle(
color: Color(0xffedac51),
fontSize: 15,
fontWeight: FontWeight.bold)),
onPressed: () {
}
),