tekartik / sqflite

SQLite flutter plugin
BSD 2-Clause "Simplified" License
2.86k stars 521 forks source link

After desktop build application not open #945

Open waulite-786 opened 1 year ago

waulite-786 commented 1 year ago

I created msix build for windows desktop then application not open but if i remove sqlite and sqflite_common_ffi then application working perfectly

cliftonlabrum commented 1 year ago

I'm having this same issue. Did you ever find a solution?

cliftonlabrum commented 1 year ago

I think I figured it out. The app is failing to launch because it can't initialize SQLite on Windows. Here's what I did to fix it:

Step 1: Add sqlite3.dll to your project

Download the SQLite 3 Windows DLL you want.

Put the file in your Flutter project in: /windows/sqlite3.dll.

Step 2: Tweak CMakeLists

Add this line to /windows/CMakeLists.txt:

install(FILES sqlite3.dll DESTINATION "${CMAKE_INSTALL_PREFIX}" COMPONENT Runtime)

Step 3: Initialize the Database

Wherever you initialize your SQFlite database, set up the database factory for Windows (and Linux, if applicable):

import 'package:sqflite/sqflite.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';

class DB{
  //Setup ==========
  static setup() async{
    if (Platform.isWindows || Platform.isLinux) {
      sqfliteFfiInit();
      databaseFactory = databaseFactoryFfi;
      database = await databaseFactory.openDatabase('/path/to/database.db');
    }
  }
  //Open ==========
  static Future<Database> open() async {
    if (Platform.isWindows || Platform.isLinux) {
      //Windows
      return await databaseFactory.openDatabase(Paths.database);
    } else {
      //macOS, iOS, and Android
      return await openDatabase(Paths.database);
    }
  }
}

Step 4: Use Your Database

You can then open and use your database in your app elsewhere:

final db = await DB.open();

final results await db.rawQuery('SELECT * FROM whatever');

Hopefully that helps.

waulite-786 commented 1 year ago

thanks Tekartik/Sqflite

Problem resolved, when I downloaded sqlte3 and put it runner folder

On Thu, Apr 20, 2023 at 1:35 AM Clifton Labrum @.***> wrote:

I think I figured it out. The app is failing to launch because it can't initialize SQLite on Windows. Here's what I did to fix it: Step 1: Add sqlite3.dll to your project

Download the SQLite 3 Windows DLL https://www.sqlite.org/download.html you want.

Put the file in your Flutter project in: /windows/sqlite3.dll. Step 2: Tweak CMakeLists

Add this line to /windows/CMakeLists.txt:

install(FILES sqlite3.dll DESTINATION "${CMAKE_INSTALL_PREFIX}" COMPONENT Runtime)

Step 3: Initialize DB

Wherever you initialize your SQFlite database, set up the database factory for Windows (and Linux, if applicable):

import 'package:sqflite/sqflite.dart';import 'package:sqflite_common_ffi/sqflite_ffi.dart'; class DB{ //Setup ========== static setup() async{ if (Platform.isWindows || Platform.isLinux) { sqfliteFfiInit(); databaseFactory = databaseFactoryFfi; database = await databaseFactory.openDatabase('/path/to/database.db'); } } //Open ========== static Future open() async { if (Platform.isWindows || Platform.isLinux) { //Windows return await databaseFactory.openDatabase(Paths.database); } else { //macOS, iOS, and Android return await openDatabase(Paths.database); } } }

Step 4: Use Your Database

You can then open and use your database in your app elsewhere:

final db = await DB.open(); final results await db.rawQuery('SELECT * FROM whatever');

Hopefully that helps.

— Reply to this email directly, view it on GitHub https://github.com/tekartik/sqflite/issues/945#issuecomment-1515310817, or unsubscribe https://github.com/notifications/unsubscribe-auth/APA77QSIDPO6XSBZICFY3J3XCBAR5ANCNFSM6AAAAAAVA24IOE . You are receiving this because you authored the thread.Message ID: @.***>

cliftonlabrum commented 1 year ago

@waulite-786 Great! Be sure to mark this issue as closed. 😊

waulite-786 commented 1 year ago

Awesome, thanks!

On Thu, Apr 20, 2023 at 9:50 AM Clifton Labrum @.***> wrote:

@waulite-786 https://github.com/waulite-786 Great! Be sure to mark this issue as closed. 😊

— Reply to this email directly, view it on GitHub https://github.com/tekartik/sqflite/issues/945#issuecomment-1515691405, or unsubscribe https://github.com/notifications/unsubscribe-auth/APA77QWQ5MTSITGOTGZT5UTXCC2RFANCNFSM6AAAAAAVA24IOE . You are receiving this because you were mentioned.Message ID: @.***>

ayoubzulfiqar commented 1 year ago

SQFlite on Windows

Step - 1

Remember to Download Windows Binary Download SQLite DDL FIle

Path

Add the DLL File to this path sqlpath

Code

class SQFLite {
  Database? _database;

  Future<Database> get database async {
    if (_database != null) {
      return _database!;
    }
    _database = await initDB();
    return _database!;
  }

  Future<Database> initDB() async {
    if (Platform.isWindows || Platform.isLinux) {
      sqfliteFfiInit();
      final databaseFactory = databaseFactoryFfi;
      final appDocumentsDir = await getApplicationDocumentsDirectory();
      final dbPath = join(appDocumentsDir.path, "databases", "data.db");
      final winLinuxDB = await databaseFactory.openDatabase(
        dbPath,
        options: OpenDatabaseOptions(
          version: 1,
          onCreate: _onCreate,
        ),
      );
      return winLinuxDB;
    } else if (Platform.isAndroid || Platform.isIOS) {
      final documentsDirectory = await getApplicationDocumentsDirectory();
      final path = join(documentsDirectory.path, "data.db");
      final iOSAndroidDB = await openDatabase(
        path,
        version: 1,
        onCreate: _onCreate,
      );
      return iOSAndroidDB;
    }
    throw Exception("Unsupported platform");
  }

// based on what type of data you want to add your Table
  Future<void> _onCreate(Database database, int version) async {
    final db = database;
    await db.execute(""" CREATE TABLE IF NOT EXISTS users(
              id INTEGER PRIMARY KEY,
              name TEXT,
              email TEXT,
              password INTEGER,
              phoneNumber INTEGER
            )
  """);
  }
}

Initialization

Either you can write this in the main function or if you are working on separate file You can add it inside initState()

  void initState() {
    super.initState();
    WidgetsFlutterBinding.ensureInitialized();
    helper.initWinDB();
  }

Github Code

xujianfen commented 8 months ago

I don't understand why not use plugin_bundled_libraries directly to complete it, This should not be imported by the user

Itzarvind-2024 commented 5 months ago

I think I figured it out. The app is failing to launch because it can't initialize SQLite on Windows. Here's what I did to fix it:

Step 1: Add sqlite3.dll to your project

Download the SQLite 3 Windows DLL you want.

Put the file in your Flutter project in: /windows/sqlite3.dll.

Step 2: Tweak CMakeLists

Add this line to /windows/CMakeLists.txt:

install(FILES sqlite3.dll DESTINATION "${CMAKE_INSTALL_PREFIX}" COMPONENT Runtime)

Step 3: Initialize the Database

Wherever you initialize your SQFlite database, set up the database factory for Windows (and Linux, if applicable):

import 'package:sqflite/sqflite.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';

class DB{
  //Setup ==========
  static setup() async{
    if (Platform.isWindows || Platform.isLinux) {
      sqfliteFfiInit();
      databaseFactory = databaseFactoryFfi;
      database = await databaseFactory.openDatabase('/path/to/database.db');
    }
  }
  //Open ==========
  static Future<Database> open() async {
    if (Platform.isWindows || Platform.isLinux) {
      //Windows
      return await databaseFactory.openDatabase(Paths.database);
    } else {
      //macOS, iOS, and Android
      return await openDatabase(Paths.database);
    }
  }
}

Step 4: Use Your Database

You can then open and use your database in your app elsewhere:

final db = await DB.open();

final results await db.rawQuery('SELECT * FROM whatever');

Hopefully that helps.

Its not working Can you give more explanation on this.