扑动/ dart /sqflite:如何将动态数据库名称传递给类以返回数据库连接

0vvn1miw  于 2023-01-18  发布在  其他
关注(0)|答案(2)|浏览(114)

我需要在flutter应用程序中创建一个动态命名数据库的连接,我如何将参数传递给一个具有数据库名称的类,并返回一个连接?
我知道getter不能接受参数,我尝试添加setter函数来设置_databaseName,但没有成功。
我有一个数据库类如下:

import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart' show join;
import 'dart:io' as io;
import 'package:path_provider/path_provider.dart';
class DatabaseHelper {
  static const _databaseName = "tinkershell.db";
  static const _databaseVersion = 1;
  DatabaseHelper._privateConstructor(); // make this a singleton class
  static final DatabaseHelper instance = DatabaseHelper._privateConstructor();
  static Database?  _database; // only have a single app-wide reference to the database
  static String path = "";

  Future<Database> get database async => _database ??= await _initDatabase();

  _onOpen(Database db) async {
    //do something here later
  }

  Future<Database> _initDatabase() async {
    io.Directory documentDirectory = await getApplicationDocumentsDirectory();
    path = join(documentDirectory.path , _databaseName);
    return await openDatabase(
      join(path),
      version: _databaseVersion,
      onOpen: _onOpen,
    );
  }
}

我成功地调用了它,使用的函数如下:

Future<void> createNewApplication() async {
  Database db = await DatabaseHelper.instance.database;
  final data = db.rawQuery(" SELECT * from myTable");
}

我想打这样的电话

Future<void> createNewApplication() async {
  Database db = await DatabaseHelper.instance.database("database1234");
  final data = db.rawQuery(" SELECT * from myTable");
}
llmtgqce

llmtgqce1#

这是否可行:

import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart' show join;
import 'dart:io' as io;
import 'package:path_provider/path_provider.dart';
class DatabaseHelper {
  static const _databaseName = "tinkershell.db";
  static const _databaseVersion = 1;
  DatabaseHelper._privateConstructor(); // make this a singleton class
  static final DatabaseHelper instance = DatabaseHelper._privateConstructor();
  static Database?  _database; // only have a single app-wide reference to the database
  static String path = "";

  Future<Database> database(String databaseName) async {
   if (_databaseName==databaseName) return _database ??= await _initDatabase(databaseName);
   
  _databaseName=databaseName;
  return await _initDatabase(databaseName);
}

  _onOpen(Database db) async {
    //do something here later
  }

  Future<Database> _initDatabase(String databaseName) async {
    io.Directory documentDirectory = await getApplicationDocumentsDirectory();
    path = join(documentDirectory.path , databaseName);
    return await openDatabase(
      join(path),
      version: _databaseVersion,
      onOpen: _onOpen,
    );
  }
}
q8l4jmvw

q8l4jmvw2#

然后使用method而不是getter并传递数据库名称,如下所示:

Future<Database> database(String databaseName) async =>_database ??= await _initDatabase(databaseName);

Future<Database> _initDatabase(String databaseName) async {
  io.Directory documentDirectory = await getApplicationDocumentsDirectory();
  path = join(documentDirectory.path, databaseName); // use passed databaseName
  return await openDatabase(
    join(path),
    version: _databaseVersion,
    onOpen: _onOpen,
  );
}

相关问题