Flutter Sqflite数据库数据库异常没有这样的表出现

8qgya5xd  于 2023-02-09  发布在  Flutter
关注(0)|答案(1)|浏览(134)

[错误:flutter/运行时/dart_vm_initializer. cc(41)]未处理的异常:数据库异常(没有这样的表:MST_大学(代码1 SQLITE_错误):,编译时:SELECT * 从MST_大学)sql 'SELECT * 从MST_大学'参数[]

import 'dart:developer';
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';

class DataBaseHelper {
  static var _database;

  DataBaseHelper._privateConstructor();
  static final DataBaseHelper instance = DataBaseHelper._privateConstructor();

  Future<Database> get database async {
    if (_database != null) {
      log("Database new is not creatred and retuned ");
      return _database;
    }
    // Call init Databasee method for New Database Creation
    log("new Database is create");
    _database = await _initDatabase();
    return _database;
  }

  Future<Database> _initDatabase() async {
    Directory appDocDir = await getApplicationDocumentsDirectory();
    log("new dAta");
    String databasePath = join(appDocDir.path, 'pharmacy2.db');
    return await openDatabase(
      databasePath,
      version: 2,
    );
  }

  Future<void> copyPasteAssetFileToRoot() async {
    Directory documentsDirectory = await getApplicationDocumentsDirectory();
    log("assets database is loading");
    String path = join(documentsDirectory.path, "pharmacy2.db");

    if (FileSystemEntity.typeSync(path) == FileSystemEntityType.notFound) {
      ByteData data =
          await rootBundle.load(join('assests/database', 'pharmacy2.db'));
      List<int> bytes =
          data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
      await File(path).writeAsBytes(bytes);
    }
  }

  Future<void> getAllUniversity() async {
    Database db = await instance.database;

    var result = await db.query("MST_University");

    log(result.toString());
  }
}

我尝试使用一个privet构造函数来处理单个示例,并且还从一个资产加载数据库

js81xvg6

js81xvg61#

  • 我看不到你在哪里叫copyPasteAssetFileToRoot,所以也许永远不会?
  • assests/database看起来非常像一个错别字。
  • 即使这一切都是正确的,您打开的数据库也没有这样的表。

为什么不在调试器中设置几个断点,看看哪些步骤有效,哪些步骤无效。路径是否设置正确,文件是否被复制。是否打开了正确的文件。文件是否真的有这样的表。

相关问题