You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I'm trying to access the bytes of my sqlite database file. How do I do that on the web? xOpen and xRead are super confusing and I can't find an example.
Working none-web implementation
import'dart:async';
import'dart:io';
import'dart:typed_data';
import'package:path_provider/path_provider.dart';
import'package:sqlite3/common.dart';
import'package:sqlite3/sqlite3.dart';
Future<CommonDatabase> loadDb() async {
final applicationDir =awaitgetApplicationDocumentsDirectory();
final file =File(applicationDir.path +'/foobar.db');
return sqlite3.open(file.path);
}
Future<Uint8List> dbBytes() async {
final applicationDir =awaitgetApplicationDocumentsDirectory();
final file =File(applicationDir.path +'/foobar.db');
return file.readAsBytes();
}
Not working web implementation
import'dart:async';
import'dart:io';
import'dart:typed_data';
import'package:sqlite3/wasm.dart';
Future<CommonDatabase> loadDb() async {
final sqlite =awaitWasmSqlite3.loadFromUrl(Uri.parse('sqlite3.wasm'));
final fileSystem =awaitIndexedDbFileSystem.open(dbName:'foobar');
sqlite.registerVirtualFileSystem(fileSystem, makeDefault:true);
return sqlite.open('foobar.db', mode:OpenMode.readWriteCreate);
}
Future<Uint8List> dbBytes() async {
final fileSystem =awaitIndexedDbFileSystem.open(dbName:'foobar');
final open = fileSystem.xOpen(
Sqlite3Filename('foobar.db'), SqlFlag.SQLITE_OPEN_READONLY);
print(open.file);
return (open.file asFile).readAsBytes();
}
Thanks!
The text was updated successfully, but these errors were encountered:
We're not using dart:io files on the web and instead mirror structures used by sqlite3 internally, which is also the reason the methods are named the way they are. So the way to go is to xOpen the file as you're doing it, and then call xRead with a Uint8List that you've allocated (you can use xFileSize() to get the current size) to read bytes from the file into a buffer. Then you just need to xClose() the file after you're done. I didn't test this, but I imagine something like this should work:
Future<Uint8List> dbBytes() async {
final fileSystem =awaitIndexedDbFileSystem.open(dbName:'foobar');
final open = fileSystem.xOpen(
Sqlite3Filename('foobar.db'), SqlFlag.SQLITE_OPEN_READONLY);
final buffer =Uint8List(open.xFileSize());
file.xRead(buffer, 0); // read from offset 0 (entire file)
file.xClose();
fileSystem.close();
}
Hi, I'm trying to access the bytes of my sqlite database file. How do I do that on the web? xOpen and xRead are super confusing and I can't find an example.
Working none-web implementation
Not working web implementation
Thanks!
The text was updated successfully, but these errors were encountered: