db.pg
安装postgres数据库
sudo dnf install postgresql-server postgresql-contrib
sudo systemctl enable postgresql
sudo systemctl start postgresqlbrew install postgresqlC客户端库
brew install libpq使用pg库
pub struct Config { //数据库连接配置结构体
pub:
host string
user string
password string
dbname string
}
pub struct DB { //DB对象
mut:
conn &C.PGconn
}
pub struct Row { //查询结果集的行
pub mut:
vals []string
}
pub fn connect(config pg.Config) !DB //连接数据库函数,返回DB对象
//然后就是DB的各种方法:
pub fn (db DB) exec(query string) []pg.Row //执行SQL语句
pub fn (db DB) exec_one(query string) !pg.Row //执行SQL语句,返回结果的第一行
pub fn (db DB) exec_param(query string, param string) []pg.Row //带1个参数
pub fn (db DB) exec_param2(query string, param, param2 string) []pg.Row //带2个参数
pub fn (db DB) exec_param_many(query string, params []string) []pg.Row //带多个参数libpg客户端库使用参考
最后更新于