我正在尝试在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%知道我做得对不对。
是的,我仔细检查了表的命名。
提前感谢!
发布于 2021-09-30 12:39:04
在_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
https://stackoverflow.com/questions/69383473
复制相似问题