Check if table exists before entering value in the Sqflite -- Flutter database

Asked

Viewed 327 times

0

I am developing an app, my problem is that with app updates I can use other tables that do not yet exist in the existing database. The solution that came to mind was always before entering a value check if the table already exists, but I’m having problems to do this.

Query:

await db.execute('''
    CREATE TABLE IF NOT EXISTS Order (
                id INT PRIMARY KEY,
                order TEXT NOT NULL
              )
      ''');

Error Received:

Exception has occurred.
SqfliteDatabaseException (DatabaseException(near "Order": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS Order (
                id INT PRIMARY KEY,
                order TEXT NOT NULL
              )) sql '    CREATE TABLE IF NOT EXISTS Order (
                id INT PRIMARY KEY,
                order TEXT NOT NULL
              )
      ' args []})

Does the query have syntax errors or does Sqflite not support this type of approach? What I could do in this house?

1 answer

1


Your problem is related to word ORDER.

ORDER is a reserved word from the database, what you can do is modify the name of your table and field...

CREATE TABLE IF NOT EXISTS Orders (id INT PRIMARY KEY, orders TEXT NOT NULL);

You can take the test here on that website if you want to see how it works.

Useful link: Sqlite Order By

Browser other questions tagged

You are not signed in. Login or sign up in order to post.