gpt4 book ai didi

sqlite - SQLite Flutter更新查询问题

转载 作者:行者123 更新时间:2023-12-03 03:05:28 32 4
gpt4 key购买 nike

我正在尝试在flutter应用程序中为Sqlite数据库编写一个简单的更新查询。没有成功。

这是我尝试过的一组代码。我尝试了评论和未评论的一个。

  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;
}


我在未注释段中遇到的错误
E/SQLiteLog( 3834): (20) statement aborts at 13: [UPDATE deposit_tbl SET id = NULL, username = ?, amount = ?, note = NULL, date = ? WHERE username=?] datatype mismatch
I/flutter ( 3834): Exception in Deposit = DatabaseException(datatype mismatch (code 20)) sql 'UPDATE deposit_tbl SET id = NULL, username = ?, amount = ?, note = NULL, date = ? WHERE username=?' args [gireesh@gmail.com, 200.0, Apr 18, 2019, gireesh@gmail.com]}


使用带注释的段时的错误
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,
};
}

数据更新后
updateQuestion(Question question) async {
final db = await database;
var res = await db.update("Question", question.toJson(),
where: "id = ?", whereArgs: [question.id]);
return res;
}

有关更多信息,请参阅此。
https://pub.dartlang.org/packages/sqflite
https://medium.com/flutter-community/using-sqlite-in-flutter-187c1a82e8b

关于sqlite - SQLite Flutter更新查询问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55744314/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com