Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - How to update the table structure in Sqflite?

My app is in production and I want to manage user data when user updates the app without loss of their data, how can I achieve this with sqflite. Explicitly I want to add a column and delete another.

like image 642
Mr Magloire Avatar asked Dec 05 '22 10:12

Mr Magloire


1 Answers

You can probably add a column using raw sql, but sqlite (and thus sqflite) doesn't support dropping a column. For that you would need to do the following:

  • increase the database version number
  • in onUpgrade copy the old database columns to a temporary table
  • delete the original table
  • create a new table using the original table name but with the right schema
  • copy the data from the temp table
  • delete the temp table

Sorry, this isn't a full answer, but it is the direction I would go if I were in your situation.

like image 163
Suragch Avatar answered Jan 18 '23 22:01

Suragch