-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
37 lines (33 loc) · 794 Bytes
/
main.go
File metadata and controls
37 lines (33 loc) · 794 Bytes
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
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
func main() {
testConnString("root@tcp(127.0.0.1:4000)/")
testConnString("root@tcp(127.0.0.1:4000)/test")
testConnString("root@tcp(127.0.0.1:4000)/1234.test")
testConnString("root@tcp(127.0.0.1:4000)/42.test")
testConnString("root@tcp(127.0.0.1:4000)/42")
}
func testConnString(connStr string) {
defer fmt.Println("--")
fmt.Printf("connect with '%s'\n", connStr)
db, err := sql.Open("mysql", connStr)
if err != nil {
fmt.Println("open error:", err)
return
}
defer db.Close()
rows, err := db.Query("SELECT DATABASE();")
if err != nil {
fmt.Println("query error:", err)
return
}
defer rows.Close()
rows.Next()
var dbName string
rows.Scan(&dbName)
fmt.Println("using db:", dbName)
}