-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
67 lines (57 loc) · 1.84 KB
/
db.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
67
package main
import (
"fmt"
"database/sql"
_ "github.com/go-sql-driver/mysql"
"github.com/coopernurse/gorp"
"gopkg.in/mgo.v2"
)
func initDb() *gorp.DbMap {
// connect to db using standard Go database/sql API
// use whatever database/sql driver you wish
//db, err := sql.Open("sqlite3", "/tmp/post_db.bin")
db, err := sql.Open("mysql", "root:1@unix(/var/run/mysqld/mysqld.sock)/mydb")
checkErr(err, "sql.Open failed")
// construct a gorp DbMap
// dbmap := &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}
//dbmap.TraceOn("[gorp]", log.New(os.Stdout, "myapp:", log.Lmicroseconds))
dbmap.AddTableWithName(Company{}, "company").SetKeys(true, "Id")
dbmap.AddTableWithName(Feedback{}, "feedbacks").SetKeys(true, "Id")
dbmap.AddTableWithName(FeedbackForm{}, "feedbacks").SetKeys(true, "Id")
// first check
var rs []string
dbmap.Select(&rs, "select company from myReport")
var c Company
dbmap.SelectOne(&c, "select * from company where id = ? ", 1)
fmt.Println("com1 ---- ", c)
// var alls []Report
// dbmap.Select(&alls, "select * from myReport order by id")
// for x, r := range alls {
// fmt.Printf("a --- %d, %v\n", x, r)
// }
// fmt.Println(alls)
for x, r := range rs {
fmt.Printf("b --- %d, %v\n", x, r)
}
fmt.Println("----- print table")
// -----
return dbmap
}
func initMgo() *mgo.Session{
session, err := mgo.Dial("127.0.0.1")
if err != nil { panic(err) }
session.SetMode(mgo.Monotonic, true)
return session
}
// this struct is for initdb test try operation, so put it here
type Company struct {
Id int64 `form:"Id"`
Name string `form:"Name"`
Person string `form:"Person"`
Phone string `form:"Phone"`
Time string
Operator string
Info string `form:"Info"`
Usedomain string `form:"Usedomain"`
}