010-Go 操作PostgreSQL数据库2

1:sql脚本

create table post(
    id    serial primary key,
    content    text,
    author    varchar(100)
)

 

2:post.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package post
 
import(
    "fmt"
    "database/sql"
    _"github.com/lib/pq"
)
 
const(
    host = "192.168.72.128"
    port = 5432
    user = "test"
    password = "test"
    dbname = "testdb"
)
 
type Post struct{
    ID      int
    Content string
    Author  string
}
 
var Db *sql.DB
 
func init(){
    var err error
     
    psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
        "password=%s dbname=%s sslmode=disable",host, port, user, password, dbname)
 
    Db, err = sql.Open("postgres", psqlInfo)
    if err != nil{
        panic(err)
    }
}
 
func (post *Post) GetPosts(limit int) (posts []Post, err error){
    sql := "select id,content,author from post limit $1";
     
    rows, err := Db.Query(sql, limit)
    if err != nil{
        return
    }
 
    defer rows.Close()
 
    for rows.Next(){
        post := Post{}
        err = rows.Scan(&post.ID, &post.Content, &post.Author)
        if err != nil{
            return
        }
        posts = append(posts, post)
    }
     
    return
}
 
func (p *Post) GetPost(id int) (post Post, err error){
    post = Post{}
 
    sql := "select id,content,author from post where id=$1"
 
    err = Db.QueryRow(sql, id).Scan(&post.ID, &post.Content, &post.Author)
 
    return
}
 
func (post *Post) AddPost() (err error){
    sql := "insert into post(content,author) values($1,$2) returning id"
 
    stmt, err := Db.Prepare(sql)
    if err != nil{
        return
    }
 
    defer stmt.Close()
 
    err = stmt.QueryRow(post.Content, post.Author).Scan(&post.ID)
 
    return
}
 
func (post *Post) DelPost()(err error){
    sql := "delete from post where id=$1"
    _,err = Db.Exec(sql, post.ID)
    return
}
 
func (post *Post) EditPost() (err error){
    sql := "update post set content=$1 where id=$2"
    _,err = Db.Exec(sql, post.Content, post.ID)
    return
}

3:main.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main
 
import(
    "fmt"
    "Chapter02/post"
)
 
func main(){
    p := post.Post{
        Content: "Hello java!",
        Author: "王五",
    }
 
    fmt.Println(p)
 
    err := p.AddPost()
    if err != nil{
        panic(err)
    }
 
    fmt.Println(p)
 
    var posts = []post.Post{}
    posts, err = p.GetPosts(10)
    if err != nil{
        panic(err)
    }
    for _,post := range posts{
        fmt.Printf("ID:%d,Content:%s,Author:%s\n", post.ID, post.Content, post.Author)
    }
 
    pp, err := p.GetPost(p.ID)
    if err != nil{
        panic(err)
    }
    fmt.Println(pp)
}

  

posted @   yshy  阅读(205)  评论(0编辑  收藏  举报
编辑推荐:
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
阅读排行:
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· DeepSeek火爆全网,官网宕机?本地部署一个随便玩「LLM探索」
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 上周热点回顾(1.20-1.26)
· 【译】.NET 升级助手现在支持升级到集中式包管理
点击右上角即可分享
微信分享提示