Creating flutter tables generating error in such table

Asked

Viewed 260 times

0

I’m having a problem where I’m not being able to create more than one table in the fluuter sqlite. When trying to create a table if the database has already been created generates the error no such table

I’m creating it this way:

static Future<Database> database() async {
  final dbPath = await getDatabasesPath();
  final sql = '''CREATE TABLE cartoes(
    id INTEGER,
    instituiBanc int NOT NULL,
    bandeira int NOT NULL,
    isDebito INTEGER,
    vencimento TEXT,
    saldoDisponivel REAL,
    limite REAL,
    PRIMARY KEY("id" AUTOINCREMENT)
  )''';

  return openDatabase(
    path.join(dbPath, 'table.db'),
    onCreate: (db, version) {
    return db.execute(sql);
    },
    version: 1,
  );
}

static Future<Database> database() async {
final dbPath = await getDatabasesPath();
final sql = '''CREATE TABLE categoria(
  id INTEGER,
  descricao TEXT NOT NULL,
  isReceita INTEGER ,
  PRIMARY KEY("id" AUTOINCREMENT)
)''';

return openDatabase(
  path.join(dbPath, 'table.db'),
  onCreate: (db, version) {
    return db.execute(sql);
  },
  version: 1,
);
}

If it enters the bank and enters the same already created, it should not create only the missing table?

1 answer

1


The problem is that you should create all the tables you need when creating the database. Each new table added later should be added along with the version increment "version".

For your case, do it as follows, so it should work:

static Future<Database> database() async {
  final dbPath = await getDatabasesPath();

  final sqlCartoes = '''CREATE TABLE cartoes(
    id INTEGER,
    instituiBanc int NOT NULL,
    bandeira int NOT NULL,
    isDebito INTEGER,
    vencimento TEXT,
    saldoDisponivel REAL,
    limite REAL,
    PRIMARY KEY("id" AUTOINCREMENT)
  )''';
  
  final sqlCategoria = '''CREATE TABLE categoria(
    id INTEGER,
    descricao TEXT NOT NULL,
    isReceita INTEGER ,
    PRIMARY KEY("id" AUTOINCREMENT)
  )''';

  return openDatabase(
    path.join(dbPath, 'table.db'),
    onCreate: (db, version) async {
      await db.execute(sqlCartoes);
      await db.execute(sqlCategoria);
    },
    version: 1,
  );
}

If you want a few more examples, you can see in the package page itself sqflite.

Browser other questions tagged

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