Go語言中mysql資料庫操作(二)

來源:互聯網
上載者:User

標籤:Go語言中mysql資料庫操作   Go語言資料庫連接池   Go語言調用預存程序   

串連池

Go語言內建資料庫連接池,直接使用即可,非常方便。

func init() {    db, _ = sql.Open("mysql", "root:[email protected](127.0.0.1:3306)/betting?charset=utf8")    // 設定最大開啟的串連數,預設值為0表示不限制。    db.SetMaxOpenConns(2000)    // 用於設定閑置的串連數。    db.SetMaxIdleConns(1000)    db.Ping()}
  • 設定最大的串連數,可以避免並發太高導致串連mysql出現too many connections的錯誤。
  • 設定閑置的串連數則當開啟的一個串連使用完成後可以放在池裡等候下一次使用。
調用預存程序

我在使用Go調用預存程序的時候,怎樣擷取out參數裡的值,一直未找到正確的方法,所以只總結了使用in參數的方法。

預存程序

CREATE DEFINER = ‘root‘@‘localhost‘ PROCEDURE `proc_test`(        IN `in_id` INTEGER    )    NOT DETERMINISTIC    CONTAINS SQL    SQL SECURITY DEFINER    COMMENT ‘‘BEGIN    SELECT log_ip FROM admin_log WHERE id=in_id;END;

Go中調用

func main() {    db, err := sql.Open("mysql", "root:[email protected](127.0.0.1:3306)/betting?charset=utf8")    checkError("串連資料庫", err)    defer db.Close()    var outArg string    row := db.QueryRow("call proc_test(?)", 2000)    row.Scan(&outArg)    fmt.Println(outArg)}

執行結果

192.168.88.91

Go語言中mysql資料庫操作(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.