I am trying to query threw all the Data in my Database where the name of the Table is 'links', but i always get this Error:
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
This is my Helper class with the query-function:
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;
}
}
I am trying to fix that Error since 5 days now, but i can't find any solution for that problem.
CodePudding user response:
It seems you are trying to delete the database while opening it (i.e. during onCreate) which won't work because the database is already open at this point. You should move what you are currently doing in _onCreate
before calling openDatabase
.
See https://github.com/tekartik/sqflite/blob/master/sqflite/doc/opening_asset_db.md for an example on how to open an asset database.