我想使用dart的postgresql驅動程序執行以下兩個插入操作。 下面的代碼工作!postgresql打開連接並執行多個命令
INSERT INTO posts(
title, description, posted_at, last_edited, "user",
editor, up_votes, down_votes, flag, links_to)
VALUES ('test title', 'test description', now(), now(), 'dartUser', 'dartUser',
0, 0, 'healthy', 'http://google.com');
INSERT INTO atatched_tags(
post_id, tag)
VALUES (currval('posts_post_id_seq'), 'testTag');
第二次插入只能在給予dart方法一個可選參數時完成。 我現在的代碼看起來像這樣。
addPost(Map post_values, [Map tag_values]){
Connection conn;
connect(uri)
.then((_conn){
conn = _conn;
})
.then((_){
conn.execute('''insert into posts(title, description, posted_at, last_edited, "user", editor, up_votes, down_votes, flag, links_to)
values(@title, @description, now(), now(), @user, @editor, @upVotes, @downVotes, @flag, @links_to)''', post_values)
.catchError((err){
print('Execute error in addPost: $err');
})
.then((_){
if(tag_values != null){
_addTagToPost(conn, tag_values);
}
})
.whenComplete(() => conn.close());
})
.catchError((err) => print('Error in addPost: $err'));
}
_addTagToPost(Connection conn, Map values) {
print('trying to add tag to attached tags');
conn.execute("insert into atatched_tags values(currval('posts_post_id_seq'), @tag)", values)
.catchError((err){
print('Execute error in addTagToPost: $err');
});
}
我運行該方法如下。
dbUtil.addPost({'title': 'Sample title',
'description': 'This is a description for a sample post',
'user': 'dartUser', 'editor': 'dartUser', 'upVotes': 0,
'downVotes': 0, 'flag': 'healthy', 'links_to':
'http://google.com'}, {'tag':'testTag'});
dbUtil
是上述兩種方法駐留的類的一個實例。
該代碼不會引發任何錯誤。但即使它寫入控制檯trying to add tag to attached tags
,也不會執行第二次插入操作(表中未添加任何行)。第一個按預期工作。
任何幫助,非常感謝。
編輯:如果有人想在_addTagToPost
函數中添加多個標籤,那麼這個代碼可能是有用的。 編輯2:下面的代碼是不正確我離開它,但檢查接受的答案中的意見。
Future _addTagToPost(Connection conn, List<Map> values) {
var completer = new Completer();
values.forEach((value){
conn.execute("insert into attached_tags values(currval('posts_post_id_seq'), @tag)", value)
.catchError((err){
print('Execute error in addTagToPost: $err');
});
});
return completer.future;
}
我更新了我的答案來完成。 –