SQLite Flutter更新查询问题

时间:2019-04-18 10:41:06

标签: sqlite dart flutter

我,试图在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 []}

2 个答案:

答案 0 :(得分:0)

以这种方式尝试使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

答案 1 :(得分:0)

您在查询中使用带有$的{​​{1}}符号。 使用username

您的查询将如下所示;

where = 'dUsername =?'