Dart的数据库操作

释放双眼,带上耳机,听听看~!

连接数据库

Dart连接数据库需要先从Pub下载sqljocky包
我新建了一个数据库,内容如下

连接数据库的代码如下


1
2
3
4
5
6
7
8
9
1import 'package:sqljocky/sqljocky.dart';
2
3main(List<String> arguments) {
4  //创建一个连接池,host:连接地址,port:端口,user:用户名,password:密码,db:数据库名,max:最大并发数
5  var pool = new ConnectionPool(host: 'localhost',port: 3306, user: 'root', password: '123456', db: 'test_mysql', max: 5);
6  //执行一条SQL语句
7  pool.query("SELECT * FROM users");
8}
9

控制台如果没有报错,那就说明连接成功了

使用下标读取结果


1
2
3
4
5
6
7
8
9
10
11
12
1import 'package:sqljocky/sqljocky.dart';
2
3main(List<String> arguments) {
4  var pool = new ConnectionPool(host: 'localhost',port: 3306, user: 'root', password: '123456', db: 'test_mysql', max: 5);
5  pool.query("SELECT * FROM users").then((results) {
6    results.forEach((row) {
7      //使用下标查询结果
8      print('${row[1]},${row[3]}');
9    });
10  });
11}
12

执行代码,控制台输出如下

使用字段读取结果


1
2
3
4
5
6
7
8
9
10
11
12
1import 'package:sqljocky/sqljocky.dart';
2
3main(List<String> arguments) {
4  var pool = new ConnectionPool(host: 'localhost',port: 3306, user: 'root', password: '123456', db: 'test_mysql', max: 5);
5  pool.query("SELECT * FROM users").then((results) {
6    results.forEach((row) {
7      //使用字段查询结果
8      print('${row.name},${row.age}');
9    });
10  });
11}
12

执行代码,控制台输出如下

准备SQL语句执行一次


1
2
3
4
5
6
7
8
9
10
11
12
13
14
1import 'package:sqljocky/sqljocky.dart';
2
3main(List<String> arguments) {
4  var pool = new ConnectionPool(host: 'localhost',port: 3306, user: 'root', password: '123456', db: 'test_mysql', max: 5);
5  //准备一个SQL语句
6  pool.prepare('insert into users (name, age, email) values (?, ?, ?)').then((query) {
7    //执行SQL语句
8    query.execute(['咖啡', 22, 'kf@qq.com']).then((result) {
9      //一个插入语句的结果是空的,但是会有一个自动递增的id
10      print("新用户的ID:${result.insertId}");
11    });
12  });
13}
14

先查看一下控制台的输出

再看一下数据库

好了,我们成功插入了一条新数据

准备SQL语句批量执行


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1import 'package:sqljocky/sqljocky.dart';
2
3main(List<String> arguments) {
4  var pool = new ConnectionPool(host: 'localhost',port: 3306, user: 'root', password: '123456', db: 'test_mysql', max: 5);
5  pool.prepare('insert into users (name, age, email) values (?, ?, ?)').then((query) {
6    //批量执行SQL语句
7    query.executeMulti([['绿豆', 18, 'ld@qq.com'],
8                        ['红豆', 17, 'hd@qq.com'],
9                        ['青豆', 17, 'hd@qq.com']]).then((results) {
10      //使用结果列表
11      for (var result in results) {
12        print("新用户的ID:${result.insertId}");
13      }
14    });
15  });
16}
17

先看看控制台输出

再看看数据库

执行一个事务


1
2
3
4
5
6
7
8
9
10
11
12
13
1import 'package:sqljocky/sqljocky.dart';
2
3main(List<String> arguments) {
4  var pool = new ConnectionPool(host: 'localhost',port: 3306, user: 'root', password: '123456', db: 'test_mysql', max: 5);
5  //创建一个事务
6  pool.startTransaction().then((trans) {
7    trans.query("DELETE FROM users WHERE id = '6'").then((result) {
8      //提交事务
9      trans.commit();
10    });
11  });
12}
13

看看是否成功删除了id为6的用户

版权声明:本文为博主原创文章,未经博主允许不得转载。

给TA打赏
共{{data.count}}人
人已打赏
安全技术

C++异常

2022-1-11 12:36:11

安全技术

“让开发者爱上安全测试”系列之“源码安全测试”——开发者之伤

2016-12-26 13:18:02

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索