• golang与 postgresql简单的增删改查


    创建数据库和表如下

     创建表SQL语句

    CREATE TABLE userinfo
    (
    uid serial NOT NULL,
    username character varying(100) NOT NULL,
    departname character varying(500) NOT NULL,
    Created date,
    CONSTRAINT userinfo_pkey PRIMARY KEY (uid)
    )
    WITH (OIDS=FALSE);

    CREATE TABLE userdeatail
    (
    uid integer,
    intro character varying(100),
    profile character varying(100)
    )
    WITH(OIDS=FALSE);

    GOlang代码

    package main
    import (
    "database/sql"
    "fmt"
    _ "github.com/lib/pq"
    "log"
    "time"
    )

    const (
    host = "localhost"
    port = 5432
    user = "postgres"
    password = "atenobserve"
    dbname = "testDB"
    )
    type userinfo struct {
    uid int
    username string
    departname string
    Created string `sql:"type:timestamp"`

    }


    type appContext struct {
    db *sql.DB
    }
    func main() {
    c, err := connectDB()
    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!")


    }

    func connectDB() (c *appContext, errorMessage string) {

    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 {
    fmt.Println("连接数据库错误:"+err.Error())

    }else {

    fmt.Println("Successfully connected!")
    }

    err = db.Ping()
    if err != nil {

    fmt.Println("DBPing错误:"+err.Error())

    }else
    {
    fmt.Println("DBPingSuccess")

    }



    //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}, ""
    }

    // Create
    func (c *appContext) Create() {
    // get insert id
    lastInsertId := 0
    now_str:=time.Now().Format("2006-01-02 15:04:05")
    err := c.db.QueryRow("INSERT INTO userinfo(username,departname,Created) VALUES($1,$2,$3) RETURNING uid", "cruise", "软件部",now_str).Scan(&lastInsertId)
    if err != nil {
    log.Fatal(err)
    }
    fmt.Println("inserted id is ", lastInsertId)
    }

    // Read
    func (c *appContext) Read() {
    rows, err := c.db.Query("SELECT * FROM userinfo")

    if err != nil {
    fmt.Println(err.Error())
    return
    }
    defer rows.Close()

    for rows.Next() {
    p := new(userinfo)
    err := rows.Scan(&p.uid, &p.username, &p.departname,&p.Created)
    if err != nil {
    fmt.Println(err)
    }
    fmt.Println(p.uid, p.username, p.departname,p.Created)
    }
    }

    // UPDATE
    func (c *appContext) Update() {
    stmt, err := c.db.Prepare("UPDATE userinfo SET username = $1 WHERE uid = $2")
    if err != nil {
    log.Fatal(err)
    }
    result, err := stmt.Exec("jack", 1)
    if err != nil {
    log.Fatal(err)
    }
    affectNum, err := result.RowsAffected()
    if err != nil {
    log.Fatal(err)
    }
    fmt.Println("update affect rows is ", affectNum)
    }

    // DELETE
    func (c *appContext) Delete() {
    stmt, err := c.db.Prepare("DELETE FROM userinfo WHERE uid = $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)
    }

  • 相关阅读:
    [Practical Git] Clean up commits with git rebase
    [Practical Git] Show who changed a line last with git blame
    [Practical Git] Compare file changes with git diff
    [Practical Git] Filter commit history with git log arguments
    [Practical Git] Format commit history with git log arguments
    [Practical Git] Navigate git command pager output with Unix less commands
    [Practical Git] Switching between current branch and last checkout branch
    [ES6] ES6 Parameter Object Destructuring with Required Values
    [React] Higher Order Components (replaces Mixins)
    HDU 1242 Rescue (BFS(广度优先搜索))
  • 原文地址:https://www.cnblogs.com/Cruise-Yang/p/12626597.html
Copyright © 2020-2023  润新知