0%

golang使用数据库

golang使用数据库

QQ群:397745473

golang使用sqlite数据库

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
)

const (
DBFILENAME = "./Magboo.db"
)

db, err := sql.Open("sqlite3", DBFILENAME)
checkErr(err)
createDBSql := `CREATE TABLE if not exists MagbooUrl (
url TEXT PRIMARY KEY
UNIQUE,
ShellID TEXT(20),
User TEXT(20),
Price TEXT(20),
UserInfo TEXT(20),
Moz TEXT(20)
);`
_, err = db.Exec(createDBSql)
checkErr(err)



//// 插入数据
//stmt, err := db.Prepare("INSERT INTO userinfo(username, department, created) values(?,?,?)")
//checkErr(err)
//
//res, err := stmt.Exec("astaxie", "研发部门", "2012-12-09")
//checkErr(err)
//
//id, err := res.LastInsertId()
//checkErr(err)
//
//fmt.Println(id)
//// 更新数据
//stmt, err = db.Prepare("update userinfo set username=? where uid=?")
//checkErr(err)
//
//res, err = stmt.Exec("astaxieupdate", id)
//checkErr(err)
//
//affect, err := res.RowsAffected()
//checkErr(err)
//
//fmt.Println(affect)
//
//// 查询数据
//rows, err := db.Query("SELECT * FROM userinfo")
//checkErr(err)
//
//for rows.Next() {
// var uid int
// var username string
// var department string
// var created time.Time
// err = rows.Scan(&uid, &username, &department, &created)
// checkErr(err)
// fmt.Println(uid)
// fmt.Println(username)
// fmt.Println(department)
// fmt.Println(created)
//}
//
//// 删除数据
//stmt, err = db.Prepare("delete from userinfo where uid=?")
//checkErr(err)
//
//res, err = stmt.Exec(id)
//checkErr(err)
//
//affect, err = res.RowsAffected()
//checkErr(err)
//
//fmt.Println(affect)
//
//db.Close()


// 参考: https://my.oschina.net/u/4417839/blog/3691049

QQ群:397745473

欢迎关注我的其它发布渠道