sqliteHow do I use SQLite with Flutter?
Using SQLite with Flutter is a very simple process. Here is an example of how to use it:
import 'package:sqflite/sqflite.dart';
//get a database
var db = await openDatabase('my_db.db');
//create a table
await db.execute('CREATE TABLE IF NOT EXISTS my_table (id INTEGER PRIMARY KEY, name TEXT, value INTEGER, num REAL)');
//insert some data
int id1 = await db.rawInsert('INSERT INTO my_table(name, value, num) VALUES("some name", 1234, 456.789)');
//query the database
List<Map> list = await db.rawQuery('SELECT * FROM my_table');
//update some data
int count = await db.rawUpdate('UPDATE my_table SET name = ?, VALUE = ? WHERE name = ?', ['new name', '9876', 'some name']);
//delete data
int count = await db.rawDelete('DELETE FROM my_table WHERE name = ?', ['some name']);
//close database
db.close();
Above code will:
- Open a database connection with
openDatabase
- Create a table with
execute
- Insert data with
rawInsert
- Query the database with
rawQuery
- Update data with
rawUpdate
- Delete data with
rawDelete
- Close the database connection with
close
Helpful links
More of Sqlite
- How do I use the SQLite ZIP VFS to compress a database?
- How do I use the SQLite Workbench?
- How do I use a SQLite viewer to view my database?
- How do I use UUIDs in SQLite?
- How do I call sqlitepcl.raw.setprovider() when using SQLite?
- How do I set up an autoincrement primary key in SQLite?
- How can I adjust the text size in SQLite?
- How can I use a SQLite viewer on Windows?
- How do I use SQLite with Visual Studio?
- How do I use SQLite transactions?
See more codes...