'flutter sqflite query data from two tables based on given arguments
I am working on a dictionary app for my local language say for different dialects and this is my first fltter project. I want to retrieve all the data from the table corresponding to the matches from user query. If user query matches to the dWord from dzongkha table, I wanted to retrieve all related data that from Zhebsa table based on the id mapping from DzongkhaZhebsa table.
here is my table structure in which third table links the first two tables.
Future<void> _onCreate(Database db, int version) async {
await db.execute(
'''CREATE TABLE Dzongkha(
dId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
dWord TEXT NOT NULL UNIQUE,
dPhrase TEXT,
dHistory TEXT,
dFavourite TEXT,
dUpdateTime TEXT)''',
);
await db.execute(
'''CREATE TABLE Zhebsa(
zId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
zWord TEXT NOT NULL UNIQUE,
zPhrase TEXT,
zPronunciation TEXT,
zHistory TEXT,
zFavourite TEXT,
zUpdateTime TEXT)''',
);
// Run the CREATE {dzongkha_zhebsa} TABLE statement on the database.
await db.execute(
'CREATE TABLE DzongkhaZhebsa(DzongkhadId INTEGER NOT NULL, ZhebsazId INTEGER NOT NULL, updateTime TEXT, PRIMARY KEY (DzongkhadId, ZhebsazId), FOREIGN KEY(DzongkhadId) REFERENCES Dzongkha(dId) ON DELETE SET NULL, FOREIGN KEY(ZhebsazId) REFERENCES Zhebsa(zId) ON DELETE SET NULL)',
);
//Insert raw data to database
var dt = DateTime.now();
var dtStr = dt.toIso8601String();
await db.rawInsert(
'INSERT INTO Dzongkha(dId, dWord, dPhrase, dUpdateTime) VALUES(1, "བཀབ་ནེ།", "བཀབ་ནེ་བཀབ་གོ།", "$dtStr"),(2, "ཁ།", "ཁ། inn", "$dtStr"), (3, "བརྩེ་བ།", "བརྩེ་བ། kindness", "$dtStr")');
await db.rawInsert(
'INSERT INTO Zhebsa(zId, zWord, zPhrase, zPronunciation, zUpdateTime) VALUES(1, "སྐུ་གཟན།", "སྐུ་གཟན yes", "2.mp3", "$dtStr"), (2, "ན༌བཟའ།", "ན༌བཟའ་ཨིན།", "2.mp3", "$dtStr"), (3, "ཞལ།", "ཞལ། is correct", "3.mp3", "$dtStr"), (4, "བརྩེ་བ།", "བརྩེ་བ། honoriffic", "4.mp3", "$dtStr")');
await db.rawInsert(
'INSERT INTO DzongkhaZhebsa (DzongkhadId, ZhebsazId, updateTime) VALUES(1, 1, "$dtStr"), (1, 2, "$dtStr"), (2, 3, "$dtStr"), (3, 4, "$dtStr")');
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
