有人能告诉我如何使用Dart连接到mysql数据库吗?我已经阅读和搜索了好几天,但没有找到合适的答案。我刚学习Web编程。谢谢!
dependencies:
sqljocky: 0.0.4
将内容添加到您的pubspec.yaml,然后运行pub install。现在您可以像这样连接到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);
}
});
});
我认为对于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();
}
(测试版本为Dart 2.1.0(构建版本为2.1.0-dev.9.4 f9ebf21297))
你可以尝试使用sqljocky -> http://pub.dartlang.org/packages/sqljocky