forked from minus5/gofreetds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (58 loc) · 1.29 KB
/
main.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
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
"gofreetds"
"os"
)
func main() {
//create connection pool
connStr := os.Getenv("GOFREETDS_CONN_STR")
if connStr == "" {
panic("Set connection string for the pubs database in GOFREETDS_CONN_STR environment variable!\n")
}
pool, err := freetds.NewConnPool(connStr)
if err != nil {
panic(err)
}
defer pool.Close()
//get connection
conn, err := pool.Get()
if err != nil {
panic(err)
}
defer conn.Close()
//execute query
rst, err := conn.Exec("select au_id, au_lname, au_fname from authors")
if err != nil {
panic(err)
}
printResult(rst[0])
auId := "111-11-1111"
//insert row
rst, err = conn.ExecuteSql("insert into authors (au_id, au_lname, au_fname, contract) values (?, ?, ?, ?)",
auId, "Pero", "Zdero", 1)
if err != nil {
panic(err)
}
fmt.Printf("inserted %d row(s)\n", rst[0].RowsAffected)
//delete row
rst, err = conn.ExecuteSql("delete from authors where au_id = ?", auId)
if err != nil {
panic(err)
}
fmt.Printf("deleted %d row(s)\n", rst[0].RowsAffected)
}
func printResult(rst *freetds.Result) {
//print query result, columns
for _, c := range rst.Columns {
fmt.Printf("%-20s", c.Name)
}
fmt.Printf("\n")
//rows
for _, row := range rst.Rows {
for _, value := range row {
fmt.Printf("%-20v", value)
}
fmt.Printf("\n")
}
}