使用golang将数据插入到postgres中
我暂时插入了一行文字试试看。
package main
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
)
const (
host = "localhost"
port = 5432
user = "postgres"
password = "1234"
dbname = "sample"
)
func main() {
psqlInfo := fmt.Sprintf("host=%s user=%s password=%s dbname=%s sslmode=disable", host,user,password,dbname)
fmt.Println(psqlInfo)
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
defer db.Close()
sqlStatement := `
INSERT INTO account (id) values (30)`
_, err = db.Exec(sqlStatement)
if err != nil {
panic(err)
}
}
使用 db.Query 进行多行的 Select 操作。
参考:使用Go的sql包查询多个记录
[Go语言] 尝试使用database/sql包。