sql
最后更新于:2022-04-02 02:43:09
[TOC]
## 初始化
conn.go
```
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
var (
dbConn *sql.DB
err error
)
func init() {
dbConn, err = sql.Open("mysql", "root:12345678@tcp(localhost:3306)/video_server?charset=utf8")
if err != nil {
panic(err)
}
dbConn.SetMaxIdleConns(2) //保持连接的最大空闲连接数
dbConn.SetMaxOpenConns(5) //最大连接数,设置过大会出现 to many connect
dbConn.SetMaxIdleConns(7 * time.Hour) //闲置连接的最大存活时间,0 永不过期
err = dbConn.Ping()
if err != nil {
panic(err)
}
}
```
## 增
```
stmt, e := dbConn.Prepare(`INSERT INTo sessions (session_id,TTL,login_name) values(?,?,?)`)
defer stmt.Close()
_, e = stmt.Exec(sid, ttlstr, uname)
```
## 查单条
```
stmt, e := dbConn.Prepare(`SELECT TTL,login_name FROM sessions where session_id=?`)
defer stmt.Close()
e = stmt.QueryRow(sid).Scan(&ss.TTL, &ss.UserName)
if e != nil && e != sql.ErrNoRows {
return nil, e
}
```
## 查多条
```
stmt, e := dbConn.Prepare("select video_id from video_del_rec limit=?")
var ids []string
rows, e := stmt.Query(count)
for rows.Next() {
var id string
if e := rows.Scan(&id); e != nil {
return ids, e
}
ids = append(ids, id)
}
```
';