SQFlite 未找到现有 Table
SQFlite does not find existing Table
我正在尝试查询我数据库中 Table 名称为 'links' 的所有数据,但我总是收到此错误:
W/e.dsa_regelwik( 4691): Accessing hidden method Lsun/misc/Unsafe;->getUnsafe()Lsun/misc/Unsafe; (greylist,core-platform-api, linking, allowed)
W/e.dsa_regelwik( 4691): Accessing hidden method Lsun/misc/Unsafe;->objectFieldOffset(Ljava/lang/reflect/Field;)J (greylist,core-platform-api, linking, allowed)
W/e.dsa_regelwik( 4691): Accessing hidden method Lsun/misc/Unsafe;->compareAndSwapObject(Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z (greylist, linking, allowed)
W/e.dsa_regelwik( 4691): Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
E/SQLiteLog( 4691): (1) no such table: links
这是我的助手class,具有查询功能:
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 {
var databasesPath = await getDatabasesPath();
var path = join(databasesPath, 'DSA_Database.db');
await deleteDatabase(path);
ByteData data = await rootBundle.load(join('assets', 'DSA_Database.db'));
List<int> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
await new File(path).writeAsBytes(bytes, flush: true);
}
Future<List<Titles>> getTitles() async {
Database db = await instance.database;
var titles = await db.rawQuery('SELECT * FROM links');
List<Titles> titlesList =
titles.isNotEmpty
? titles.map((e) => Titles.fromMap(e)).toList()
: [];
return titlesList;
}
}
从 5 天以来我一直在尝试修复该错误,但我找不到该问题的任何解决方案。
您似乎正试图在打开数据库时删除它(即在 onCreate 期间),这将不起作用,因为此时数据库已经打开。在调用 openDatabase
.
之前,您应该在 _onCreate
中移动您当前正在做的事情
有关如何打开资产数据库的示例,请参阅 https://github.com/tekartik/sqflite/blob/master/sqflite/doc/opening_asset_db.md。
我正在尝试查询我数据库中 Table 名称为 'links' 的所有数据,但我总是收到此错误:
W/e.dsa_regelwik( 4691): Accessing hidden method Lsun/misc/Unsafe;->getUnsafe()Lsun/misc/Unsafe; (greylist,core-platform-api, linking, allowed)
W/e.dsa_regelwik( 4691): Accessing hidden method Lsun/misc/Unsafe;->objectFieldOffset(Ljava/lang/reflect/Field;)J (greylist,core-platform-api, linking, allowed)
W/e.dsa_regelwik( 4691): Accessing hidden method Lsun/misc/Unsafe;->compareAndSwapObject(Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z (greylist, linking, allowed)
W/e.dsa_regelwik( 4691): Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
E/SQLiteLog( 4691): (1) no such table: links
这是我的助手class,具有查询功能:
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 {
var databasesPath = await getDatabasesPath();
var path = join(databasesPath, 'DSA_Database.db');
await deleteDatabase(path);
ByteData data = await rootBundle.load(join('assets', 'DSA_Database.db'));
List<int> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
await new File(path).writeAsBytes(bytes, flush: true);
}
Future<List<Titles>> getTitles() async {
Database db = await instance.database;
var titles = await db.rawQuery('SELECT * FROM links');
List<Titles> titlesList =
titles.isNotEmpty
? titles.map((e) => Titles.fromMap(e)).toList()
: [];
return titlesList;
}
}
从 5 天以来我一直在尝试修复该错误,但我找不到该问题的任何解决方案。
您似乎正试图在打开数据库时删除它(即在 onCreate 期间),这将不起作用,因为此时数据库已经打开。在调用 openDatabase
.
_onCreate
中移动您当前正在做的事情
有关如何打开资产数据库的示例,请参阅 https://github.com/tekartik/sqflite/blob/master/sqflite/doc/opening_asset_db.md。