Reputation: 59
I'm trying to learn flutter by myself so i've decided to make an app. I admit that i have difficulties to make a database correctly so i've copied the code for the database from an opensource app.
Basically the copied app creates notes object which have a title, description date, id. Now what i want is to add another attribute to the object (dateexamen) (String)
What I have : I've a main.dart, a database.dart, model folder within a note.dart, a Screen folder within a page with a listView and a floating button to another page for modifying/add/deleting object.
My object has :
When i click on the floatingbutton an object is created and then the form page opens and updates the object just created.
So what i did ?:
I've just went into the note.dart, added my attribute dateexamen everywhere juste like everything was inside it and did this for all the file.
I don't have any crashes, but now it doesn't want to create any object, it works again when a delete my modification to all files..
There is the error in the console :
E/SQLiteLog( 4684): (1) table note_table has no column named dateexamen E/flutter ( 4684): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: DatabaseException(table note_table has no column named dateexamen (code 1 SQLITE_ERROR): , while compiling: INSERT INTO note_table (title, description, priority, date, dateexamen) VALUES (?, ?, ?, ?, ?)) sql 'INSERT INTO note_table (title, description, priority, date, dateexamen) VALUES (?, ?, ?, ?, ?)' args [a, b, 2, Jul 13, 2019, Sunday, July 14, 2019 at 12:00PM]}
There is the link to the app repo: https://github.com/fakman99/flutter_first/
Thank's in advance for your help, i'm very excited about flutter and i need to understand how database and backend works with flutter.
Have a good day, Fatih
Upvotes: 2
Views: 6930
Reputation: 4646
It seems that you just adjust the objects, but not the database itself.
Search for the openDatabase
openDatabase(join(await getDatabasesPath(), 'cache.db'),
onCreate: (db, version) {
return db.execute(
'CREATE TABLE cache(timestamp INTEGER, query TEXT PRIMARY KEY, data TEXT)');
}, version: 1)
and adjust the create table according to your needs. (change version or wipe app data.) retry....
Upvotes: 0
Reputation: 6239
It is likely that anyone who tries your application will not get the error you get. My guess is that you ran the application once before adding your new columns so indeed the column does not exist yet. In sqlite, once the tables are defined, unless you drop/recreate them, columns must be added manually.
Once solution for this is to bump the version number and modify (or simply drop/create the table) during the onUpgrade
callback. We can call this a schema migration process. There is an example here: https://github.com/tekartik/sqflite/blob/master/sqflite/doc/migration_example.md.
One quick test on your side is to simply uninstall your app, or delete the app data before trying you code again => magic, it works!
Upvotes: 3