sqflite

622 views
Skip to first unread message

barak taya

unread,
Oct 11, 2020, 4:35:31 PM10/11/20
to Flutter Development (flutter-dev)
anyone knows how to write without future & async, every time I want to call a function from that object I would love to start connection & end before the return.
Can't find any guides online.

Suzuki Tomohiro

unread,
Oct 11, 2020, 6:49:41 PM10/11/20
to Flutter Development (flutter-dev)
I don’t think that’s possible. Any Dart code that requires IO needs to use async/await (or “then” method in Future).

On Sun, Oct 11, 2020 at 16:35 barak taya <bara...@gmail.com> wrote:
anyone knows how to write without future & async, every time I want to call a function from that object I would love to start connection & end before the return.
Can't find any guides online.

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/6778e521-6438-4b04-a1af-8c89205b97f5n%40googlegroups.com.

barak taya

unread,
Oct 11, 2020, 6:51:20 PM10/11/20
to Flutter Development (flutter-dev)
about the X.db file, do I need to create one of my own or the system creates me one?

Suzuki Tomohiro

unread,
Oct 11, 2020, 7:14:27 PM10/11/20
to Flutter Development (flutter-dev)
> about the X.db file

I’m not sure what “X.db file“ you’re referencing. Would you share the documentation you’re reading?


barak taya

unread,
Oct 11, 2020, 7:25:14 PM10/11/20
to Flutter Development (flutter-dev)

class DBContacts {
   static final _databaseName = "phonebook.db";
   static final _databaseVersion = 1;
   static final table = 'contacts';
   static final columnName = 'name';
   static final columnServer = 'server';

   DBContacts._privateConstructor();
   static final DBContacts instance = DBContacts._privateConstructor();

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

   _initDatabase() async {
      Directory documentsDirectory = await getApplicationDocumentsDirectory();
      String path = join(documentsDirectory.path, _databaseName);
      return await openDatabase(path,
         version: _databaseVersion,
         onCreate: _onCreate);
}

   Future _onCreate(Database db, int version) async {
      await db.execute('''
         CREATE TABLE $table (
            $columnName TEXT NOT NULL,
            $columnServer TEXT NOT NULL,
         )
      ''');
   }

   Future<int> insert(Map<String, dynamic> row) async {
      Database db = await instance.database;
      return await db.insert(table, row);
   }

   Future<List<Map<String, dynamic>>> queryAllRows() async {
      Database db = await instance.database;
      return await db.query(table);
   }
}

Suzuki Tomohiro

unread,
Oct 11, 2020, 7:32:46 PM10/11/20
to barak taya, Flutter Development (flutter-dev)
The code takes care of creating the phonebook.db file at:

openDatabase(path,
         version: _databaseVersion,
         onCreate: _onCreate);


The onCreate parameter is executed when phonebook.db is not there yet.

barak taya

unread,
Oct 11, 2020, 7:33:37 PM10/11/20
to Flutter Development (flutter-dev)
E/SQLiteLog(30936): (1) table contacts has no column named server
E/flutter (30936): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: DatabaseException(table contacts has no column named server (code 1 SQLITE_ERROR): , while compiling: INSERT INTO contacts (name, server) VALUES (?, ?)) sql 'INSERT INTO contacts (name, server) VALUES (?, ?)' args [Bob, something]}

Suzuki Tomohiro

unread,
Oct 11, 2020, 7:42:45 PM10/11/20
to Flutter Development (flutter-dev)
It seems the phonebook.db was created already but without “server” column. You want to delete the phonebook.db file and try again.

barak taya

unread,
Oct 11, 2020, 7:44:26 PM10/11/20
to Flutter Development (flutter-dev)

I have no clue how to delete this file :(

barak taya

unread,
Oct 11, 2020, 7:49:57 PM10/11/20
to Flutter Development (flutter-dev)
FOUND: "await deleteDatabase(path);"
thanks for the help so far ♥

barak taya

unread,
Oct 11, 2020, 8:07:36 PM10/11/20
to Flutter Development (flutter-dev)
OK, so I'm not done yet, new issue, after deleting with what I wrote before it gives:
"E/flutter (16571): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: NoSuchMethodError: The method 'query' was called on null.".
Which is great!, it is deleted!.
But... new issue has appeared, what I changed is in here:

_initDatabase() async {
   Directory documentsDirectory = await getApplicationDocumentsDirectory();
   String path = join(documentsDirectory.path, _databaseName);
   //await deleteDatabase(path);  //delete file incase of error

   return await openDatabase(path,
      version: _databaseVersion,
      onCreate: _onCreate);
}

Future _onCreate(Database db, int version) async {
   await db.execute('''CREATE TABLE $table ($columnName TEXT NOT NULL, $columnServer TEXT NOT NULL,);''');
}

gives me this error:
E/SQLiteLog(16571): (1) near ")": syntax error
I/flutter (16571): error DatabaseException(near ")": syntax error (code 1 SQLITE_ERROR): , while compiling: CREATE TABLE contacts (name TEXT NOT NULL, server TEXT NOT NULL,);) sql 'CREATE TABLE contacts (name TEXT NOT NULL, server TEXT NOT NULL,);' args []} during open, closing...
E/flutter (16571): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: DatabaseException(near ")": syntax error (code 1 SQLITE_ERROR): , while compiling: CREATE TABLE contacts (name TEXT NOT NULL, server TEXT NOT NULL,);) sql 'CREATE TABLE contacts (name TEXT NOT NULL, server TEXT NOT NULL,);' args []}

Souvik Dutta Chowdhury

unread,
Oct 11, 2020, 9:50:47 PM10/11/20
to barak taya, Flutter Development (flutter-dev)
I guess the syntax error is because you have an extra ";" in the db.execute() command. What you have is:- await db.execute('''CREATE TABLE $table ($columnName TEXT NOT NULL, $columnServer TEXT NOT NULL,);''');
And what I think it will be is:- await db.execute('''CREATE TABLE $table ($columnName TEXT NOT NULL, $columnServer TEXT NOT NULL,)''');
That is getting rid of the ";" after 'NULL,)'.

barak taya

unread,
Oct 12, 2020, 7:07:03 AM10/12/20
to Flutter Development (flutter-dev)
Future _onCreate(Database db, int version) async {
   await db.execute('''CREATE TABLE $table ($columnName TEXT NOT NULL, $columnServer TEXT NOT NULL,)''');
}

gives same error:

E/SQLiteLog(18296): (1) near ")": syntax error
I/flutter (18296): error DatabaseException(near ")": syntax error (code 1 SQLITE_ERROR): , while compiling: CREATE TABLE contacts (name TEXT NOT NULL, server TEXT NOT NULL,)) sql 'CREATE TABLE contacts (name TEXT NOT NULL, server TEXT NOT NULL,)' args []} during open, closing...
E/flutter (18296): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: DatabaseException(near ")": syntax error (code 1 SQLITE_ERROR): , while compiling: CREATE TABLE contacts (name TEXT NOT NULL, server TEXT NOT NULL,)) sql 'CREATE TABLE contacts (name TEXT NOT NULL, server TEXT NOT NULL,)' args []}

So I tried to delete this ")" it writes is as an error:

Future _onCreate(Database db, int version) async {
   await db.execute('''CREATE TABLE $table ($columnName TEXT NOT NULL, $columnServer TEXT NOT NULL,''');
}

Same result:
E/SQLiteLog(18296): (1) incomplete input
I/flutter (18296): error DatabaseException(incomplete input (code 1 SQLITE_ERROR): , while compiling: CREATE TABLE contacts (name TEXT NOT NULL, server TEXT NOT NULL,) sql 'CREATE TABLE contacts (name TEXT NOT NULL, server TEXT NOT NULL,' args []} during open, closing...
E/flutter (18296): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: DatabaseException(incomplete input (code 1 SQLITE_ERROR): , while compiling: CREATE TABLE contacts (name TEXT NOT NULL, server TEXT NOT NULL,) sql 'CREATE TABLE contacts (name TEXT NOT NULL, server TEXT NOT NULL,' args []}

Souvik Dutta Chowdhury

unread,
Oct 12, 2020, 8:53:28 PM10/12/20
to barak taya, Flutter Development (flutter-dev)
Well maybe the comma ","  before the ")" is the cause of the error. 

barak taya

unread,
Oct 13, 2020, 7:55:24 AM10/13/20
to Flutter Development (flutter-dev)
Doesn't work, I gave up on this.
Reply all
Reply to author
Forward
0 new messages