how to initialize Database variable in flutter app - flutter

I'm a beginner in flutter, i want to use SQlite database using sqflite package in my Flutter App, when I declare the _database variable with this syntax static Database _database;, I get a compilation error saying _database must be initialized except I don't know how to initialize it ?

You can initialise a database like this
// Set path of the database
var databasesPath = await getDatabasesPath();
String path = join(databasesPath, 'demo.db');
// Load the database (or create a new one, if it does not yet exist)
Database database = await openDatabase(path, version: 1,
onCreate: (Database db, int version) async {
// When creating the db, create the table
await db.execute(
'CREATE TABLE Test (id INTEGER PRIMARY KEY, name TEXT, value INTEGER, num REAL)');
});
Of course, you will need to modify SQL command depending on your desired database structure.
After initialisation, you can perform all your database operations. But don't forget to close the connection in the end via
await database.close();
More info can be found on the documentation page of the sqflite package. This is often a good place to start your research.

Related

How to bulk update SQLite table entries in Flutter

My Flutter app makes use of an SQLite database with multiple tables and Firebase authentication. Some time after publishing the app, I added a User ID column to each table, so that I could ensure that results could be filtered by users and they would only see their own data, should another user log into the app on the same device.
When I created the new column in each table though, for the entries that already existed, I allowed the data in the new column to be null to prevent an error with the database. The problem with that is that users who made the initial calculations where the User ID is null will now 'lose' that data, as it won't show up when I implement filtering with User ID.
What I want to do is bulk-update the null values in that column of each SQLite database table, if a null value exists in that table. The null values should be replaced by the current logged in User ID. I'm not sure of the best way to do this but my idea is something like this, with a database update function after it has been initialised:
Future _update(Database db, int oldVersion, int newVersion) async {
if (oldVersion < newVersion) {
Future<int> updateDB(String value) async {
final db = await instance.database;
return db.update(
values,
value.toJson(),
where: '${ValueFields.id} = ?',
whereArgs: [value.id],
);
}
}
}
Thing is, this doesn't seem to actually work and of course doesn't bulk update all rows in that particular column. Can someone advise on how I can build the bulk update function, to take all null User ID values in a particular table and change them to the current logged in user?
Thank you!
What you are doing is never updating anything if id is null. That is even if null were passed as the id via the whereargs, it would never update any rows as null is considered unique/distinct so will never be the same as another null. Instead you can use IS NULL. If the id is not null then it would not update rows where the id is null.
You can update all in one go if you use, (in SQL)
UPDATE the_table SET the_column = the_value WHERE the_column IS NULL
which I believe would be along the lines of:-
return db.update(
values,
value.toJson(),
where: '${ValueFields.id} IS NULL', /*<<<<< CHANGED WHERE CLAUSE */
whereArgs: [], /*<<<<< CHANGED NO WHERE ARGS*/
);

I want to create multiple table in sqflite by calling same function

I want to create multiple tables in sqflite by calling the same function, cause I want to create multiple playlists, playlist name will be the table name, which came from user input,
for this reason, users call the databaseCreate function in multiple time. But its show some error :
Unhandled Exception: DatabaseException(no such table: sports (code 1
SQLITE_ERROR): , while compiling: INSERT INTO sports (title, link, logo,
playlistName) VALUES (?, ?, NULL, ?)) sql 'INSERT INTO sports (title, link, logo,
playlistName)
Database Create Code :
Future open( String name) async {
Directory documentsDirectory = await getApplicationDocumentsDirectory();
final path = join(documentsDirectory.path, 'playlist1.db');
_database = await openDatabase(path, version: 2,
onCreate: (Database db, int version) async {
await db.execute('''
create table $name (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,playlistName TEXT, link TEXT, title TEXT,
logo TEXT)
''');
});
}
Hare's name comes from the user. At a very fast time, it will be okay but for creating a second playlist it will be crashed
You should prefer use parameterized queries:
await db.execute("
create table ? (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,playlistName TEXT, link TEXT, title TEXT,
logo TEXT)
", [name]);
UPDATE
If you need to update the database schema, you need to update the version of the database, like this:
return await openDatabase(
path,
version: 2, // <=== Update (increase) this number
onOpen: (db) {},
onCreate: createDatabase,
onUpgrade: upgradeDatabase,
);
Doing this, you ask SQFLite to update the schema. Use onUpgrade to tell the new schema.
You can also uninstall the app when you are in development stage. In this case, new schema should be in onCreate.
If i resume, onCreate is called once when database is created. And onUpgrade is called each time version is increased.
UPDATE 2
For unknown reasons (haven't search more), execute method does not replace the ? by parameter. Perharps for security reasons. In fact using dynamic table name is not a very good design.
So for achive this, you shoult not use parameterized query like i said previously. You should use a concatened string, like this:
await db.rawQuery(
"create table " +
name +
" ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,playlistName TEXT, link TEXT, title TEXT, logo TEXT)",[]);

How to reset auto increment id in SQLite table in Dart

I need to reset the automatic incremental ID to 0. When I delete every record from the table, the data will be deleted, then adding new data onto the table, the numbering continues from where it was before. I tried
db.delete('table_name'); // works fine. but numbering continues.
....
db.execute('TRUNCATE TABLE table_name'); // shows exception
The exception I get is:
DatabaseException(near "TRUNCATE": syntax error (code 1 SQLITE_ERROR):
How to fix this ?
This will reset the sequence index for the table you specify
await db!.delete('table_name');
await db!.update('sqlite_sequence', {'seq':1}, where: 'name = ?', whereArgs: ['table_name']);
To query all tables in sqlite schema you can use:
Future<List<Object>> showTables() async {
List<Object> tableNames = [];
List<Map<String, Object?>> tables = await db!.rawQuery("SELECT * FROM sqlite_master WHERE type=?", ['table']);
for (Map<String, Object?> table in tables) {
tableNames.add(table['name']!);
}
return tableNames;
}

How to set Autoincrement Id starts with Even Or Odd Number in SQLite With Flutter?

How to set Autoincrement Id starts with Even Or Odd Number in SQLite With Flutter?
As we have solution for MySQL but for SQLite is there any way to set Autoincrement starts with Even Or Odd Numbers
As I know same syntax is used in SQLite so if you know how to do it in MySQL just copy and paste it.
for example this has same syntax as in MySQL.
var database = await openDatabase(
path,
version: 1,
onCreate: (Database db, int version) async {
await db.execute('''
CREATE TABLE $alarm(
$columnId INTEGER PRIMARY KEY AUTOINCREMENT,
$columntitle TEXT NOT NULL,
$columnAlarmId INTEGER,
$columnDateTime TEXT NOT NULL,
$columnDaysOn UINT8LIST
)
''');
},
);
return database;
}

I am unable to extract numeric fields from a PostgreSQL database using Dart and the Postgres package

I have a simple, single row, PostgreSQL database table that includes a numeric(8,2) field, set to 120.00, and a character field set to 'N'.
This is a Windows 10 setup.
When I run the following Dart program the result is unexpected:
import 'package:postgres/postgres.dart';
void main() async {
print('Attaching to DB...');
final conn = PostgreSQLConnection(
'localhost',
5432,
'pdpdb',
username: 'XXXXXXX',
password: 'XXXXXXX',
);
await conn.open();
print('Connected to DB');
var results = await conn.query('''
SELECT verification, balance FROM contacts
''');
print(results);
await conn.close();
}
My result is this:
Attaching to DB...
Connected to DB
[[N, ╔ ╗ 2]]
So the 'N' is clear, but the numeric field is gibberish!
Why am I not seeing 120.00 as I do when I run the same command using pgAdmin's query tool:
I have tried to parse the field as if it were a double, but that gives me:
Unhandled exception:
FormatException: Invalid double
I am sure this must be something obvious so any hints will be gratefully received. Thank you.
If I write a similar process using toMaps():
final results = await conn.query('''
SELECT verification, balance FROM contacts
''').toMaps();
print(results);
I get the output:
[{verification: N, balance:
Suggesting that something has broken the output.
This isn't really an answer - more of a work around:
It seems that the Postgres package doesn't support NUMERIC fields as Dart doesn't have an equivalent type.
This workaround:
github issue
suggests the use of CASTing the numeric field to a float within the SELECT statement.
I've tried it and it does work. Granted it is not ideal, but as it works that helps me out a lot at the moment.