-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbr.go
56 lines (49 loc) · 1.25 KB
/
dbr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"database/sql"
"fmt"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/gocraft/dbr/v2"
)
func newDBConnection() *dbr.Connection {
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8",
"root", "123456", "127.0.0.1:3380", "tms")
if dbConnection, openErr := dbr.Open("mysql", dsn, nil); openErr != nil {
panic(openErr)
} else {
dbConnection.SetMaxIdleConns(2)
dbConnection.SetMaxOpenConns(10)
dbConnection.SetConnMaxLifetime(6 * time.Second)
return dbConnection
}
}
func main() {
db := newDBConnection()
fmt.Println(db)
sess := db.NewSession(nil)
fmt.Println(sess)
var id int64
sess.Select("instant_room_ct").From("ebk_daily_rate").Where("daily_rate_id = ?", 12454428).Load(&id)
fmt.Println(id)
res, err := sess.Update("ebk_daily_rate").
Set("instant_room_ct", 12).
Set("status", 3).
Set("update_date", time.Now()).
Where("daily_rate_id = ?", 12454428).Exec()
if err != nil {
panic(err)
}
rows, _ := res.RowsAffected()
fmt.Println(rows)
}
func sqlx() {
db, err := sql.Open("mysql", "root:123456@tcp(127.0.0.1:3380))")
if err != nil {
panic(err)
}
db.SetMaxIdleConns(2)
db.SetMaxOpenConns(10)
db.SetConnMaxLifetime(6 * time.Second)
fmt.Println(db)
}