Go & SQLite on Windows

Source: Internet
Author: User
Tags sqlite stmt

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.