golang postgresql CRUD

來源:互聯網
上載者:User

標籤:drive   dbn   close   val   com   package   word   pac   sel   

 

package mainimport (    "database/sql"    "fmt"    "log"    _ "github.com/lib/pq" // postgres driver)// People - databasetype People struct {    id   int    name string    age  int}type appContext struct {    db *sql.DB}// ConnectDB connect specify databasefunc connectDB(driverName string, dbName string) (c *appContext, errorMessage string) {    db, err := sql.Open(driverName, dbName)    if err != nil {        return nil, err.Error()    }    if err = db.Ping(); err != nil {        return nil, err.Error()    }    return &appContext{db}, ""}// Createfunc (c *appContext) Create() {    // get insert id    lastInsertId := 0    err := c.db.QueryRow("INSERT INTO users(name,age) VALUES($1,$2) RETURNING id", "jack", 22).Scan(&lastInsertId)    if err != nil {        log.Fatal(err)    }    fmt.Println("inserted id is ", lastInsertId)}// Readfunc (c *appContext) Read() {    rows, err := c.db.Query("SELECT * FROM users")    if err != nil {        fmt.Println(err.Error())        return    }    defer rows.Close()    for rows.Next() {        p := new(People)        err := rows.Scan(&p.id, &p.name, &p.age)        if err != nil {            fmt.Println(err)        }        fmt.Println(p.id, p.name, p.age)    }}// UPDATEfunc (c *appContext) Update() {    stmt, err := c.db.Prepare("UPDATE users SET age = $1 WHERE id = $2")    if err != nil {        log.Fatal(err)    }    result, err := stmt.Exec(10, 1)    if err != nil {        log.Fatal(err)    }    affectNum, err := result.RowsAffected()    if err != nil {        log.Fatal(err)    }    fmt.Println("update affect rows is ", affectNum)}// DELETEfunc (c *appContext) Delete() {    stmt, err := c.db.Prepare("DELETE FROM users WHERE id = $1")    if err != nil {        log.Fatal(err)    }    result, err := stmt.Exec(1)    if err != nil {        log.Fatal(err)    }    affectNum, err := result.RowsAffected()    if err != nil {        log.Fatal(err)    }    fmt.Println("delete affect rows is ", affectNum)}// Mypg - Postgresql CRUDfunc main() {    c, err := connectDB("postgres", "user=user1 password=password1 dbname=exampledb")    defer c.db.Close()    if err != "" {        print(err)    }    c.Create()    fmt.Println("add action done!")    c.Read()    fmt.Println("get action done!")    c.Update()    fmt.Println("update action done!")    c.Delete()    fmt.Println("delete action done!")}

 

golang postgresql CRUD

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.