Im building and app with flutter that uses SQLite database. I have created first table using this piece of code:
void _createDb(Database db, int newVersion) async {
await db.execute('''CREATE TABLE cards (id_card INTEGER PRIMARY KEY,
color TEXT, type TEXT, rarity TEXT, name TEXT UNIQUE, goldCost INTEGER,
manaCost INTEGER, armor INTEGER, attack INTEGER, health INTEGER, description TEXT)''');
}
Table gets created and I can access it without problems.
Unfortunately I cannot include more than 1 table that i just created. I tried adding another SQL CREATE TABLE clause in the same method, and repeating method db.execute
with a different SQL clause just in the next line.
I'm mimicing code from this tutorial: https://www.youtube.com/watch?v=xke5_yGL0uk
How to add another table within the same database?
You can create several tables and views and grant privileges in one operation using the CREATE SCHEMA statement. If an individual table, view or grant fails, the entire statement is rolled back. None of the objects are created, nor are the privileges granted.
Using the "CREATE SCHEMA" command you can combine multiple DDL steps into a single statement. As a result, the failure of a single part causes all commands within the "CREATE SCHEMA" to be rolled back. CREATE SCHEMA is limited to creating tables, views and issuing grants.
Often, it is good database design practice to split a many-to-many relationship between two tables into two one-to-many relationships involving three tables. You do this by creating a third table, called a junction table or a relationship table, that has a primary key and a foreign key for each of the other tables.
You can just combine multiple db.execute calls for exampple
await db.execute('''
create table $reminderTable (
$columnReminderId integer primary key autoincrement,
$columnReminderCarId integer not null,
$columnReminderName text not null,
$columnReminderNotifyMileage integer not null,
$columnReminderEndMileage integer not null
)''');
await db.execute('''
create table $carTable (
$columnCarId integer primary key autoincrement,
$columnCarTitle text not null
)''');
yes you can do it
void _createDb(Database db, int newVersion) async {
await db.execute('''
create table $carTable (
$columnCarId integer primary key autoincrement,
$columnCarTitle text not null
)''');
await db.execute('''
create table $userTable(
$userId integer primary key autoincrement,
$name text not null
)''');
}
but to speed up the process, let's assume we have 10 tables, you could use the batch this way
void _createDb(Database db, int newVersion) async {
Batch batch = db.batch();
batch.execute("Your query-> Create table if not exists");
batch.execute("Your query->Create table if not exists");
List<dynamic> res = await batch.commit();
//Insert your controls
}
You can use a .sql file that contains your DB script.
First,add the script file to assets.
Then, import the following packages:
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'package:flutter/services.dart' show rootBundle;
finally, use the following code
void _createDb() async
{
final database = openDatabase( join( await getDatabasesPath(), 'mydb.db'),
onCreate: (db, version) async
{
// call database script that is saved in a file in assets
String script = await rootBundle.loadString("assets\\db\\script.sql");
List<String> scripts = script.split(";");
scripts.forEach((v)
{
if(v.isNotEmpty )
{
print(v.trim());
db.execute(v.trim());
}
});
},
version: 1,
);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With