databaseflutterdirectorypathsqflite

How to get the correct path for sqflite Flutter database?


I'm trying to create a local database with Flutter on Android. I originally created a directory with a particular directory which worked perfectly then I uninstalled the application from the device (not AVD) Then when I tried to create a database with a new file path directory I get this expection:

Exception has occurred.
SqfliteDatabaseException (DatabaseException(near ")": syntax error (Sqlite code 1 SQLITE_ERROR): , while compiling: CREATE TABLE KPoint (id INTEGER PRIMARY KEY,subject INTEGER,module INTEGER,type TEXT,definition TEXT,), **(OS error - 2:No such file or directory))** sql 'CREATE TABLE KPoint (id INTEGER PRIMARY KEY,subject INTEGER,module INTEGER,type TEXT,definition TEXT,)' args []})

(OS error - 2:No such file or directory))

This is my init block that runs in a singleton at first instance:

import 'package:path_provider/path_provider.dart';
      initDB() async {
        Directory documentsDirectory = await getApplicationDocumentsDirectory();
        print(documentsDirectory.uri);
        String path = join(documentsDirectory.path, "database.db");
        return await openDatabase(
          path,
          version: 1,
          onOpen: (db) {},
          onCreate: (Database db, int version) async {
            print('Creating db...');
            await db.execute("CREATE TABLE KPoint ("
                "id INTEGER PRIMARY KEY,"
                "subject INTEGER,"
                "module INTEGER,"
                "type TEXT,"
                "definition TEXT,"
                ")");
          },
          onUpgrade: (db, oldVersion, newVersion) {
            print('Upgrade has been called...');
          },
        );
      }

What directory path do I need to pass into openDatabase(..) that is acceptable?


Solution

  • I don't think the "," at the end is necessary.

    from

    "definition TEXT,"
    

    to

    "definition TEXT"