The SQLite driver package used by General Golang is github.com/mattn/go-sqlite3, but the official does not support the compilation of Windows platform directly, because the Windows platform compilation requires GCC support by default
In fact, the solution is simple, only need to install GCC under the Windows platform for normal use.
The compilation errors are as follows:
Go get github.com/mattn/go-sqlite3# github.com/mattn/go-"gcc" File in%PATH%
Here's how to fix it:
1. Download gcc http://tdm-gcc.tdragon.net/download
2. Installing GCC
3. Open MinGW Command Prompt installation sqlite3
Go get github.com/mattn/go-sqlite3
4. You can test the program by installing it.
Package Mainimport ("Database/sql" "FMT" _ "Github.com/mattn/go-sqlite3") Func main () {//1. Open Connectiondb, err:= SQL. Open ("Sqlite3",": Memory:") Checkerr (ERR) defer db. Close ()//2. Fail-fast if can ' t connect to DBCheckerr (db. Ping ())//3. CREATE TABLE_, Err= db. Exec ("CREATE TABLE USER (ID integer PRIMARY KEY, NAME string not null);") Checkerr (err)//4. Insert Data//4.1 Begin TransactionTX, Err: =db. Begin () Checkerr (err)//4.2 Prepare Insert stmt.stmt, err: = Tx. Prepare ("INSERT INTO USER (ID, NAME) VALUES (?,?)") Checkerr (err) defer stmt. Close () forI: =0; I <Ten; i++{_, Err= stmt. Exec (i, FMT. Sprint ("user-", i)) Checkerr (ERR)}//4.3 Commit transactionTx.commit ()//5. Query Datarows, err:= db. Query ("SELECT * from USER") Checkerr (ERR) defer rows. Close ()//5.1 Iterate through result set forrows. Next () {varNamestring varIdintERR:= Rows. Scan (&id, &name) Checkerr (err) fmt. Printf ("id=%d, name=%s\n", ID, name)} //5.2 Check error, if any, that were encountered during iterationErr =rows. Err () Checkerr (err)}func Checkerr (err error, args ...string) { ifErr! =Nil {fmt. Println ("Error") fmt. Println ("%q:%s", err, args)}}
Go & SQLite on Windows