【发布时间】:2020-02-26 05:46:53
【问题描述】:
我正在尝试将 List 插入到 Flutter 中的 sql 数据库中,但我不知道该怎么做,谁能帮助我?
我在初始化 mi 数据库时有这个:
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, "tuCuestionarioDB.db");
return await openDatabase(path, version: 1, onOpen: (db) {},
onCreate: (Database db, int version) async {
await db.execute("CREATE TABLE Question ("
"id INTEGER PRIMARY KEY,"
"subject INTEGER,"
"topic INTEGER,"
"question TEXT,"
"answer INTEGER,"
"answers TEXT ARRAY," //THIS IS MY LIST<STRING>
"rightAnswer INTEGER,"
"correctly BIT,"
"answered BIT"
")");
});
我有这个插入数据:
newQuestion(Question newQuestion) async {
final db = await database;
//get the biggest id in the table
var table = await db.rawQuery("SELECT MAX(id)+1 as id FROM Question");
int id = table.first["id"];
//insert to the table using the new id
var raw = await db.rawInsert(
"INSERT Into Question (id,subject,topic,question,answer,answers,rightAnswer,correctly,answered)"
" VALUES (?,?,?,?,?,?,?,?,?)",
[id != null ? id : 0, newQuestion.subject, newQuestion.topic,newQuestion.question,newQuestion.answer,newQuestion.answers,newQuestion.rightAnswer,newQuestion.correctly ? 1 : 0,newQuestion.answered ? 1 : 0]);
return raw;
}
但是当你尝试插入这样的值时:
{subject: 1, topic: 1, question: What is the best community?, answer: 3, rightAnswer: 0, answers: [Stack Overflow, Facebook, Yahoo Answers], correctly: false, answered: false}
我遇到了这样的错误:
发生异常。
SqfliteDatabaseException (DatabaseException(java.lang.String 不能 转换为 java.lang.Integer) sql 'INSERT Into Question (id,主题,主题,问题,答案,答案,正确答案,正确,已回答) VALUES (?,?,?,?,?,?,?,?,?)' args [0, 1, 1, 什么是最好的 community?, 3, [Stack Overflow, Facebook, Answers Yahoo], 0, 0, 0]})
当我删除 anwsers 字段时,我没有发现任何错误
【问题讨论】:
标签: sql flutter visual-studio-code sqflite