简体   繁体   English

Flutter 将数据插入数据库 sqflite

[英]Flutter Insert data into the database sqflite

I need to insert the data one at a time and add it to the end of the string , but what happens is that the way I'm doing is overwriting the data我需要一次insert一个data并将其添加到string的末尾,但发生的是我正在做的事情是覆盖data

 for (var i = 0; i< isChecked.length; i++) {
  receipt.ingredientsReceipt = isChecked[i]; //receipt is table name, ingredientsReceipt is column name
  print(isChecked[i]);// isChecked is my list containing [ING 1, ING 2, ING 3]
 }

Output: Output:

I/flutter ( 5360): ING 1
I/flutter ( 5360): ING 2
I/flutter ( 5360): ING 3

This is my SQL :这是我的SQL

CREATE TABLE receipt(id INTEGER PRIMARY KEY AUTOINCREMENT, nameReceipt TEXT, descReceipt TEXT, ingredientsReceipt TEXT)

Output SQL : Output SQL

{id: null, nameReceipt: TEST, descReceipt: TEST, ingredientsReceipt: ING 3}

I need it to be like this:我需要它是这样的:

{id: 1, nameReceipt: TEST, descReceipt: TEST, ingredientsReceipt: ING 1, ING 2, ING 3}

According to my comment, use 3 tables:根据我的评论,使用 3 个表:

RECEIPTS收据

id ID nameReceipt姓名收据 descReceipt描述收据
1 1 name1名称1 desc1描述1
2 2 name2名称2 desc2描述2

INGREDIENTS配料

id ID name姓名
1 1 ingr1 ingr1
2 2 ingr2 ingr2
3 3 ingr3 ingr3
4 4 ingr4 ingr4

RECEIPT_INGREDIENT RECEIPT_INGREDIENT

id ID receipt_fk收据_fk ingredient_fk成分_fk
1 1 1 1 1 1
2 2 1 1 2 2
3 3 1 1 3 3
4 4 2 2 3 3
5 5 2 2 4 4

So when you want to get a receipt, you'll have to write a query to select data from receipt, join with receipt_ingredient (where receipt.id = receipt_ingredient.receipt_fk), join with ingredient (where receipt_ingredient.ingredeint_fk = ingredient.id).因此,当您想获得收据时,您必须从收据中向 select 数据写入查询,加入receipt_ingredient(其中receipt.id =receipt_ingredient.receipt_fk),加入成分(其中receipt_ingredient.ingredeint_fk = 成分.id) .

Ex.前任。 get receipt id = 1 =>获取收据 id = 1 =>

id ID nameReceipt姓名收据 descReceipt描述收据 receipt_fk收据_fk ingredient_fk成分_fk name姓名
1 1 name1名称1 desc1描述1 1 1 1 1 ingr1 ingr1
1 1 name1名称1 desc1描述1 1 1 2 2 ingr2 ingr2
1 1 name1名称1 desc1描述1 1 1 3 3 ingr3 ingr3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM