I am working with an existant SQLite database that is copied from assets. Further now everything is alright but when trying to insert some data in the table an error appears:
DatabaseException(no such table: Parametre (code 1 SQLITE_ERROR[1])
and this is my :
Future<bool> insertUser (String user, String depot, String pwd) async {
int result = 0;
Database database = await openDatabase(dbPath);
final db = await database;
try {
result = await db.rawInsert(
'INSERT INTO Parametre (USER, DEPOT) VALUES ("$user", "$depot")');
}catch(e){
print(e);
}
if(result>0){
return true;
}else{
return false;
}
}
what have i missed?
CodePudding user response:
Alright, the problem appeared to be in the dbPath. The dbPath was set to 'assets/myDatabase.db'. First step was to copy the database from the assets to the app.
Second after copying the database i don't have to use the full path to openDatabase
. The right way was to use only the database name, means openDatabase('myDatabase.db')
.
CodePudding user response:
From the looks of it, either you've mispelled the table name Parametre
or you don't have a table with that name in your database. You should run a CREATE TABLE
statement to create the table somewhere before (or, if you have it in your code, it has not been run or contains some other error).
It may help you knowing that sqflite
provides two great arguments to its openDatabase
method: onCreate
and onUpdate
. They allow you to call a custom function when the database is created (a simple way to create all the tables your app needs at the first run) and another different function to update your database by adding tables or altering columns if you just provide a coherent version
argument to the same openDatabase
function.