I got issue when i wanna create db to sqflite, DatabaseException(Error Domain=FMDatabase Code=1 "no such table) - flutter

I wanna create db with sqflite but this no create and show something issue, Unhandled Exception: DatabaseException(Error Domain=FMDatabase Code=1 "no such table: todo" UserInfo={NSLocalizedDescription=no such table: todo}) sql 'SELECT * FROM todo WHERE id = ?' args [11], oke let's check it my db helper :
db helper
import 'package:sqflite/sqflite.dart';
import 'model.dart';
class DatabaseHelper {
//singleton base
static DatabaseHelper? _instance;
DatabaseHelper._internal() {
_instance = this;
}
factory DatabaseHelper() => _instance ??= DatabaseHelper._internal();
//TODO: add database name
static late Database _database;
//TODO: add get database
Future<Database> get databse async {
_database = await _initializeDatabase();
return _database;
}
//TODO: add db name
static String _tableName = 'todo';
//TODO: initialize db
Future<Database> _initializeDatabase() async {
final path = await getDatabasesPath();
final db = openDatabase('notes.db', version: 1, onCreate: (db, version) {
version:
1;
db.execute('''CREATE TABLE $_tableName(
id INTEGER PRIMARY KEY,
title TEXT,
description TEXT,
)''');
});
return db;
}
Future<void> insertData(Note note) async {
final Database db = await databse;
List<Map<String, dynamic>> result = await db.query(
_tableName,
where: 'id = ?',
whereArgs: [note.id],
);
await db.insert(_tableName, note.toJson());
}
//get
Future<List<Note>> getAllNotes() async {
final Database db = await databse;
List<Map<String, dynamic>> result = await db.query(_tableName);
return result.map((e) => Note.fromJson(e)).toList();
}
}

Related

Error :The method 'update' isn't defined for the type 'DatabaseException'. Error: The method 'delete' isn't defined for the type 'DatabaseException'

With null safety enabled, I'm using the new dart version <2.18.1>.
this is my code
import 'dart:async';
import 'dart:io';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';
import 'package:untitled3/product.dart';
class ProductDBHelper{
static final _databaseName = 'mydb.db';
static final _databaseVersion = 1;
static final _table_products = 'products';
static String? path;
ProductDBHelper._privateContructor();
static final ProductDBHelper instance = ProductDBHelper._privateContructor();
static Database? _database;
///////////// Check whether the database created or not ...........
Future get database async{
if(_database != null) return _database;
_database = await _initDatabase();
return _database;
}
//////////////////////////Inittalize database with lile path, db name .............
_initDatabase() async{
Directory documentDirectory = await getApplicationDocumentsDirectory();
///////// Localstorage path/databasename.db
String path = join(documentDirectory.path , _databaseName);
return await openDatabase(
path,
version: _databaseVersion,
onCreate: _onCreate);
}
///////////////////// On Create for creating datadase ........................
FutureOr _onCreate(Database db, int version) async{
await db.execute('CREATE TABLE $_table_products(id INTEGER PRIMARY KEY autoincrement, name TExt , price TEXT, quantity INTEGER)');
}
static Future getfileData() async
{
return getDatabasesPath().then((value)
{
return path = value;
}
);
}
Future insertProduct(Product product) async {
Database db = await instance.database;
return await db.insert( _table_products, Product.toMap(product) , conflictAlgorithm: ConflictAlgorithm.ignore);
}
Future<List<Product>> getProductList() async{
Database db = await instance.database;
List<Map> maps = await db.query(_table_products);
print(maps);
return Product.fromMapList(maps);
}
Future<Product> updateProduct(Product product) async{
DatabaseException db = await instance.database;
await db.update(_table_products, Product.toMap(product) , where: 'id = ?'
, whereArgs: [product.id]);
return product;
}
Future deleteProduct(Product product) async {
DatabaseException db = await instance.database;
var deletedProduct = await db.delete(_table_products, where: 'id = ?'
, whereArgs: [product.id]);
return product;
}
}
I'm getting these errors:
The method 'update' isn't defined for the type 'DatabaseException'.
The method 'delete' isn't defined for the type 'DatabaseException'.
and this error in the Run log:
lib/product_db_helper.dart:76:14: Error: The method 'update' isn't
defined for the class 'DatabaseException'.
'DatabaseException' is from 'package:sqflite_common/src/exception.dart'
('../../Documents/flutter/Flutter/flutter_windows_3.3.2-stable/flutter/.pub-cache/hosted/pub.dartlang.org/sqflite_common-2.3.0/lib/src/exception.dart').
Try correcting the name to the name of an existing method, or defining
a method named 'update'.
await db.update(_table_products, Product.toMap(product) , where: 'id = ?'
^^^^^^ lib/product_db_helper.dart:84:35: Error: The method 'delete' isn't defined for the class 'DatabaseException'.
'DatabaseException' is from 'package:sqflite_common/src/exception.dart'
('../../Documents/flutter/Flutter/flutter_windows_3.3.2-stable/flutter/.pub-cache/hosted/pub.dartlang.org/sqflite_common-2.3.0/lib/src/exception.dart').
Try correcting the name to the name of an existing method, or defining
a method named 'delete'.
var deletedProduct = await db.delete(_table_products, where: 'id = ?'
^^^^^^
Future<Product> updateProduct(Product product) async{
DatabaseException db = await instance.database;
await db.//update//(_table_products, Product.toMap(product) , where: 'id = ?'
, whereArgs: [product.id]);
return product;
}
Future deleteProduct(Product product) async {
DatabaseException db = await instance.database;
var deletedProduct = await db.//delete//(_table_products, where: 'id = ?'
, whereArgs: [product.id]);
return product;
}
This is where the errors appear. I put // where the red underlines are.
Please help me.
in your updateProduct function,
Change This
DatabaseException db = await instance.database;
to this
Database db = await instance.database;

Issues sqflite in flutter

It's been a while I started flutter. And now I am working on the database part. I started with sqflite since it was the ideal one for offline apps but now I can't understand a thing please can somebody help me with this.
import 'package:flutter/widgets.dart';
import 'note.dart';
import 'package:sqflite/sqflite.dart';
import 'dart:async';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
class DatabaseHelper {
static DatabaseHelper _databaseHelper = DatabaseHelper();
static Database? _database;
String noteTable = 'note_table';
String colID = 'id';
String colTitle = 'title';
String colDescription = 'description';
String colPriority = 'priority';
String colDate = 'date';
DatabaseHelper._createInstance();
factory DatabaseHelper() {
if (_databaseHelper == null) {
var databaseHelper = DatabaseHelper._createInstance();
_databaseHelper = databaseHelper;
}
return _databaseHelper;
}
//custom getter for the database
Future<Database> get database async {
// ignore: prefer_conditional_assignment
if (_database == null) {
_database = await initializeDatabase();
}
return _database!;
}
Future<Database> initializeDatabase() async {
Directory directory = await getApplicationDocumentsDirectory();
String path = directory.path + 'note.db';
var notesDatabase =
await openDatabase(path, version: 1, onCreate: _createDb);
return notesDatabase;
}
void _createDb(Database db, int newVersion) async {
await db.execute(
'CREATE TABLE $noteTable($colID INTEGER PRIMARY KEY AUTOINCREMENT, $colTitle TEXT, $colDescription TEXT, $colPriority INTEGER, $colDate TEXT)');
}
Future<List<Map<String, dynamic>>> getNoteMapList() async {
Database db = await this.database;
var result = await db.query(noteTable, orderBy: '$colPriority ASC');
return result;
}
Future<int> insertData(Note note) async {
Database db = await this.database;
var result = await db.insert(noteTable, note.toMap());
return result;
}
Future<int> updateNote(Note note) async {
Database db = await this.database;
var result = await db
.update(noteTable, note.toMap(), where: '$colID', whereArgs: [note.id]);
return result;
}
Future<int> deleteNote(int id) async {
Database db = await this.database;
var result =
await db.rawDelete('DELETE FROM $noteTable where $colID = $id');
return result;
}
Future<int> getCount() async {
Database db = await this.database;
List<Map<String, dynamic>> x =
await db.rawQuery('SELECT COUNT (*) FROM $noteTable');
int? result = Sqflite.firstIntValue(x);
return result!;
}
Future<List<Note>> getNoteList() async {
var noteMapList = await getNoteMapList();
int count = noteMapList.length;
}
}
This is the whole code. I have watched many videos but can't understand a thing. The problem starts when I the database part starts. And while answering the questions please try to be a little simple.

MissingPluginException(No implementation found for method getDatabasesPath on channel com.tekartik.sqflite)

I am using flutter in vs code. I have got this error when I am connecting to sqflite database.
I have added sqflite,path plugin in pubspec.yaml file. I have tried
flutter clean
flutter run command too but nothing worked.
This is my Database helper method
final databaseName = "notes.db";
final tableName = "notes";
DatabaseHelper._();
static final DatabaseHelper db = DatabaseHelper._();
static Database _database;
final fieldMap = {
"id": "INTEGER PRIMARY KEY AUTOINCREMENT",
"title": "TEXT",
"body": "TEXT",
"creation_date": "DATE",
};
Future<Database> get database async {
if (_database != null) return _database;
_database = await initDB();
return _database;
}
initDB() async {
var path = await getDatabasesPath();
var dbPath = join(path, 'notes.db');
// ignore: argument_type_not_assignable
Database dbConnection = await openDatabase(dbPath, version: 1,
onCreate: (Database db, int version) async {
print("executing create query from onCreate callback");
await db.execute(_buildCreateQuery());
});
await dbConnection.execute(_buildCreateQuery());
_buildCreateQuery();
return dbConnection;
}
The values are not getting stored in sqflite databse. It returns null. So what should I do?

Flutter does not run Async function

I am trying to get an async function _read() to run and the function does not pass the line:
Reading reading = await helper.queryReading(rowId); in this function:
_read() async {
DatabaseHelper helper = DatabaseHelper.instance;
int rowId = 1;
//lines above here executes
Reading reading = await helper.queryReading(rowId); //this is the line it stops on
// nothing below here is executed
if (reading == null) {
print('read row $rowId: empty');
} else {
print('read row $rowId: ${reading.reading}');
}
}
It is being called from the following function
class Profile {
Widget getScreen(){
print("Attempting to read db");
_read();
return Scaffold( ...)
Here is my helper class:
import 'dart:ffi';
import 'dart:io';
import 'package:ema/Readings.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path_provider/path_provider.dart';
//table structure
final String tableName = 'Readings';
final String databasecolumnId = '_id';
final String databaseReading = 'Reading';
final String databaseDate = 'Time';
class DatabaseHelper {
//This is the name of the database file on disk
static final _databaseName = "readings.db";
//handles versioning for databases
static final _databaseVersion = 1;
//makes a singleton classs
DatabaseHelper._privateConstructor();
static final DatabaseHelper instance = DatabaseHelper._privateConstructor();
//allows only one item to access the database
static Database _database;
Future<Database> get database async {
_database = await _initDatabase();
return database;
}
//this opens and creates the database
_initDatabase() async {
// The path_provider plugin gets the right directory for Android or iOS.
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, _databaseName);
// Open the database. Can also add an onUpdate callback parameter.
return await openDatabase(path,
version: _databaseVersion,
onCreate: _onCreate);
}
//Creates the database
Future _onCreate(Database db, int version) async {
await db.execute(
'''
CREATE TABLE $tableName (
$databasecolumnId INTEGER PRIMARY KEY,
$databaseReading REAL NOT NULL,
$databaseDate INTERGER NOT NULL
)
'''
);
}
Future<int> insertReading(Reading reading) async {
Database db = await database;
int id = await db.insert(tableName, reading.toMap());
return id;
}
//gets reading
Future<Reading> queryReading(int id) async {
print("queryReading"); //gets here
Database db = await database;
print("Getting Db"); // not actually getting here
List<Map> maps = await db.query(tableName,
columns: [databasecolumnId, databaseReading, databaseDate],
where: '$databasecolumnId = ?',
whereArgs: [id]);
if (maps.length > 0) {
return Reading.fromMap(maps.first);
}
print('maps length : ${maps.length}');
return null;
}
}
Here is my Readings class:
class Reading {
int id;
double reading;
DateTime date;
//constructor
Reading({this.id, this.reading, this.date});
Map<String, dynamic> toMap() {
var map = <String, dynamic>{
databaseReading: reading,
databaseDate: date.millisecondsSinceEpoch,
};
if (id != null) {
map[databasecolumnId] = id;
}
return map;
}
//extracts a node object from the map obect
Reading.fromMap(Map<String, dynamic> map) {
id = map[databasecolumnId];
reading = map[databaseReading];
date = new DateTime.fromMillisecondsSinceEpoch(map [databaseDate]);
}
}
Turns out there was a deadlock in getting the database. By putting a lock on it it worked.
Here is the code to resolve it:
///declreation of the database
Database _database;
///Gets the database ensuring that there are no locks currently on the database
Future<Database> get database async {
if (_database != null) return _database;
_database = await _initDatabase();
return _database;
}

How do I get data from an sqflite table and display it as a % inside text widget

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';
// 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
)
''');
}
// 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<double> queryRowCount() async {
Database db = await instance.database;
List<Map<String, dynamic>> x = await db.rawQuery('SELECT COUNT(*) FROM $table');
int rowCount = Sqflite.firstIntValue(x);
return rowCount.toDouble();
}
// 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: '$columnName = ?', whereArgs: ['omnivore']);
}
Future<List<Map<String, dynamic>>> queryPescatarian() async {
Database db = await instance.database;
return await db.query(table, where: '$columnName = ?', whereArgs: ['pescatarian']);
}
Future<List<Map<String, dynamic>>> queryVegetarian() async {
Database db = await instance.database;
return await db.query(table, where: '$columnName = ?', whereArgs: ['vegetarian']);
}
Future<int> queryVegetarianCount() async {
var vegList = await queryVegetarian();
int count = vegList.length;
return count;
}
Future<double> queryOmnivoreCount() async {
var omniList = await queryOmnivore();
int omniCount = omniList.length;
return omniCount.toDouble();
}
Future<double> calcOmnivorePercentage() async {
var x = await queryOmnivoreCount();
var y = await queryRowCount();
double omniPercentage = (x / y) * 100;
return omniPercentage;
}
}
Hey Folks!
I was hoping someone may be able to help me please?!
I'm trying to figure out how to take data out of a a sqflite table I've created, perform a calculation that expresses it as a percentage of the other values, and display it inside a text widget in the app.
I've actually managed to get the result to print in the console using this code:
void omnivorePercentageQ() async {
final omni = await dbHelper.calcOmnivorePercentage();
print('omnivore percentage: ${omni.toStringAsFixed(1)}');
}
But I have no idea how to get it to show up in a text widget in the app itself.
Any ideas would be greatly appreciated!
Thank you,
Jason
you are not far off the answer, and already catch the value of the calculation needed. As i can see you dont need to pass any parameters to the function so i would recomend using a futurebuilder:
return FutureBuilder(
future: dbHelper.calcOmnivorePercentage(),
builder: (context, AsyncSnapshot<double> snapshot) {
if (snapshot.hasData) {
return Center( child: Text('${snapshot.data.toStringAsFixed(1)}'),);
}else
return Center(
child: CupertinoActivityIndicator(),
);
});
The Future Builder class https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html
Serves to manage widgets that depend on futures, since your calculation and database querys are async you can check its state (As inside the widget in snapshot.hasData). That conditional checks if the future has finished and else shows an indicator. Hope it helps