flutter sqflite DatabaseException没有这样的表

vhipe2zx  于 2023-05-18  发布在  Flutter
关注(0)|答案(4)|浏览(201)

我尝试在这里使用sqflite,但我一直得到这个错误,当我尝试插入值时,没有这样的表,但我想创建一个表

SqfliteDatabaseException (DatabaseException(no such table: usernotes (code 1 SQLITE_ERROR): , while compiling: INSERT OR REPLACE INTO usernotes (id, title, content) VALUES (?, ?, ?)) sql 'INSERT OR REPLACE INTO usernotes (id, title, content) VALUES (?, ?, ?)' args [2021-03-05 20:44:59.369377, a new note, what should i do]})

下面是我的数据库助手类和控制器

import 'package:mynotes/models/note_model.dart';
import 'package:sqflite/sqflite.dart' as sql;
import 'package:path/path.dart' as path;

class DBHelper {
  static sql.Database _database;

  static Future<void> init() async {
    if (_database != null) {
      return;
    }

    try {
      var databasePath = await sql.getDatabasesPath();
      String _path = path.join(databasePath, 'notes.db');
      _database = await sql.openDatabase(_path, version: 1, onCreate: onCreate);
    } catch (e) {
      print(e);
    }
  }

  static Future onCreate(sql.Database db, int version) async {
    await db.execute(
        'CREATE TABLE usernotes(id TEXT PRIMARY KEY,title TEXT,content TEXT)');
  }

  static Future insert(String table, NoteModel noteModel) async =>
      await _database.insert(table, noteModel.toMap(),
          conflictAlgorithm: sql.ConflictAlgorithm.replace);
}

还有这里

import 'package:mynotes/helper/db_helper.dart';
import 'package:mynotes/models/note_model.dart';

class NoteController with ChangeNotifier {
  List<NoteModel> _notes = [];
  List<NoteModel> get notes => _notes;

  Future<void> addToNote(String title, String content) async {
    try {
      await DBHelper.init();
      final newNote = NoteModel(
        id: DateTime.now().toString(),
        title: title,
        content: content,
      );
      _notes.add(newNote);
      notifyListeners();
      DBHelper.insert(
          'usernotes',
          NoteModel(
              id: newNote.id, title: newNote.title, content: newNote.content));
      notifyListeners();
      print(notes);
    } catch (e) {
      print(e);
    }
  }}

那么问题在哪里呢?我试图运行具有相同功能的其他项目,此错误没有显示

rt4zxlrg

rt4zxlrg1#

尝试切换

static Future<void> init() async {
if (_database != null) {
  return;
}

try {
  var databasePath = await sql.getDatabasesPath();
  String _path = path.join(databasePath, 'notes.db');
  _database = await sql.openDatabase(_path, version: 1, onCreate: onCreate);
} catch (e) {
  print(e);
}}

static Future<Database> init() async {
if (_database != null) return _database;

try {
  var databasePath = await sql.getDatabasesPath();
  String _path = path.join(databasePath, 'notes.db');
  _database = await sql.openDatabase(_path, version: 1, onCreate: onCreate);
  return _database;
} catch (e) {
  print(e);
}}

因为你需要返回数据库。接下来,在最后一段中,您需要将await DBHelper.init();替换为db = await DBHelper.init();,最后将DBHelper.insert(...替换为db.insert(...

4smxwvx5

4smxwvx52#

我只是在打开数据库之前添加了这一行

Directory path = await getApplicationDocumentsDirectory();

并利用这条路

jpfvwuh4

jpfvwuh43#

每次修改数据库时,都需要卸载应用程序并重新安装(或尝试增加数据库中的模式版本)。

tuwxkamq

tuwxkamq4#

如果您使用的是db.batch(),在openDatabase的onCreate和onUpgrade事件中,请确保提交更改以将其持久化到磁盘。

onCreate: (db, version) async {

    final batch = db.batch();
        batch.execute('''
          CREATE TABLE $myTable (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            name TEXT NOT NULL
          );  
        ''');
    await batch.commit(); // <----
  }, version: version,

相关问题