Flutter 向数据库sqflite中插入数据
Flutter Insert data into the database sqflite
我需要一次 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]
}
输出:
I/flutter ( 5360): ING 1
I/flutter ( 5360): ING 2
I/flutter ( 5360): ING 3
这是我的 SQL
:
CREATE TABLE receipt(id INTEGER PRIMARY KEY AUTOINCREMENT, nameReceipt TEXT, descReceipt TEXT, ingredientsReceipt TEXT)
输出SQL
:
{id: null, nameReceipt: TEST, descReceipt: TEST, ingredientsReceipt: ING 3}
我需要这样:
{id: 1, nameReceipt: TEST, descReceipt: TEST, ingredientsReceipt: ING 1, ING 2, ING 3}
根据我的评论,使用 3 个表:
收据
id
nameReceipt
descReceipt
1
name1
desc1
2
name2
desc2
成分
id
name
1
ingr1
2
ingr2
3
ingr3
4
ingr4
RECEIPT_INGREDIENT
id
receipt_fk
ingredient_fk
1
1
1
2
1
2
3
1
3
4
2
3
5
2
4
所以当你想获得收据时,你必须对收据中的 select 数据编写查询,加入 receipt_ingredient (其中 receipt.id = receipt_ingredient.receipt_fk), 加入配料 (其中 receipt_ingredient.ingredeint_fk = ingredient.id).
Ex. 获取收据 id = 1 =>
id
nameReceipt
descReceipt
receipt_fk
ingredient_fk
name
1
name1
desc1
1
1
ingr1
1
name1
desc1
1
2
ingr2
1
name1
desc1
1
3
ingr3
我需要一次 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]
}
输出:
I/flutter ( 5360): ING 1
I/flutter ( 5360): ING 2
I/flutter ( 5360): ING 3
这是我的 SQL
:
CREATE TABLE receipt(id INTEGER PRIMARY KEY AUTOINCREMENT, nameReceipt TEXT, descReceipt TEXT, ingredientsReceipt TEXT)
输出SQL
:
{id: null, nameReceipt: TEST, descReceipt: TEST, ingredientsReceipt: ING 3}
我需要这样:
{id: 1, nameReceipt: TEST, descReceipt: TEST, ingredientsReceipt: ING 1, ING 2, ING 3}
根据我的评论,使用 3 个表:
收据
id | nameReceipt | descReceipt |
---|---|---|
1 | name1 | desc1 |
2 | name2 | desc2 |
成分
id | name |
---|---|
1 | ingr1 |
2 | ingr2 |
3 | ingr3 |
4 | ingr4 |
RECEIPT_INGREDIENT
id | receipt_fk | ingredient_fk |
---|---|---|
1 | 1 | 1 |
2 | 1 | 2 |
3 | 1 | 3 |
4 | 2 | 3 |
5 | 2 | 4 |
所以当你想获得收据时,你必须对收据中的 select 数据编写查询,加入 receipt_ingredient (其中 receipt.id = receipt_ingredient.receipt_fk), 加入配料 (其中 receipt_ingredient.ingredeint_fk = ingredient.id).
Ex. 获取收据 id = 1 =>
id | nameReceipt | descReceipt | receipt_fk | ingredient_fk | name |
---|---|---|---|---|---|
1 | name1 | desc1 | 1 | 1 | ingr1 |
1 | name1 | desc1 | 1 | 2 | ingr2 |
1 | name1 | desc1 | 1 | 3 | ingr3 |