-
Notifications
You must be signed in to change notification settings - Fork 3
/
testsql.go
54 lines (43 loc) · 1.14 KB
/
testsql.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
package main
import (
"database/sql"
"errors"
//"fmt"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
"log"
)
// Print Debug info to stdout
var Debug bool = true
func TestSQL() (err error) {
drivername := "postgres"
dsn := "user=golang password=golang dbname=golang sslmode=disable"
//dialect := gorp.PostgresDialect{}
//drivername := "mysql"
//dsn := "golang:golang@/golang"
//dialect := gorp.MySQLDialect{"InnoDB", "UTF8"}
// connect to db using standard Go database/sql API
//db, err := sql.Open("mysql", "golang:golang@/golang")
//db, err := sql.Open("postgres", "user=golang password=golang dbname=golang sslmode=disable")
db, err := sql.Open(drivername, dsn)
if err != nil {
return errors.New("sql.Open failed: " + err.Error())
}
// Open doesn't open a connection. Validate DSN data:
if err = db.Ping(); err != nil {
return errors.New("db.Ping failed: " + err.Error())
}
postid := "t3_36qf3s"
_, err = db.Query("SELECT * FROM posts WHERE postid=:postid", postid)
if err != nil {
log.Fatal(err)
}
return
}
func main() {
err := TestSQL()
if err != nil {
log.Fatalln("Failed TestSQL: ", err)
panic(err)
}
}