Search code examples
sqlfluttersqlitedartsqflite

Sqflite table not created


I'm building a simple app which requires two tables in a database. To create the tables and the database I use the following syntax->

import 'package:sqflite/sqflite.dart' as sql;
import 'package:path/path.dart' as path;
import 'package:sqflite/sqlite_api.dart';

class DBHelper {
  static Future<Database> database() async {
    final dbPath = await sql.getDatabasesPath();
    
    return sql.openDatabase(path.join(dbPath, 'users.db'),
        onCreate: (db, version) {
      return db.execute(
          'CREATE TABLE user_profile(id TEXT PRIMARY KEY, name TEXT, address TEXT,mobileno TEXT,dob TEXT);CREATE TABLE user_transactions(id TEXT PRIMARY KEY, date TEXT, amount TEXT,cart TEXT)');
    }, version: 1);
  }

When I excuete a method to add data to the the table 'user_transactions' it gives me an error as 'E/SQLiteLog(24687): (1) no such table: user_transactions '. The fetching and adding data to user_profile works perfectly fine. I also executed the sql query in db browser for sqlite and no error was found there.I also tried uninstalling the app and installing it again.Please help


Solution

  • I think the best solution to create multiple tables in SQLite is this instead of yours:

    class DBHelper {
    Future<Database> database() async {
    return openDatabase(
      join(await getDatabasesPath(), 'users.db'),
      onCreate: (db, version) async {
        await db.execute(
            "CREATE TABLE user_profile(id TEXT PRIMARY KEY, name TEXT, address TEXT,mobileno TEXT,dob TEXT)");
        await db.execute(
            "CREATE TABLE user_transactions(id TEXT PRIMARY KEY, date TEXT, amount TEXT,cart TEXT)");
        return db;
      },
      version: 1,
    );}