postgresql - How do I use the postgres crate to receive table modification events from PostgreSQL?
问题描述
I have a Rust process dealing with a PostgreSQL database. This process communicates with a server process to pass on the details using MPSC channels. I am stuck on how to listen for and process events for table modification.
I tried using tokio-postgres but it seems there is an issue with it on Windows.
解决方案
在 PostgreSQL 的特定频道上监听事件如下:-
// Establish connection with database.
let url = "postgresql://root:root1234@127.0.01/test";
let conn = Connection::connect(url, TlsMode::None).unwrap();
// Listen for events on channel 'myevent'.
conn.execute("LISTEN myevent", &[]).expect("Could not send LISTEN");
let notifications = conn.notifications();
let mut it = notifications.blocking_iter();
println!("Waiting for notifications...");
loop {
let a = it.next();
match a {
Ok(Some(b)) => {
println!("{:?}", b);
},
Err(e) => println!("Got error {:?}", e),
_ => panic!("Unexpected operation!!!")
}
}
推荐阅读
- python - 比较 JSON 文件中的多个字典?
- javascript - window.open() 每次从 Firefox 响应式视图返回 null
- c# - 将内联添加到 TextBox 非常慢
- wpf - 单击 Datagrid 中的特定行后,将数据从 DataGrid 传递到相应的文本框
- javascript - 如何在拖动到指定的 div 时删除 Gridstack.js 小部件
- r - 用节点对构建树
- android - 使用指纹和密码
- ios - 我在 iPhone 上安装了应用程序,没问题。该应用程序没有错误,但不能用作 QR 阅读器
- java - 从 CSR 文件中获取 SSL 证书
- haskell - 在 Dependent Haskell 中证明 m + (1 + n) == 1+ (m + n)