D
D
Daniil Chashkov2019-11-29 15:42:32
Dart
Daniil Chashkov, 2019-11-29 15:42:32

Why is there an error that the table does not exist?

When trying to get data from the database from the lesson table , an error appears that there is no such table.
The database is, the table too.

Receipt Code

Future<List<Lesson>> getLessons() async {
    final db = await database;
    var res = await db.query("lessons");
    List<Lesson> list =  res.isNotEmpty ? res.map((c) => Lesson.fromMap(c)).toList() : [];
    print(list);
    return list;
  }


Base code

initDB() async {
    
    Directory documentsDirectory = await getApplicationDocumentsDirectory();
    print(documentsDirectory.path);
    String path = join(documentsDirectory.path, "TestDB.db");
    await deleteDatabase(path);
    return await openDatabase(path, version: 1, onOpen: (Database db) {}, 
      onCreate: (Database db, int version) async {
      await db.execute("CREATE TABLE homeworks("
          "id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ,"
          "lesson INTEGER,"
          "date INTEGER,"
          "homework TEXT,"
          "isDone INTEGER"
          ");"
          "CREATE TABLE lessons ("
          "id INTEGER PRIMARY KEY,"
          "lesson TEXT)");
    });
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daniil Chashkov, 2019-12-01
@imnotwhoexpect

It turns out db.execute() can only execute one command.
For each table, you need to call db.execute () again

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question