Go & SQLite on Windows

來源:互聯網
上載者:User

標籤:ofo   rom   編譯錯誤   prompt   com   enc   check   strong   測試程式   

一般golang使用的sqlite驅動包都是github.com/mattn/go-sqlite3,但是官方並沒有直接支援windows平台的編譯,因為windows平台編譯預設需要gcc支援

其實解決辦法很簡單,只需要在windows平台下安裝gcc即可正常使用。

編譯錯誤如下:

go get github.com/mattn/go-sqlite3# github.com/mattn/go-sqlite3exec: "gcc": executable file not found in %PATH%

 

下面說明如何解決:

1. 下載GCC  http://tdm-gcc.tdragon.net/download

2. 安裝GCC

 

 

3. 開啟MinGW Command Prompt 安裝sqlite3

go get github.com/mattn/go-sqlite3

 

4. 安裝好就可以測試程式了

package mainimport (    "database/sql"    "fmt"    _ "github.com/mattn/go-sqlite3")func main() {    //1. Open connection    db, err := sql.Open("sqlite3", ":memory:")    checkErr(err)    defer db.Close()    //2. fail-fast if can‘t connect to DB    checkErr(db.Ping())    //3. create table    _, err = db.Exec("create table USER (ID integer PRIMARY KEY, NAME string not null); delete from USER;")    checkErr(err)    //4. insert data    //4.1 Begin transaction    tx, 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()    for i := 0; i < 10; i++ {        _, err = stmt.Exec(i, fmt.Sprint("user-", i))        checkErr(err)    }    //4.3 Commit transaction    tx.Commit()    //5. Query data    rows, err := db.Query("select * from USER")    checkErr(err)    defer rows.Close()    //5.1 Iterate through result set    for rows.Next() {        var name string        var id int        err := 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 iteration    err = rows.Err()    checkErr(err)}func checkErr(err error, args ...string) {    if err != nil {        fmt.Println("Error")        fmt.Println("%q: %s", err, args)    }}

 

Go & SQLite on Windows

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.