Flutter sqflite插入列表< String> [英] Flutter sqflite insert List<String>

查看:84
本文介绍了Flutter sqflite插入列表< String>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在Flutter中将列表插入到sql数据库中,但是我不知道该怎么办,有人可以帮助我吗?

i'm trying to insert a List into a sql database in flutter , but i dont know how can i do it , can anyone help me ?

当我初始化mi数据库时,我有这个:

i have this when i initialize mi database:

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

但是在y时尝试插入这样的值:

but when y try to insert a value like this:

{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,什么是最好的社区?,3,[Stack Overflow,Facebook,Answers Yahoo],0,0,0]})

SqfliteDatabaseException (DatabaseException(java.lang.String cannot be cast to java.lang.Integer) sql 'INSERT Into Question (id,subject,topic,question,answer,answers,rightAnswer,correctly,answered) VALUES (?,?,?,?,?,?,?,?,?)' args [0, 1, 1, What is the best community?, 3, [Stack Overflow, Facebook, Answers Yahoo], 0, 0, 0]})

当我删除anwsers字段时,我没有任何错误

and when i remove anwsers field i got no erros

推荐答案

我很确定您不能以这种方式存储字符串列表.根据 docs ,它仅支持blob的整数.我认为您可以通过两种方式进行此操作.

I'm pretty sure you cannot store a list of strings in this way. According to the docs, it only supports a blob of ints. I think there are two ways you could do this.

一种方法是将答案分成一个字符串.您可以使用 join 方法并将其分开它由一个不包含任何答案的字符组成(例如|这样的管道).然后,当您从数据库中读取数据时,可以 split使用您选择的字符将字符串返回数组.

One way would be to stringify the answers into a single string. You can use the join method and separate it by a character that no answer will contain (maybe a pipe like |). Then, when you read from the database, you can split the string back into an array using the character you chose.

执行此操作的另一种方法是创建一个表,该表将单个问题与多个答案相关联.这基本上是sudo代码,但是如果您有一个与此表匹配的表,它应该可以工作.

The other way to do this would be to create a table that associates a single question with multiple answers. This is basically sudo code, but if you had a table that matched this it should work.

Answer

int id autoincremented, int questionId, String answerText, int isCorrectAnswer (0 or 1)

也没有什么联系,但是sqflite支持AUTOINCREMENT关键字,我建议您将其用作主键ID.

Also, a bit unrelated, but sqflite supports the AUTOINCREMENT keyword which I recommend you use for your primary key IDs.

这篇关于Flutter sqflite插入列表&lt; String&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆