Model 从数据库获取数据
在“Dart可伸缩应用程序开发”一书中,有一些例子可以使用它Model 从数据库获取数据,model,dart,rethinkdb,Model,Dart,Rethinkdb,在“Dart可伸缩应用程序开发”一书中,有一些例子可以使用它 class UserStore{ final List<User> users = new List(); User user; Rethinkdb rdb = new Rethinkdb(); Connection conn; // opening a connection to the database: openAndStore() { rdb.c
class UserStore{
final List<User> users = new List();
User user;
Rethinkdb rdb = new Rethinkdb();
Connection conn;
// opening a connection to the database:
openAndStore() {
rdb.connect(db: "test", port:9090, host: "127.0.0.1").
then((_conn) {
conn = _conn;
storeData(conn);
}).catchError(print);
}
storeData(conn) {
List jobsMap = new List();
for (user in users) {
var jobMap = user.toMap();
jobsMap.add(jobMap);
}
// storing data:
rdb.table("users").insert(jobsMap).run(conn)
.then((response)=>print('documents inserted'))
.catchError(print);
// close the database connection:
close();
}
openAndRead() {
rdb.connect(db: "test", port:9090, host: "127.0.0.1").then((_conn) {
conn = _conn;
readData(conn);
}).catchError(print);
}
// reading documents:
readData(conn) {
print("test3");
rdb.table("users").getAll("1,2,3").run(conn).then((results) {
processJob(results);
close();
});
}
// working with document data:
processJob(results) {
for (var row in results) {
// Refer to columns by nam:
print('${row.id}');
}
}
close() {
conn.close();
}
类用户存储{
最终列表用户=新列表();
用户;
RejectionDB rdb=新的RejectionDB();
连接接头;
//打开与数据库的连接:
openAndStore(){
连接(数据库:“测试”,端口:9090,主机:“127.0.0.1”)。
然后(_conn){
conn=_conn;
storeData(康涅狄格州);
}).catchError(打印);
}
storeData(康涅狄格州){
列表作业映射=新列表();
for(用户中的用户){
var jobMap=user.toMap();
作业地图添加(作业地图);
}
//存储数据:
表(“用户”).insert(jobsMap.run(conn)
.然后((响应)=>打印('插入文档')
.catchError(打印);
//关闭数据库连接:
close();
}
openAndRead(){
连接(数据库:“测试”,端口:9090,主机:“127.0.0.1”)。然后(_conn){
conn=_conn;
读取数据(康涅狄格州);
}).catchError(打印);
}
//阅读文件:
读取数据(康涅狄格州){
打印(“测试3”);
表(“用户”).getAll(“1,2,3”).run(conn)。然后((结果){
处理作业(结果);
close();
});
}
//使用文档数据:
processJob(结果){
for(结果中的var行){
//参考nam的专栏:
打印('${row.id}');
}
}
关闭(){
康涅狄格州关闭();
}
但在实践中,该过程不会导致死亡。尽管它被称为openAndRead()
catchError不起作用。
使用模型和数据库最简单的方法是什么?我不确定自己是否完全理解这个问题,但我建议使用Dart的
async/await
语法,因为这使代码更易于阅读
比如说
main() async {
Rethinkdb rdb = new Rethinkdb();
try {
// get the connection
Connection conn = await rdb.connect(port: 28015);
// insert a row into the users table
await rdb.table('users').insert({'a': 'b'}).run(conn);
// query the users table
Cursor c = await rdb.table('users').run(conn);
// grab all the data from the cursor
List rows = await c.toList();
print(rows);
// close the connection
conn.close();
} catch (e) {
print('catching exception $e');
}
}
您可以创建一个类来保存数据库连接,这样就不需要一直打开和关闭它
class Database {
Rethinkdb _rdb = new Rethinkdb();
Connection _conn;
connect(int port) async {
_conn = await _rdb.connect(port: port);
}
Future<List> fetch() async {
Cursor c = await _rdb.table('users').run(_conn);
return await c.toList();
}
close() {
_conn.close();
}
}
对于包开发人员来说,这可能值得一提。您会注意到,在上面的main()中,没有捕获异常
}).catchError((err) => throw new RqlDriverError(
"Could not connect to $_host:$_port. Error $err"));
return _completer.future;