16

Does anyone can tell me how to connect to mysql database with Dart? I've been reading and searching for days but can't find any suitable answers. I just learning web programming. Thank you!

1
  • This problem I've solved recently. I just created a server with NodeJS, then I connect my flutter app yo mysql through a rest API. Commented Feb 10, 2020 at 20:29

4 Answers 4

15

You can use SQLJocky to connect to MySQL. Add

dependencies:
  sqljocky: 0.0.4

to your pubspec.yaml an run pub install. Now you can connect to MySQL like this

var cnx = new Connection();
cnx.connect(username, password, dbName, port, hostname).then((nothing) {
    // Do something with the connection
    cnx.query("show tables").then((Results results) {
    print("tables");
    for (List row in results) {
      print(row);
    }
  });
});
Sign up to request clarification or add additional context in comments.

Comments

9

I think for dart 2 mysql1 is a simple choice.

Example:

import 'package:mysql1/mysql1.dart';

Future main() async {
  // Open a connection (testdb should already exist)
  final connection = await MySqlConnection.connect(new ConnectionSettings(
      host: '10.0.2.2',
      port: 3306,
      user: 'root',
      password: '0123456789',
      db: 'development',
      ));
  var results = await connection.query('select * from tableName');
  for (var row in results) {
    print('${row[0]}');
  }

  // Finally, close the connection
  await connection.close();
}

(tested on Dart version 2.1.0 (build 2.1.0-dev.9.4 f9ebf21297))

1 Comment

how to connect with SSH?
0

I haven't tried this, but here is one: http://github.com/jamesots/sqljocky

Comments

0

You can try using sqljocky -> http://pub.dartlang.org/packages/sqljocky

1 Comment

Mine is giving me late initialization error have you tackle with this error, when you connect to MySQL connection it gives you late initialization or null error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.