1

How do I check the tables and database I created? Im getting an error saying there is no table.

Exception has occurred. SqfliteDatabaseException (DatabaseException(Error Domain=FMDatabase Code=1 "no such table: addCash" UserInfo={NSLocalizedDescription=no such table: addCash}) sql 'SELECT * FROM addCash' args []})

class DBProvider {
  DBProvider._();

  static final DBProvider db = DBProvider._();
  Database _database;

  Future<Database> get database async {
    if (_database != null) {
      return _database;
    }

    _database = await initDB();
    return _database;
  }

  initDB() async {
    Directory documentsDir = await getApplicationDocumentsDirectory();
    String path = join(documentsDir.path, 'app.db');

    return await openDatabase(path, version: 1, onOpen: (db) async {},
        onCreate: (Database db, int version) async {

      await db.execute('''
                CREATE TABLE addCash(
                    id INTEGER PRIMARY KEY,
                    contents TEXT DEFAULT ''
                )
            ''');
    });
  }

  newAddCash(AddCash addCash) async {
    final db = await database;
    var res = await db.insert('addCash', addCash.toJson());

    return res;
  }

  getAddCashx() async {
    final db = await database;
    var res = await db.query('addCash'); // ERROR IS HERE
    List<AddCash> addCash = res.isNotEmpty
        ? res.map((addCash) => AddCash.fromJson(addCash)).toList()
        : [];

    return addCash;
  }

  getAddCash(int id) async {
    final db = await database;
    var res = await db.query('addCash', where: 'id = ?', whereArgs: [id]);

    return res.isNotEmpty ? AddCash.fromJson(res.first) : null;
  }

  updateAddCash(AddCash addCash) async {
    final db = await database;
    var res = await db.update('addCash', addCash.toJson(),
        where: 'id = ?', whereArgs: [addCash.id]);

    return res;
  }

  deleteAddCash(int id) async {
    final db = await database;

    db.delete('addCash', where: 'id = ?', whereArgs: [id]);
  }
}
Kagimura
  • 397
  • 1
  • 7
  • 20

1 Answers1

1

See this answer about schema metadata for details on querying the sqlite_master table.

Alternatively, open the database using a third party database management tool... very useful for doing all sorts of database preparation, verification, inspection, testing SQL, etc.

Otherwise test it using a SELECT query just like you already did. If it returns an error, then apparently the table is missing. Do you have sufficient exception handling to report errors from within the onCreate function? onCreate should work for new databases, but perhaps you are opening a database that was created prior to that working, so it has no schema defined? (onCreate is not called if the database already exists.)

There are various other debugging techniques you can use that your code does not demonstrate, like logging, tracing, exception handling, etc. You could be doing many more things to gather information about how your code is running.

C Perkins
  • 3,733
  • 4
  • 23
  • 37