forked from xiaojiong/DhtCrawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dao.go
52 lines (44 loc) · 1.21 KB
/
Dao.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
package DhtCrawler
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
type Dao struct {
user string
password string
ip string
port int
database string
db *sql.DB
HashIns1 *sql.Stmt
HashIns2 *sql.Stmt
}
func NewDao(user string, password string, ip string, port int, database string) *Dao {
dao := new(Dao)
dao.user = user
dao.password = password
dao.ip = ip
dao.port = port
dao.database = database
dao.Init()
return dao
}
func (dao *Dao) Init() {
dns := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?timeout=90s&collation=utf8_general_ci", dao.user, dao.password, dao.ip, dao.port, dao.database)
db, err := sql.Open("mysql", dns)
if err != nil {
panic(err.Error()) // Just for example purpose. You should use proper error handling instead of panic
}
dao.db = db
HashIns1, err := dao.db.Prepare("INSERT INTO `hash1` (`id`, `hash`) VALUES (NULL, ?);")
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
dao.HashIns1 = HashIns1
HashIns2, err := dao.db.Prepare("INSERT INTO `hash2` (`id`, `hash`) VALUES (NULL, ?);")
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
dao.HashIns2 = HashIns2
}