sqlite与golang
package main import ( "database/sql" "fmt" "time" _ "github.com/mattn/go-sqlite3" ) func main() { //打开数据库,如果不存在,则创建 db, err := sql.Open("sqlite3", "./foo.db") checkErr(err) //创建表 sql_table := ` CREATE TABLE IF NOT EXISTS userinfo( uid INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR(64) NULL, departname VARCHAR(64) NULL, created DATE NULL ); ` db.Exec(sql_table) // insert stmt, err := db.Prepare("INSERT INTO userinfo(username, departname, created) values(?,?,?)") checkErr(err) res, err := stmt.Exec("wangshubo", "国务院", "2017-04-21") checkErr(err) id, err := res.LastInsertId() checkErr(err) fmt.Println(id) // update stmt, err = db.Prepare("update userinfo set username=? where uid=?") checkErr(err) res, err = stmt.Exec("wangshubo_new", id) checkErr(err) affect, err := res.RowsAffected() checkErr(err) fmt.Println(affect) // query rows, err := db.Query("SELECT * FROM userinfo") checkErr(err) var uid int var username string var department string var created time.Time for rows.Next() { err = rows.Scan(&uid, &username, &department, &created) checkErr(err) fmt.Println(uid) fmt.Println(username) fmt.Println(department) fmt.Println(created) } rows.Close() // delete 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() } func checkErr(err error) { if err != nil { panic(err) } }
1、包管理,apt-get install golang-glide 然后glide init,会自动产生glide.yaml,注意需要配置环境变量GOPATH,GOPATH可以是任意目录,但是init的时候会提示没有src目录,所以需要在GOPATH下面mkdir src文件夹。
2、glide get xxxx,然后将$GOPATH里面的包,cp到GOPATH的指定目录
原文:http://www.cnblogs.com/peterinblog/p/7833360.html