安裝
go get github.com/mattn/go-sqlite3
sqlite3 建立 測試 table
CREATE TABLE a(id integer primary key autoincrement,a1,a2,a3);
範例
package main
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
)
func checkErr(err error) {
if err != nil {
panic(err)
}
}
func main() {
db, err := sql.Open("sqlite3", "./foo.db")
stmt, err := db.Prepare("INSERT INTO a(a1, a2, a3) values(?,?,?)")
checkErr(err)
res, err := stmt.Exec("a1", "a2", "a3")
checkErr(err)
id, err := res.LastInsertId()
checkErr(err)
fmt.Println("id=", id);
rows, err := db.Query("SELECT * FROM a")
checkErr(err)
var uid int
var a1,a2,a3 string
for rows.Next() {
err = rows.Scan(&uid, &a1, &a2, &a3)
checkErr(err)
fmt.Println(uid, a1,a2,a3)
}
}
沒有留言:
張貼留言