SqfLite 没有这样的 table(代码 1 SQLITE_ERROR)

SqfLite No such table (code 1 SQLITE_ERROR)

也许这个问题已经得到解答,但我尝试将 sqfLite 添加到我的项目中,但出现此错误:

在我的调试控制台中:

no such table: infotable in "SELECT * FROM infotable"

这是我的代码:

final String infoTable = 'infotable';
final String columnID = '_id';
final String columnName = 'name';
final String columnCivilStatus = 'civil';
final String columnWorking = 'working';
final String columnJobTitle = 'job';
final String columnWorkingDomain = 'workingdomain';
final String columnMonthlyIncome = 'monthlyincome';

class SqfLite {
  int id;
  int sliderValue;
  int monthChoiceChip;
  String reasonChoiceChip;
  String name;
  String civilStatus;
  int working;
  String jobTitle;
  int workingDomain;
  String monthlyIncome;
  String currency;
  Position latitude;
  Position longitude;

  Map<String, dynamic> toMap() {
    var map = <String, dynamic>{
      columnCivilStatus: civilStatus,
      columnJobTitle: jobTitle,
      columnMonthlyIncome: monthlyIncome,
      columnWorkingDomain: workingDomain,
      columnWorking: working,
      columnName: name,
    };
    if (id != null) {
      map[columnID] = id;
    }
    return map;
  }

  SqfLite();
  SqfLite.fromMap(Map<String, dynamic> map) {
    id = map[columnID];
    name = map[columnName];
    civilStatus = map[columnCivilStatus];
    working = map[columnWorking];
    jobTitle = map[columnJobTitle];
    workingDomain = map[columnWorkingDomain];
    monthlyIncome = map[columnMonthlyIncome];
  }
}

class SqfLiteProvider {
  Database db;

  Future open(String path) async {
    db = await openDatabase(path, version: 1,
        onCreate: (Database db, int version) async {
      await db.execute('''
create table $infoTable ( 
  $columnID integer primary key autoincrement, 
  $columnName text not null,
  $columnCivilStatus text not null,
  $columnWorking integer not null,
  $columnJobTitle text not null,
  $columnWorkingDomain integer not null,
  $columnMonthlyIncome text not null)
''');
    });
  }


  Future<SqfLite> getSqfLite(int id) async {
    List<Map> maps = await db.query(infoTable,
        columns: [
          columnID,
          columnName,
          columnCivilStatus,
          columnWorking,
          columnJobTitle,
          columnWorkingDomain,
          columnMonthlyIncome,
        ],
        where: '$columnID = ?',
        whereArgs: [id]);
    if (maps.length > 0) {
      return SqfLite.fromMap(maps.first);
    }
    return null;
  }


  Future close() async => db.close();
}

我在这里得到错误:

Future<void> openDataBase() async {
    db = await openDatabase('infotable.db');
    records = await db.query('infotable');
    mapRead = records.first;
    ....
  }

我已经按照 plugin

的官方页面中的示例进行操作

由于某些pubspec.yaml冲突,我无法使用最新版本。

我已尝试 flutter clean 并重新安装该应用程序,但没有成功

您在 SqfLiteProvider 中定义了 open 方法,但您没有使用它。相反,您从未定义 onCreate 回调的 sqflite 包中调用相同的 openDatabase('infotable.db'); 方法 - 您会收到错误消息。

因此,不要直接调用 db = await openDatabase('infotable.db');,而是使用您定义的 SqfLiteProvider.open()