Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pragma are not set when write inside SQL DB file. #1051

Open
EArminjon opened this issue Sep 27, 2023 · 0 comments
Open

Pragma are not set when write inside SQL DB file. #1051

EArminjon opened this issue Sep 27, 2023 · 0 comments

Comments

@EArminjon
Copy link

I created a DB and would like to put inside its scheme some pragma rules. On open, I should be able to use the database using theses pragma.

In the following example, i would like to set the following pragma inside my database sql file.

PRAGMA recursive_triggers = true;

After opening the database, the pragma is not set but when I manually set the pragma inside onConfigure it works... That mean, pragma are well skipped in this scenario.

Code example bellow :

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';

void main() => runApp(
      const MaterialApp(
        home: Home(),
      ),
    );

class Home extends StatefulWidget {
  const Home({super.key});

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  bool? pragmaEnabled;

  Future<void> fromDBFile() async {
    final Directory directory = await getApplicationDocumentsDirectory();
    const String dbName = "test.db";
    final String dbPath = "${directory.path}/$dbName";
    final File dbFile = File(dbPath);
    await dbFile.writeAsString(
      """
        PRAGMA recursive_triggers = true;
      """,
      mode: FileMode.writeOnly,
    );

    final Database db = await openDatabase(
      dbPath,
      version: 1,
      onCreate: (Database db, _) async {
        final List<Map<String, Object?>> result =
            await db.rawQuery("PRAGMA recursive_triggers;");
        print("TEST-fromDBFile: $result");
        pragmaEnabled = result.first['recursive_triggers'] == 1;
        setState(() {});
      },
    );
    await db.close();
  }

  Future<void> fromDBOnConfigure() async {
    final Directory directory = await getApplicationDocumentsDirectory();
    const String dbName = "test.db";
    final String dbPath = "${directory.path}/$dbName";
    final File dbFile = File(dbPath);
    await dbFile.writeAsString(
      "",
      mode: FileMode.writeOnly,
    );

    final Database db = await openDatabase(
      dbPath,
      version: 1,
      onConfigure: (Database db) async {
        await db.rawQuery("PRAGMA recursive_triggers = true;");
      },
      onCreate: (Database db, _) async {
        final List<Map<String, Object?>> result =
            await db.rawQuery("PRAGMA recursive_triggers;");
        print("TEST-fromDBOnConfigure: $result");
        pragmaEnabled = result.first['recursive_triggers'] == 1;
        setState(() {});
      },
    );

    await db.close();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Pragma issue"),
        centerTitle: true,
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            const SizedBox(height: 16),
            OutlinedButton(
              onPressed: fromDBFile,
              child: const Text("From DB file"),
            ),
            const SizedBox(height: 16),
            OutlinedButton(
              onPressed: fromDBOnConfigure,
              child: const Text("From onConfigure method"),
            ),
            const SizedBox(height: 16),
            Text("Pragma is set ? $pragmaEnabled"),
          ],
        ),
      ),
    );
  }
}

Screenshot :

Using the code example above you can run a demo app using the two methods to set the pragma :

  • from the sql db file
  • from the onConfigute callback
Capture d’écran 2023-09-27 à 14 49 46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant