Flutter: SQLite database in external storage. database is null - sqlite

I am writing an app to store some events and dates at which events happened (For history students as part of a project). I wish to create an SQLite database, stored in some preferred location in my device. Later I want this database to merge with main database using computer. Here is my database helper class;
class DatabaseHelper {
static final _dbName = 'mainDatabase.db';
static final _dbVersion = 1;
static final _mainTable = 'mainTable';
static final _storyTable = 'storyTable';
static final _topicTable = 'topicTable';
static final columnId = '_id';
static final title = 'title';
static final description = 'description';
static final parentId = 'topic_id';
static final priority = 'priority';
static final iconId = 'iconId';
static final futureImage = 'image';
static final year = 'year';
static final month = 'month';
static final day = 'day';
DatabaseHelper._privateConstructor();
static final DatabaseHelper instance = DatabaseHelper._privateConstructor();
static Database _database;
Future<Database> get database async {
if (_database != null) {
return _database;
}
_database = await _checkPermission();
return _database;
}
var status;
_checkPermission() async {
this.status = await Permission.storage.status;
debugPrint(' storage permission status : ${this.status}');
if (await Permission.storage.request().isGranted) {
_initiateDatabase();
} else if (await Permission.storage.request().isUndetermined) {
debugPrint('Undetermined permission');
} else if (await Permission.storage.request().isDenied) {
debugPrint('Permission denied');
_checkPermission();
} else if (await Permission.storage.request().isPermanentlyDenied) {
debugPrint(' it has been permenantly denied');
}
}
_initiateDatabase() async {
debugPrint(' database initialized');
Directory directory = await getExternalStorageDirectory();
String path = join(directory.path, _dbName);
debugPrint('Path for database: ${path}');
return await openDatabase(path, version: _dbVersion, onCreate: _onCreate);
}
Future _onCreate(Database db, int version) async {
await db.execute('''
CREATE TABLE $_mainTable (
$columnId INTEGER NOT NULL,
$year INTEGER,
$month INTEGER,
$day INTEGER,
$title TEXT NOT NULL,
$description TEXT,
$priority INTEGER,
$parentId INTEGER,
PRIMARY KEY($columnId AUTOINCREMENT)
);
'''); /
Future<int> insertEvent(Map<String, dynamic> row) async {
Database db = await instance.database;
debugPrint(' event : ${row}');
assert(db != null); //Issue!!!!
return await db.insert(_mainTable, row);
}
}
My code may seem messy, because I am not from this background. I apologize for that.
When I tried to add an event I get this error;
I/flutter ( 8090): event id is null
I/flutter ( 8090): storage permission status : PermissionStatus.granted
I/flutter ( 8090): database initialized
I/flutter ( 8090): event : {year: null, month: null, day: null, title: adsfasdf, description: null, priority: 0, topic_id: null}
E/flutter ( 8090): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: 'package:thisDay/config/databaseHelper.dart': Failed assertion: line 189 pos 12: 'db != null': is not true.
/////////////// some code
E/flutter ( 8090):
I/flutter ( 8090): Path for database: /storage/emulated/0/Android/data/com.example.thisDay/files/mainDatabase.db
What should I do? I am stuck at this point. I have my db file at /storage/emulated/0/Android/data/com.example.thisDay/files/mainDatabase.db . But can't add anything in it.
Any help would be greately appreciated.
Nb:- At first I used getApplicationDirectory instead of External directory. There were no issues. I have to switch because of portability issues

Finally, I fixed my issue by myself. _initializeDatabase() has a return type Future<Database>. So making respective changes on code fixed my issue.
Future<Database> _checkPermission() async {
this.status = await Permission.storage.status;
debugPrint(' storage permission status : ${this.status}');
if (await Permission.storage.request().isGranted) {
return await _initiateDatabase();
} else if (await Permission.storage.request().isUndetermined) {
debugPrint('Undetermined permission');
} else if (await Permission.storage.request().isDenied) {
debugPrint('Permission denied');
_checkPermission();
} else if (await Permission.storage.request().isPermanentlyDenied) {
debugPrint(' it has been permenantly denied');
}
}
Future<Database> _initiateDatabase() async {
debugPrint(' database initialized');
Directory directory = await getExternalStorageDirectory();
String path = join(directory.path, _dbName);
debugPrint('Path for database: ${path}');
return await openDatabase(path, version: _dbVersion, onCreate: _onCreate);
}

for creating a database and table you can refer this code
Database database;
opendatabase() async {
// Delete the database
String databasesPath = await getDatabasesPath();
String path = join(databasesPath, "sample.db");
// await deleteDatabase(path);
database = await openDatabase(path, version: 1,
onCreate: (Database db, int version) async {
// When creating the db, create the table
await db.execute(
'CREATE TABLE tbl_test (id INTEGER PRIMARY KEY, sample TEXT, code TEXT, format TEXT)',
);
});
}

Related

Storing thumnail to SqlLite from Flutter App

Here i want to store the thumbnail to My database which is sqlite, and i am not able to store it. i am able to store it in local directory but. i wana call it from my data model. please help
Here is my Code.
_getThumb(videoPathUrl) async {
var appDocDir = await getApplicationDocumentsDirectory();
final folderPath = appDocDir.path;
print(folderPath);
String thumb = await Thumbnails.getThumbnail(
thumbnailFolder: folderPath,
videoFile: videoPathUrl,
imageType: ThumbFormat.JPEG,
quality: 25,
);
print('thumbnail: $thumb');
return thumb = model.thumbnail;
}
}
I Have Found a Solution "the Problem is not with the plugin or the method it adopts to store the Image" The problem is that it is not clear that how should a Unit8List is to be stored in the database.
In My case. __
I am Using this code to Store the Image.
_getThumb(videoPathUrl) async {
final thumb = await VideoThumbnail.thumbnailData(
video: videoPathUrl,
imageFormat: ImageFormat.JPEG,
maxWidth: 100,
maxHeight: 100,
quality: 75,
);
}
Then I am Using this final thumb to be saved against my VideoModel Variable of thumbnail
_getThumb(videoPathUrl) async {
final thumb = await VideoThumbnail.thumbnailData(
video: videoPathUrl,
imageFormat: ImageFormat.JPEG,
maxWidth: 100,
maxHeight: 100,
quality: 75,
);
print('thumbnail $thumb'); //Add these lines
model.thumbnail = thumb; //Add these Lines
return thumb; //Add these lines
}
And Once i have saved it i can see the print result in my debug console. Once i have that i am sure that it is saved.
Now i have to save it as a Blob in my db not as a String.
and thumbnail should be a Unit8List variable not a String Variable.
See the example
and in database
I am Also adding the full Video Model and DbHelper files for Others to use.
VideoModel.dart
import 'dart:typed_data';
import 'model.dart';
class VideoModel extends Model {
static String table = 'videos';
int id;
String videoName;
String video;
Uint8List thumbnail;
VideoModel({
this.id,
this.videoName,
this.video,
this.thumbnail,
});
static VideoModel fromMap(Map<String, dynamic> map) {
return VideoModel(
id: map["id"],
videoName: map['videoName'].toString(),
video: map['video'],
thumbnail: map['thumbnail'],
);
}
Map<String, dynamic> toMap() {
Map<String, dynamic> map = {
'id': id,
'videoName': videoName,
'video': video,
'thumbnail': thumbnail,
};
if (id != null) {
map['id'] = id;
}
return map;
}
}
DbHelper.dart
abstract class DB {
static Database _db;
static int get _version => 1;
static Future<void> init() async {
if (_db != null) {
return;
}
try {
var databasesPath = await getDatabasesPath();
String _path = p.join(databasesPath, 'video.db');
_db = await openDatabase(_path, version: _version, onCreate: onCreate);
} catch (ex) {
print(ex);
}
}
static void onCreate(Database db, int version) async {
await db.execute(
'CREATE TABLE videos (id INTEGER PRIMARY KEY AUTOINCREMENT, videoName STRING, video STRING, thumbnail BLOB)');
}
static Future<List<Map<String, dynamic>>> query(String table) async =>
_db.query(table);
static Future<int> insert(String table, Model model) async =>
await _db.insert(table, model.toMap());
static Future<int> update(String table, Model model) async => await _db
.update(table, model.toMap(), where: 'id = ?', whereArgs: [model.id]);
static Future<int> delete(String table, Model model) async =>
await _db.delete(table, where: 'id = ?', whereArgs: [model.id]);
static Future<Batch> batch() async => _db.batch();
}
Now when you have done these edits You can just all your image using
Image.memory(add your Image Here);
see example:
I hope that this will help some of you if you need help Let me know. Thanks

Can't create a database. No db in device file explorer

I am trying to make a little project in Flutter where I add some data in a form along with a few images in the sqflite database, but I can't figure out why exactly my code isn't working.
Here is my db_helper class
Future<Database> get db async {
if (_db != null)
return _db;
_db = await initDb();
return _db;
}
//Creating a database with name test.dn in your directory
initDb() async {
io.Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, "products.db");
var theDb = await openDatabase(path, version: 1, onCreate: _onCreate);
return theDb;
}
// Creating a table name Employee with fields
void _onCreate(Database db, int version) async {
// When creating the db, create the table
await db.execute(
"""CREATE TABLE product(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
brand TEXT NOT NULL,
color TEXT NOT NULL,
size INTEGER NOT NULL,
qty INTEGER NOT NULL)""");
await db.execute(
"""CREATE TABLE image(
id INTEGER PRIMARY KEY AUTOINCREMENT,
path TEXT,
product_id INTEGER,
FOREIGN KEY(product_id) REFERENCES product (id)
ON DELETE NO ACTION ON UPDATE NO ACTION)""");
debugPrint("Created tables");
}
// Get all products
Future<List<Map<String, dynamic>>> getProducts() async {
Database db = this.initDb();
var result = await db.query('product');
return result;
}
// Inser or update image
Future<product_model.Image> insertImage(product_model.Image image) async {
// var count = Sqflite.firstIntValue(await _db.rawQuery("SELECT COUNT(*) FROM image WHERE username = ?", [image.id]));
Database database = await db;
await database.insert("image", image.toMap(), conflictAlgorithm: ConflictAlgorithm.ignore);
// await _db.update("image", image.toMap(), where: "id = ?", whereArgs: [image.id]);
return image;
}
// Inser or update product
Future<product_model.Product> insertProduct(product_model.Product product) async {
// var count = Sqflite.firstIntValue(await _db.rawQuery("SELECT COUNT(*) FROM product WHERE username = ?", [product.id]));
Database database = await db;
await database.insert("product", product.toMap(), conflictAlgorithm: ConflictAlgorithm.ignore);
// await _db.update("product", product.toMap(), where: "id = ?", whereArgs: [product.id]);
return product;
}
And here is where I call the db:
void _saveProductWithPictures() async{
db = DBHelper();
// var db = await dbHelper.initDb();
product = db_elements.Product();
imageModels = List<db_elements.Image>();
int result;
await db.insertProduct(product).whenComplete(() => {
_nameController.clear(),
_brandController.clear(),
_colorController.clear(),
_sizeController.clear(),
_qtyController.clear()
});
if (result != 0) { // Success
_showAlertDialog('Status', 'Note Saved Successfully');
} else { // Failure
_showAlertDialog('Status', 'Problem Saving Note');
}
//save the product
// end save the product
//save the product picture/s
for (var i in _files) imageModels.add(db_elements.Image(path: i.path.toString()));
for (var image in imageModels) await db.insertImage(image).whenComplete(() => {
_files.clear()
});
//end save the product picture/s
}
I tried for weeks to fix this, but still hopeless. I can't save data to the database. Appreciate any kind of help. Thanks in advance

How to query last 3 hours in sqflite?

I'm trying to query the last 3 hours of entries from my sqflite database. Here's my current query:
Future<List<Map<String, dynamic>>> queryLastThreeHours() async {
Database db = await instance.database;
return await db.query(table, where: '$columnDate = ?', whereArgs: ['now', '-3 hours']);
}
For more context, here's the complete code for the database_helper.dart:
import 'dart:io';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path_provider/path_provider.dart';
class DatabaseHelper {
static final _databaseName = "MyDatabase.db";
static final _databaseVersion = 1;
static final table = 'my_table';
static final columnId = '_id';
static final columnName = 'name';
static final columnAge = 'age';
static final columnColour = 'colour';
static final columnDate = 'date';
// make this a singleton class
DatabaseHelper._privateConstructor();
static final DatabaseHelper instance = DatabaseHelper._privateConstructor();
// only have a single app-wide reference to the database
static Database _database;
Future<Database> get database async {
if (_database != null) return _database;
// lazily instantiate the db the first time it is accessed
_database = await _initDatabase();
return _database;
}
// this opens the database (and creates it if it doesn't exist)
_initDatabase() async {
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, _databaseName);
return await openDatabase(path,
version: _databaseVersion,
onCreate: _onCreate);
}
// SQL code to create the database table
Future _onCreate(Database db, int version) async {
await db.execute('''
CREATE TABLE $table (
$columnId INTEGER PRIMARY KEY,
$columnName TEXT NOT NULL,
$columnAge INTEGER NOT NULL,
$columnColour TEXT NOT NULL,
$columnDate INTEGER NOT NULL
)
''');
}
// Helper methods
// Inserts a row in the database where each key in the Map is a column name
// and the value is the column value. The return value is the id of the
// inserted row.
Future<int> insert(Map<String, dynamic> row) async {
Database db = await instance.database;
return await db.insert(table, row);
}
// All of the rows are returned as a list of maps, where each map is
// a key-value list of columns.
Future<List<Map<String, dynamic>>> queryAllRows() async {
Database db = await instance.database;
return await db.query(table);
}
// All of the methods (insert, query, update, delete) can also be done using
// raw SQL commands. This method uses a raw query to give the row count.
Future<int> queryRowCount() async {
Database db = await instance.database;
return Sqflite.firstIntValue(await db.rawQuery('SELECT COUNT(*) FROM $table'));
}
// We are assuming here that the id column in the map is set. The other
// column values will be used to update the row.
Future<int> update(Map<String, dynamic> row) async {
Database db = await instance.database;
int id = row[columnId];
return await db.update(table, row, where: '$columnId = ?', whereArgs: [id]);
}
// Deletes the row specified by the id. The number of affected rows is
// returned. This should be 1 as long as the row exists.
Future<int> delete(int id) async {
Database db = await instance.database;
return await db.delete(table, where: '$columnId = ?', whereArgs: [id]);
}
Future<List<Map<String, dynamic>>> queryOmnivore() async {
Database db = await instance.database;
return await db.query(table, where: '$columnColour = ?', whereArgs: ['Omnivore']);
}
Future<List<Map<String, dynamic>>> queryLastThreeHours() async {
Database db = await instance.database;
return await db.query(table, where: '$columnDate = ?', whereArgs: ['now', '-3 hours']);
}
}
Any help would be greatly appreciated. I've tried to work it out using Sqlite SELECT * for Last 7 days and How to get Last 3 hours data from SQLite, but still haven't figured it out.
Thank you very much in advance!
Jason

Escalate DBProvider Flutter

I am new to working with databases in flutter. I have a data folder with a database.dart in the following form. This is fine for one table and with a few CRUD functions such as newNote or getNotes, but what happens when you have multiple tables and multiple CRUD Functions? Are you supposed to dump them all on this database.dart file making it chaotic? What are better alternatives for organization?
class DBProvider {
DBProvider._();
static final DBProvider db = DBProvider._();
Database _database;
Future<Database> get database async {
if (_database != null) {
return _database;
}
_database = await initDB();
return _database;
}
initDB() async {
Directory documentsDir = await getApplicationDocumentsDirectory();
String path = join(documentsDir.path, 'app.db');
return await openDatabase(path, version: 1, onOpen: (db) async {
}, onCreate: (Database db, int version) async {
// Create the note table
await db.execute('''
CREATE TABLE note(
id INTEGER PRIMARY KEY,
contents TEXT DEFAULT ''
)
''');
});
}
newNote(Note note) async {
final db = await database;
var res = await db.insert('note', note.toJson());
return res;
}
}

Flutter SQflite: How to solve unhandled exception?

In my app I do want to save some data with sqflite plugin, but it keeps throwing me the error:
Unhandled exception: type '_InternalLinkedHashMap' is not a subtype of type
Map<String, dynamic>' where _InternalLinkedHashMap is from dart:collection
- Map is from dart:core
- String is from dart:core
This is my code for regenerating my error:
Rezepte.dart The where I am handling the table named Rezepte from the database.
class Rezepte{
Rezepte();
int id;
String name;
int personen;
String beschreibung;
int favorit;
static final spalten = ["id", "name", "personen", "beschreibung", "favorit"];
Map toMap(){
Map map = {
"name": name,
"personen": personen,
"beschreibung": beschreibung,
"favorit":favorit
};
if(id != null){
map["id"] = id;
}
return map;
}
static fromMap(Map map){
Rezepte rezepte = new Rezepte();
rezepte.id = map["id"];
rezepte.name = map["name"];
rezepte.personen = map["personen"];
rezepte.beschreibung = map["beschreibung"];
rezepte.favorit = map["favorit"];
return rezepte;
}
}
Datenbank.dart The code for handling the whole database. My Database-H
import 'dart:async';
import 'dart:io';
import 'package:flutter_app/datenbank/Rezepte.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path_provider/path_provider.dart';
class DatabaseClient{
Database database;
//Datenbank wird erstellt
Future erstellen() async{
Directory pfad = await getApplicationDocumentsDirectory();
String datenbankPfad = join(pfad.path, "rezept_buch.db");
database = await openDatabase(datenbankPfad, version: 1, onCreate: this.erstelleTabellen);
}
//Die Tabellen werden erstellt
Future erstelleTabellen(Database db, int version) async{
await db.execute("""
create table rezept (
id integer primary key,
name text not null,
personen_anzahl integer not null,
beschreibung text default null,
favorit integer default 0
)
""");
}
Future setRezept(Rezepte rezepte) async{
var count = Sqflite.firstIntValue(await database.rawQuery("SELECT * FROM rezept WHERE name =?", [rezepte.name]));
if(count == 0){
rezepte.id = await database.insert("rezept", rezepte.toMap());
} else {
await database.update("rezept", rezepte.toMap(), where: "id = ?", whereArgs: [rezepte.id]);
}
return rezepte;
}
//Daten aus Tabellen holen
Future getAllRezepte(int id) async{
List ergebnisse = await database.query("rezept", columns: Rezepte.spalten, where: "id=?", whereArgs: [id]);
Rezepte rezepte = Rezepte.fromMap(ergebnisse[0]);
return rezepte;
}
}
This two files are the only one, that are able to generate my error. Does anybody have an idea how I could possibly solve the error?
Hard to know where the issue happens without a longer stack trace. Make sure you are using the latest version (sqflite >=8.4) especially if you are using --preview-dart-2. I also recommend turning on implicit-casts: false in analysis_options.yaml
analyzer:
strong-mode:
implicit-casts: false
Casting a Map to Map<String, dynamic> could throw an error if the map is not of the proper type.
The crash reported by Flutter should also indicate the file and line where the issue happened.

Resources