Comments (4)
i see the example but i don't how to loop insert
Well, you can pretty much just use a loop:
final artists = ['The Beatles', 'Led Zeppelin', 'The Who', 'Nirvana'];
final stmt = db.prepare('INSERT INTO artists (name) VALUES (?)');
try {
for (final artist in artists) {
stmt.execute([artist]);
}
} finally {
stmt.dispose();
}
Is that what you were looking for?
from sqlite3.dart.
yes but when i loop to insert data the application will be Freeze for the loop insert
from sqlite3.dart.
How much data are you inserting? This library is synchronous, so it will block while the operation is happening.
You might be able to speed things up by running db.execute('BEGIN');
before and db.execute('COMMIT');
after the loop. That way, sqlite3 will only have to commit the database once and not for each write. If that doesn't fix the problem, you probably have to run the insert on another isolate.
from sqlite3.dart.
The output of the sql string must be this
"INSERT INTO table_name
(name_product,qty,price,discount) table_name
VALUES (?), (?), (?), (?), (?), (?), (?), (?)"
execute should receive a list of data: "creatSql.valuesList"
[Teste01, 1, 1, 1, Teste02, 2, 2.5, 2]
and make a loading progress, to signal that there is a process in progress for your user
void main() {
List<Map<String, dynamic>> dataList = [
{
"name_product": 'Teste01',
"qty": 1,
"price": 1.0,
"discount": 1.0,
},
{
"name_product": 'Teste02',
"qty": 2,
"price": 2.50,
"discount": 2.0,
}
];
CreatSql creatSql = CreatSql();
creatSql.toPreperSqle(dataList);
String sql = creatSql.sql;
try {
// user transaction
// sqlite3.execute('BEGIN;');
final stmt = sqlite3.prepare(sql);
stmt.execute(creatSql.valuesList);
// end transaction
// sqlite.execute('COMMIT;');
sqlit3.dispose();
} catch (e) {
// sqlite.execute('ROLLBACK;');
sqlit3.dispose();
}
}
class CreatSql {
List<dynamic>? valuesList;
String _preperer ='';
String _sql ='';
String get sql => _sql;
// CreatSql({this.valuesList, this.preperer ='', this.sql=''});
late String _keys;
toPreperSqle(List<Map<String, dynamic>> dataList) {
// convert List in string
_keys = dataList[0].keys.join(',');
for (var val in dataList) {
_returnValuesMap(val);
// print(val);
}
String tabelName = '`table_name`';
_sql = ''' INSERT INTO $tabelName ($_keys) $tabelName VALUES $_preperer''';
_sql = _sql.substring(0,_sql.length - 2);
}
_returnValuesMap(Map values) {
// convert value of Map to list
List<dynamic> valuesListAux = [];
values.forEach((key, val) {
valuesListAux.add(val);
_preperer+='(?), ';
});
valuesList = [...valuesList ?? [], ...valuesListAux];
}
}
from sqlite3.dart.
Related Issues (20)
- Writing Tests using sqlite3 package in a pure dart package HOT 1
- how to insert blob data HOT 1
- request: support sqlite3_flutter_libs `json_extract` HOT 1
- Compiler Error In iOS HOT 10
- Database disk image is malformed, HOT 7
- [Docs] Add Docs, stating `BLOB` type, expects `Uint8List`, not `T` or `Object`
- `Database.lastInsertRowId` should `return` `int?`, instead of `int` HOT 1
- how to get table-count of db, through `SQLite3.exec("SELECT count(*) FROM sqlite_schema WHERE type = 'table'").getInt()` HOT 1
- Implement a `pkg:realm:Realm:write` like method to `Database`, which calls the passed function in a transaction, commit on success, otherwise rollback HOT 1
- implement a `pkg:realm:Transaction` like class, simplifying `commit` and `rollback` HOT 3
- Cannot open WAL database via web/wasm HOT 4
- Missleading expection
- Migrate away from `elementAt` for Dart 3.3 HOT 2
- Support for NULL value HOT 1
- sqlcipher_flutter_libs link error in Ubuntu 23.10 and 24.04 HOT 3
- iOS: Could not build Objective-C module 'SQLite3' when install drift HOT 6
- sqlcipher_flutter_libs adding libraries for other platforms in Android build HOT 3
- Target "sqlcipher_flutter_libs_plugin" links to target "Threads::Threads" but the target was not found in linux-arm64
- Migrate web implementation to `dart:js_interop`
- Building to windows with sqlcipher doesn't work in another pc
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sqlite3.dart.