EisGlockner
EisGlockner

Reputation: 23

Problems with loading existing SQFlite Database in Flutter app

I am trying to open a existing SQLite Database in my Flutter app but i always get that Error message in the Console when starting the App:

I/OpenGLRenderer(16880): Davey! duration=949ms; Flags=1, IntendedVsync=32058088200606, Vsync=32058088200606, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=32058101631102, AnimationStart=32058101709175, PerformTraversalsStart=32058101714227, DrawStart=32059029835685, SyncQueued=32059030411883, SyncStart=32059030862195, IssueDrawCommandsStart=32059030915893, SwapBuffers=32059035930789, FrameCompleted=32059037678966, DequeueBufferDuration=3920000, QueueBufferDuration=1405000, 
E/SQLiteLog(16880): (1) no such table: Links
E/SQLiteLog(16880): (1) no such table: Links
I/flutter (17672): sdlu conver_refactor MemberProfileDealer 17 1 1
I/flutter (17672): sdlu conver_refactor MessageDealer 45 0 1
I/flutter (17672): sdlu conver_refactor LiveInfoDealer 497 1 0
I/flutter (17672): sdlu conver_refactor ProductInfoDealer 810 0 0
I/flutter (17672): sdlu conver_refactor MarkInfoDealer 1511 0 3
I/flutter (17672): sdlu conver_refactor MemberProfileDealer 4 0 1
I/flutter (17672): sdlu conver_refactor MessageDealer 41 0 0
I/flutter (17672): sdlu conver_refactor ProductInfoDealer 823 0 0
I/flutter (17672): sdlu conver_refactor LiveInfoDealer 1095 0 2
I/flutter (17672): sdlu conver_refactor MarkInfoDealer 1404 0 5
I/flutter (17672): sdlu conver_refactor MemberProfileDealer 19 0 0
I/flutter (17672): sdlu conver_refactor MessageDealer 46 0 0
I/flutter (17672): sdlu conver_refactor LiveInfoDealer 491 0 1
I/flutter (17672): sdlu conver_refactor ProductInfoDealer 805 0 0
I/flutter (17672): sdlu conver_refactor MarkInfoDealer 1410 0 2

This is my Database_Helper 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 {
    await rootBundle.load(join('assets', 'DSA_Database.db'));
  }

  Future<List<Titles>> getTitles() async {
    Database db = await instance.database;
    var titles = await db.query('Links');
    List<Titles> titlesList =
        titles.isNotEmpty
            ? titles.map((e) => Titles.fromMap(e)).toList()
            : [];
    return titlesList;
  }
}

The Database is already added in the pubspec.yaml file

assets:
    - assets/DSA_Database.db

I am pretty new to SQLite so i don't 100% know if i am doing it right.

And Yes, i double checked the naming of the Table.

Thanks in advance!

Upvotes: 1

Views: 1094

Answers (1)

Mohamed Ayman
Mohamed Ayman

Reputation: 59

in the _onCreate you should create the table if not existed like this code

 Future _onCreate(Database db, int version) async {
    await rootBundle.load(join('assets', 'DSA_Database.db'));
    await db.execute("CREATE TABLE IF NOT EXISTS Links(id id INTEGER PRIMARY KEY , <your Title class Properties>)");
  }

and when you try to get the data instead of var titles = await db.query('Links');

do this var titles = await db.query('SELECT * FROM Links');

i hope this work for you , you also have to check this

Upvotes: 2

Related Questions