如何使用Dart连接mysql数据库?

hoa*_*ung 13 mysql database dart

有谁能告诉我如何用Dart连接到mysql数据库?我一直在阅读和搜索天,但找不到合适的答案.我只是在学习网络编程.谢谢!

Lar*_*ann 15

您可以使用SQLJocky连接到MySQL.加

dependencies:
  sqljocky: 0.0.4
Run Code Online (Sandbox Code Playgroud)

你的pubspec.yaml运行pub安装.现在你可以像这样连接MySQL

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);
    }
  });
});
Run Code Online (Sandbox Code Playgroud)


Nae*_*Nae 5

我认为对于dart 2 mysql1是一个简单的选择。

例:

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();
}
Run Code Online (Sandbox Code Playgroud)

(已在Dart版本2.1.0(版本2.1.0-dev.9.4 f9ebf21297)上进行了测试)


小智 0

我还没有尝试过这个,但这里有一个:http://github.com/jamesots/sqljocky