0

I am building an app in which a user can create multiple projects. I want each project to have its unique database, since although columns (there will be 6-7 columns) name will be the same, but the values under columns for a new project will be different.

How to create new SQFlite database each time when a user creates a new project?

Or is there some other better way to achieve to achieve this?

Here is my code for the database that i have manually created if you want to go through it:

    class DatabaseHelper {
  static final DatabaseHelper _instance = new DatabaseHelper.internal();

  factory DatabaseHelper() => _instance;

  final String tableUser = "userTable";
  final String columnId = "id";

  final String columnRuns = "runs";

  static Database _db;

  Future<Database> get db async {
    if (_db != null) {
      return _db;
    }
    _db = await initDb();

    return _db;
  }

  DatabaseHelper.internal();

  initDb() async {
    Directory documentDirectory = await getApplicationDocumentsDirectory();
    String path = join(documentDirectory.path,
        "maindbnew.db"); //home://directory/files/maindb.db

    var ourDb = await openDatabase(path, version: 1, onCreate: _onCreate);
    return ourDb;
  }

   void _onCreate(Database db, int newVersion) async {
    await db.execute(
        "CREATE TABLE $tableUser($columnId INTEGER PRIMARY KEY, $columnRuns INTEGER)");
  }

  //CRUD - CREATE, READ, UPDATE, DELETE

  //Insertion
  Future<int> saveUser(User user) async {
    var dbClient = await db;
    int res = await dbClient.insert("$tableUser", user.toMap());
    return res;
  }

  //Get Users
  Future<List> getAllUsers() async {
    var dbClient = await db;
    var result = await dbClient.rawQuery("SELECT * FROM $tableUser");

    return result.toList();
  }

  Future<int> getCount() async {
    var dbClient = await db;
    return Sqflite.firstIntValue(
        await dbClient.rawQuery("SELECT COUNT(*) FROM $tableUser"));
  }

  Future<User> getUser(int id) async {
    var dbClient = await db;

    var result = await dbClient
        .rawQuery("SELECT * FROM $tableUser WHERE $columnId = $id");
    if (result.length == 0) return null;
    return new User.fromMap(result.first);
  }

  Future<int> deleteUser(int id) async {
    var dbClient = await db;

    return await dbClient
        .delete(tableUser, where: "$columnId = ?", whereArgs: [id]);
  }

  Future<int> updateUser(User user) async {
    var dbClient = await db;
    return await dbClient.update(tableUser, user.toMap(),
        where: "$columnId = ?", whereArgs: [user.id]);
  }

  Future close() async {
    var dbClient = await db;
    return dbClient.close();
  }
}



 class User {
  int _runs;

  int _id;

  User(this._runs);

  User.map(dynamic obj) {
    this._runs = obj['runs'];

    this._id = obj['id'];
  }

  int get runs => _runs;

  int get id => _id;

  Map<String, dynamic> toMap() {
    var map = new Map<String, dynamic>();
    map["runs"] = _runs;

    if (id != null) {
      map["id"] = _id;
    }
    return map;
  }

  User.fromMap(Map<String, dynamic> map) {
    this._runs = map["runs"];

    this._id = map["id"];
  }
}

P.S. I just have one column right now, I'll add more later.

Aman Kataria
  • 586
  • 2
  • 12
  • 24
  • Could you not have a main database that keeps track of the projects created and the paths to their databases? – Tom Alabaster Mar 20 '19 at 16:20
  • Okay, but the user can make new projects themselves. So I can create a main database, where all Projects created and paths of their databases will be created. How to automatically create databases here? I can create automatically generate a path alright. Can you tell me how exactly to do it? Or maybe share an appropriate example by someone else? – Aman Kataria Mar 20 '19 at 16:38
  • @Tom IF I make the path name variable in the database, which will be new for each project, will it work? – Aman Kataria Mar 21 '19 at 13:04
  • @Tom I just want to know if its possible or not – Aman Kataria Mar 21 '19 at 13:05
  • When you do `await openDatabase(path, version: 1, onCreate: _onCreate);`, if there isn't a DB at the path already it creates one. So yes it's possible. – Tom Alabaster Mar 21 '19 at 15:28
  • @tom What do you recommend for my purpose though? Should I assign a variable to the path? Or adjust things on static databases? What's the common practice for this purpose? – Aman Kataria Mar 21 '19 at 15:37

0 Answers0