【问题标题】:Problems with loading existing SQFlite Database in Flutter app在 Flutter 应用程序中加载现有 SQFlite 数据库的问题
【发布时间】:2021-09-29 21:20:44
【问题描述】:

我正在尝试在我的 Flutter 应用中打开现有的 SQLite 数据库,但在启动应用时我总是在控制台中收到该错误消息:

I/OpenGLRenderer(16880): Davey! duration=949ms; Flags=1, IntendedVsync=32058088200606, Vsync=32058088200606, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=32058101631102, AnimationStart=32058101709175, PerformTraversalsStart=32058101714227, DrawStart=32059029835685, SyncQueued=32059030411883, SyncStart=32059030862195, IssueDrawCommandsStart=32059030915893, SwapBuffers=32059035930789, FrameCompleted=32059037678966, DequeueBufferDuration=3920000, QueueBufferDuration=1405000, 
E/SQLiteLog(16880): (1) no such table: Links
E/SQLiteLog(16880): (1) no such table: Links
I/flutter (17672): sdlu conver_refactor MemberProfileDealer 17 1 1
I/flutter (17672): sdlu conver_refactor MessageDealer 45 0 1
I/flutter (17672): sdlu conver_refactor LiveInfoDealer 497 1 0
I/flutter (17672): sdlu conver_refactor ProductInfoDealer 810 0 0
I/flutter (17672): sdlu conver_refactor MarkInfoDealer 1511 0 3
I/flutter (17672): sdlu conver_refactor MemberProfileDealer 4 0 1
I/flutter (17672): sdlu conver_refactor MessageDealer 41 0 0
I/flutter (17672): sdlu conver_refactor ProductInfoDealer 823 0 0
I/flutter (17672): sdlu conver_refactor LiveInfoDealer 1095 0 2
I/flutter (17672): sdlu conver_refactor MarkInfoDealer 1404 0 5
I/flutter (17672): sdlu conver_refactor MemberProfileDealer 19 0 0
I/flutter (17672): sdlu conver_refactor MessageDealer 46 0 0
I/flutter (17672): sdlu conver_refactor LiveInfoDealer 491 0 1
I/flutter (17672): sdlu conver_refactor ProductInfoDealer 805 0 0
I/flutter (17672): sdlu conver_refactor MarkInfoDealer 1410 0 2

这是我的 Database_Helper 类:

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

import 'model.dart';

class DatabaseHelper {
  DatabaseHelper._privateConstructor();

  static final DatabaseHelper instance = DatabaseHelper._privateConstructor();

  static Database? _database;

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

  Future<Database> _initDatabase() async {
    Directory documentsDirectory = await getApplicationDocumentsDirectory();
    String path = join(documentsDirectory.path, 'DSA_Database.db');
    return await openDatabase(
      path,
      version: 1,
      onCreate: _onCreate,
    );
  }

  Future _onCreate(Database db, int version) async {
    await rootBundle.load(join('assets', 'DSA_Database.db'));
  }

  Future<List<Titles>> getTitles() async {
    Database db = await instance.database;
    var titles = await db.query('Links');
    List<Titles> titlesList =
        titles.isNotEmpty
            ? titles.map((e) => Titles.fromMap(e)).toList()
            : [];
    return titlesList;
  }
}

数据库已添加到 pubspec.yaml 文件中

assets:
    - assets/DSA_Database.db

我对 SQLite 很陌生,所以我不能 100% 知道我是否做得对。

是的,我仔细检查了表格的命名。

提前致谢!

【问题讨论】:

    标签: flutter sqflite


    【解决方案1】:

    _onCreate 中,如果该表不存在,您应该创建该表 喜欢这段代码

     Future _onCreate(Database db, int version) async {
        await rootBundle.load(join('assets', 'DSA_Database.db'));
        await db.execute("CREATE TABLE IF NOT EXISTS Links(id id INTEGER PRIMARY KEY , <your Title class Properties>)");
      }
    

    当您尝试获取数据而不是 var titles = await db.query('Links');

    这样做var titles = await db.query('SELECT * FROM Links');

    我希望这对你有用, 您还必须检查this

    【讨论】:

    • 有了这个解决方案,我得到了一个新的错误——但它现在是唯一的错误。 E/SQLiteLog(15193): (1) near "SELECT": syntax error 并且由于该错误,我的应用程序中仍然没有数据
    • 可以用var titles = await db.rawQuery('SELECT * FROM Links');代替var titles = await db.query('SELECT * FROM Links');
    • 再次感谢您的帮助!但现在又出现了第一个错误:E/SQLiteLog(30029): (1) no such table: Links
    猜你喜欢
    • 2019-04-07
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2020-01-19
    • 2021-10-12
    • 2021-09-25
    • 2020-11-10
    相关资源
    最近更新 更多