How to connect mysql Database with Dart?

后端 未结 4 1215
滥情空心
滥情空心 2020-12-16 15:50

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 progr

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-16 16:39

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

提交回复
热议问题