Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect mysql Database with Dart?

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!

like image 220
hoantrung Avatar asked Aug 15 '12 02:08

hoantrung


People also ask

How do I connect to a MySQL database?

To Connect to a MySQL Database Expand the Drivers node from the Database Explorer. Right-click the MySQL (Connector/J driver) and choose Connect Using.... The New Database Connection dialog box is displayed. In the Basic Setting tab, enter the Database's URL <HOST>:<PORT>/<DB> in the corresponding text field.

Can we connect flutter with MySQL?

Load the data from mysql to the Flutter DataTable widget by fetching the data from mysql and convert it to JSON data. Then convert the JSON data to list collection. And then, create the rows for the datagrid from the list collection.


2 Answers

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);
    }
  });
});
like image 131
Lars Tackmann Avatar answered Oct 03 '22 09:10

Lars Tackmann


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))

like image 27
Nae Avatar answered Oct 03 '22 08:10

Nae