SQLite颤振更新查询问题
我正在尝试在Flatter应用程序中为Sqlite db编写一个简单的更新查询。没有成功 这是我试过的代码集。我试过有评论的和没有评论的SQLite颤振更新查询问题,sqlite,dart,flutter,Sqlite,Dart,Flutter,我正在尝试在Flatter应用程序中为Sqlite db编写一个简单的更新查询。没有成功 这是我试过的代码集。我试过有评论的和没有评论的 Future<int> insertDeposit(DepositModel depositModel) async { int result = 0; Database db = await this.database; try { result = await db.update(depositTab
Future<int> insertDeposit(DepositModel depositModel) async {
int result = 0;
Database db = await this.database;
try {
result = await db.update(depositTable, depositModel.toMap(),
where: "$dUsername=?", whereArgs: [depositModel.username]);
// String sql = "UPDATE $depositTable SET $dAmount = ${depositModel.amount}, $dDate = ${depositModel.date} WHERE $dUsername = ${depositModel.username}";
// result = await db.rawUpdate(sql);
} catch (e) {
print("Exception in Deposit = $e");
}
return result;
}
使用已注释段时出错
I/flutter ( 3834): Exception in Deposit = DatabaseException(near "18": syntax error (code 1): , while compiling: UPDATE deposit_tbl SET amount = 200.0, date = Apr 18, 2019 WHERE username = gireesh@gmail.com) sql 'UPDATE deposit_tbl SET amount = 200.0, date = Apr 18, 2019 WHERE username = gireesh@gmail.com' args []}
试着这样做,让pojo类像这样
class Question {
int id;
String question;
Question({this.id, this.question});
factory Question.fromMap(Map<String, dynamic> json) => new Question(
id: json["id"],
question: json["question"],
);
Question.fromJson(Map<String, dynamic> map) {
this.id = map['id'];
this.question = map['question'];
}
Map<String, dynamic> toJson() => {
"id": id,
"question": question,
};
}
更多信息请参考此。。
您在查询中使用了
$
使用用户名签名。
使用where='dUsername=?'
您的查询将如下所示:
result=wait db.update(存款表,存款模型.toMap(),其中:“dUsername=?”,其中rgs:[存款模型.username])代码>我会调查的。但是你能解释一下我写的更新查询失败的原因吗?我有点晚了,但这会帮助其他人,你在新对象中缺少主键。请确保该对象正在使用相同的主键更新,即“id”值不能为null或等于已存在的主键。
class Question {
int id;
String question;
Question({this.id, this.question});
factory Question.fromMap(Map<String, dynamic> json) => new Question(
id: json["id"],
question: json["question"],
);
Question.fromJson(Map<String, dynamic> map) {
this.id = map['id'];
this.question = map['question'];
}
Map<String, dynamic> toJson() => {
"id": id,
"question": question,
};
}
updateQuestion(Question question) async {
final db = await database;
var res = await db.update("Question", question.toJson(),
where: "id = ?", whereArgs: [question.id]);
return res;
}