最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Golang連接并操作PostgreSQL數(shù)據(jù)庫基本操作

 更新時間:2022年09月16日 11:45:58   作者:天使手兒  
PostgreSQL是常見的免費的大型關(guān)系型數(shù)據(jù)庫,具有豐富的數(shù)據(jù)類型,也是軟件項目常用的數(shù)據(jù)庫之一,下面這篇文章主要給大家介紹了關(guān)于Golang連接并操作PostgreSQL數(shù)據(jù)庫基本操作的相關(guān)資料,需要的朋友可以參考下

前言:

本篇文章對如何使用golang連接并操作postgre數(shù)據(jù)庫進行了簡要說明。文中使用到的主要工具:DBeaver21、VSCode,Golang1.17。

以用戶,文章,評論三個表作為例子,下面是數(shù)據(jù)庫建表sql:

CREATE TABLE public.user_info (
    u_id serial4 NOT NULL,
    user_name varchar NULL,
    create_time date NULL,
    CONSTRAINT user_info_pk PRIMARY KEY (u_id)
);
CREATE TABLE public.user_info (
    u_id serial4 NOT NULL,
    user_name varchar NULL,
    create_time date NULL,
    CONSTRAINT user_info_pk PRIMARY KEY (u_id)
);
CREATE TABLE public."comment" (
    c_id serial4 NOT NULL,
    "content" varchar NULL,
    CONSTRAINT comment_pk PRIMARY KEY (c_id)
);

連接數(shù)據(jù)庫

連接postgre數(shù)據(jù)庫的驅(qū)動有很多,我們選用了github.com/lib/pq。下面看連接的方法。我們引入pq包時使用了_進行匿名加載,而不是直接使用驅(qū)動包。在對數(shù)據(jù)庫的操作仍然是使用自帶的sql包。另外,postgre默認使用的是public模式(schema),我們創(chuàng)建的表也是在這個模式下的??梢灾苯釉跀?shù)據(jù)庫中修改默認模式或者在連接url中添加currentSchema=myschema來指定默認的模式,當然也可以在sql中使用myschema.TABLE來指定要訪問的模式。

package main

import (
    "database/sql"
    "fmt"

    _ "github.com/lib/pq"
)

var db *sql.DB

func DbOpen() {
    var err error
    //參數(shù)根據(jù)自己的數(shù)據(jù)庫進行修改
    db, err = sql.Open("postgres", "host=localhost port=5432 user=angelhand password=2222 dbname=ahdb sslmode=disable")
    checkError(err)
    err = db.Ping()
    checkError(err)
}

sql.DB

需要注意的是,sql.DB并不是數(shù)據(jù)庫連接,而是一個go中的一個數(shù)據(jù)結(jié)構(gòu):

type DB struct {
    // Atomic access only. At top of struct to prevent mis-alignment
    // on 32-bit platforms. Of type time.Duration.
    waitDuration int64 // Total time waited for new connections.

    connector driver.Connector
    // numClosed is an atomic counter which represents a total number of
    // closed connections. Stmt.openStmt checks it before cleaning closed
    // connections in Stmt.css.
    numClosed uint64

    mu           sync.Mutex // protects following fields
    freeConn     []*driverConn
    connRequests map[uint64]chan connRequest
    nextRequest  uint64 // Next key to use in connRequests.
    numOpen      int    // number of opened and pending open connections
    // Used to signal the need for new connections
    // a goroutine running connectionOpener() reads on this chan and
    // maybeOpenNewConnections sends on the chan (one send per needed connection)
    // It is closed during db.Close(). The close tells the connectionOpener
    // goroutine to exit.
    openerCh          chan struct{}
    closed            bool
    dep               map[finalCloser]depSet
    lastPut           map[*driverConn]string // stacktrace of last conn's put; debug only
    maxIdleCount      int                    // zero means defaultMaxIdleConns; negative means 0
    maxOpen           int                    // <= 0 means unlimited
    maxLifetime       time.Duration          // maximum amount of time a connection may be reused
    maxIdleTime       time.Duration          // maximum amount of time a connection may be idle before being closed
    cleanerCh         chan struct{}
    waitCount         int64 // Total number of connections waited for.
    maxIdleClosed     int64 // Total number of connections closed due to idle count.
    maxIdleTimeClosed int64 // Total number of connections closed due to idle time.
    maxLifetimeClosed int64 // Total number of connections closed due to max connection lifetime limit.

    stop func() // stop cancels the connection opener.
}

在拿到sql.DB時并不會創(chuàng)建新的連接,而可以認為是拿到了一個數(shù)據(jù)庫連接池,只有在執(zhí)行數(shù)據(jù)庫操作(如Ping()操作)時才會自動生成一個連接并連接數(shù)據(jù)庫。在連接操作執(zhí)行完畢后應該及時地釋放。此處說的釋放是指釋放連接而不是sql.DB連接,通常來說一個sql.DB應該像全局變量一樣長期保存,而不要在某一個小函數(shù)中都進行Open()Close()操作,否則會引起資源耗盡的問題。

增刪改查

下面代碼實現(xiàn)對數(shù)據(jù)簡單的增刪改查操作。

插入數(shù)據(jù)

func insert() {
    stmt, err := db.Prepare("INSERT INTO user_info(user_name,create_time) VALUES($1,$2)")
    if err != nil {
        panic(err)
    }

    res, err := stmt.Exec("ah", time.Now())
    if err != nil {
        panic(err)
    }

    fmt.Printf("res = %d", res)
}

使用Exec()函數(shù)后會返回一個sql.Result即上面的res變量接收到的返回值,它提供了LastInserId() (int64, error)RowsAffected() (int64, error)分別獲取執(zhí)行語句返回的對應的id和語句執(zhí)行所影響的行數(shù)。

更新數(shù)據(jù)

func update() {
    stmt, err := db.Prepare("update user_info set user_name=$1 WHERE u_id=$2")
    if err != nil {
        panic(err)
    }
    res, err := stmt.Exec("angelhand", 1)
    if err != nil {
        panic(err)
    }

    fmt.Printf("res = %d", res)
}

查詢數(shù)據(jù)

結(jié)構(gòu)體如下:

type u struct {
    id          int
    user_name   string
    create_time time.Time
}

接下來是查詢的代碼

func query() {
    rows, err := db.Query("select u_id, user_name, create_time from user_info where user_name=$1", "ah")
    if err != nil {
        panic(err)

    }
    //延遲關(guān)閉rows
    defer rows.Close()

    for rows.Next() {
        user := u{}
        err := rows.Scan(&user.id, &user.user_name, &user.create_time)
        if err != nil {
            panic(err)
        }
        fmt.Printf("id = %v, name = %v, time = %v\n", user.id, user.user_name, user.create_time)
    }
}

可以看到使用到的幾個關(guān)鍵函數(shù)rows.Close(),rows.Next(),rows.Scan()。其中rows.Next()用來遍歷從數(shù)據(jù)庫中獲取到的結(jié)果集,隨用用rows.Scan()來將每一列結(jié)果賦給我們的結(jié)構(gòu)體。

需要強調(diào)的是rows.Close()。每一個打開的rows都會占用系統(tǒng)資源,如果不能及時的釋放那么會耗盡系統(tǒng)資源。defer語句類似于java中的finally,功能就是在函數(shù)結(jié)束前執(zhí)行后邊的語句。換句話說,在函數(shù)結(jié)束前不會執(zhí)行后邊的語句,因此在耗時長的函數(shù)中不建議使用這種方式釋放rows連接。如果要在循環(huán)中重發(fā)查詢和使用結(jié)果集,那么應該在處理完結(jié)果后顯式調(diào)用rows.Close()。

db.Query()實際上等于創(chuàng)建db.Prepare(),執(zhí)行并關(guān)閉之三步操作。

還可以這樣來查詢單條記錄:

err := db.Query("select u_id, user_name, create_time from user_info where user_name=$1", "ah").Scan(&user.user_name)

刪除數(shù)據(jù)

func delete() {
    stmt, err := db.Prepare("delete from user_info where user_name=$1")
    if err != nil {
        panic(err)
    }
    res, err := stmt.Exec("angelhand")
    if err != nil {
        panic(err)
    }

    fmt.Printf("res = %d", res)
}

總結(jié)

到此這篇關(guān)于Golang連接并操作PostgreSQL數(shù)據(jù)庫基本操作的文章就介紹到這了,更多相關(guān)Golang連接操作PostgreSQL內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • go語言編程之select信道處理示例詳解

    go語言編程之select信道處理示例詳解

    這篇文章主要為大家介紹了go語言編程之select信道處理示例詳解,<BR>有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪
    2022-04-04
  • golang切片原理詳細解析

    golang切片原理詳細解析

    這篇文章主要介紹了golang切片原理詳細解析,切片在編譯時定義為Slice結(jié)構(gòu)體,并通過NewSlice()函數(shù)進行創(chuàng)建,更多相關(guān)內(nèi)容感興趣的小伙伴可以參考一下下面文章內(nèi)容
    2022-06-06
  • Go語言-為什么返回值為接口類型,卻返回結(jié)構(gòu)體

    Go語言-為什么返回值為接口類型,卻返回結(jié)構(gòu)體

    這篇文章主要介紹了Go語言返回值為接口類型,卻返回結(jié)構(gòu)體的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • Go prometheus metrics條目自動回收與清理方法

    Go prometheus metrics條目自動回收與清理方法

    這篇文章主要為大家介紹了Go prometheus metrics條目自動回收與清理方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • 使用Golang實現(xiàn)加權(quán)負載均衡算法的實現(xiàn)代碼

    使用Golang實現(xiàn)加權(quán)負載均衡算法的實現(xiàn)代碼

    這篇文章主要介紹了使用Golang實現(xiàn)加權(quán)負載均衡算法的實現(xiàn)代碼,詳細說明權(quán)重轉(zhuǎn)發(fā)算法的實現(xiàn),通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • 關(guān)于golang高并發(fā)的實現(xiàn)與注意事項說明

    關(guān)于golang高并發(fā)的實現(xiàn)與注意事項說明

    這篇文章主要介紹了關(guān)于golang高并發(fā)的實現(xiàn)與注意事項說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-05-05
  • go install/build生成的文件命名和路徑操作

    go install/build生成的文件命名和路徑操作

    這篇文章主要介紹了go install/build生成的文件命名和路徑操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • Golang設計模式之外觀模式的實現(xiàn)

    Golang設計模式之外觀模式的實現(xiàn)

    這篇文章主要介紹了Golang設計模式之外觀模式的實現(xiàn),外觀模式是一種常用的設計模式之一,是一種結(jié)構(gòu)型設計模式,它提供了一個簡單的接口來訪問復雜系統(tǒng)的各種功能,從而降低了系統(tǒng)的復雜度,需要詳細了解可以參考下文
    2023-05-05
  • Go語言內(nèi)建函數(shù)cap的實現(xiàn)示例

    Go語言內(nèi)建函數(shù)cap的實現(xiàn)示例

    cap 是一個常用的內(nèi)建函數(shù),它用于獲取某些數(shù)據(jù)結(jié)構(gòu)的容量,本文主要介紹了Go語言內(nèi)建函數(shù)cap的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下
    2024-08-08
  • Golang?中的?strconv?包常用函數(shù)及用法詳解

    Golang?中的?strconv?包常用函數(shù)及用法詳解

    strconv是Golang中一個非常常用的包,主要用于字符串和基本數(shù)據(jù)類型之間的相互轉(zhuǎn)換,這篇文章主要介紹了Golang中的strconv包,需要的朋友可以參考下
    2023-06-06

最新評論

呼玛县| 惠来县| 肇东市| 修水县| 白朗县| 安康市| 江达县| 专栏| 昆山市| 府谷县| 澄江县| 宿迁市| 金湖县| 运城市| 嘉黎县| 敖汉旗| 芒康县| 颍上县| 郧西县| 桐乡市| 桑日县| 龙口市| 乌苏市| 景泰县| 朝阳市| 黄冈市| 三河市| 丰城市| 合川市| 静乐县| 安顺市| 南安市| 平罗县| 喀什市| 张家港市| 富锦市| 吴旗县| 遂宁市| 龙泉市| 聂荣县| 神农架林区|