I'm trying to create a new user table on existing database but sqflite keep showing the error of "no such table Exception" and it executes only one table . I tried changing database name also but nothing worked. Here is my DBconnector Code:
class DBConnector {
Future<Database> setDatabase() async {
var directory = await getApplicationDocumentsDirectory();
var path = join(directory.path, 'sddssd.db');
var database =
await openDatabase(path, version:1, onCreate: _createDatabase);
return database;
}
Future<void> _createDatabase(Database database, int version) async {
String questionTableName = QuestionModel.KEY_QUESTION_TABLE_NAME;
String userTableName = UserModel.KEY_USER_TABLE_NAME;
String quesTableSql =
"CREATE TABLE IF NOT EXISTS $questionTableName (id INTEGER PRIMARY KEY,question VARCHAR(200),answer VARCHAR(200));";
String userTableSql =
"""CREATE TABLE IF NOT EXISTS $userTableName (uid INTEGER PRIMARY KEY,name TEXT,email TEXT,password TEXT);""";
await database.execute(quesTableSql);
await database.execute(userTableSql);
}
}
Error :
Exception has occurred.
SqfliteDatabaseException (DatabaseException(no such table: User (code 1 SQLITE_ERROR): , while compiling: INSERT INTO User (uid, name, email, password) VALUES (NULL, ?, ?, ?)) sql 'INSERT INTO User (uid, name, email, password) VALUES (NULL, ?, ?, ?)' args [f, e, s]})
CodePudding user response:
String quesTableSql = """CREATE TABLE IF NOT EXISTS $userTableName
(
uid INTEGER PRIMARY KEY,
name TEXT,
email TEXT,
password TEXT
""");
CodePudding user response:
I solved this , the thing is onCreate callback will get executed only one time when you load the application after that no changes will get updated unless u use migration . So i created a seperate function for creating Table and got it working by making the instance of that class and call the create Table method.