Golang using SQLite

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Installation issues

When you import SQLite, the Golang build has the following error,

exec: "gcc": executable file not found in %PATH%

The reason is that Sqlitle3 is a CGO library and needs to GCD compile C code
Then download and install TDM-GCC (WINDOSW version) download address: Http://tdm-gcc.tdragon.net/download

Using Sqlite3 in Golang

"JS
Package Main

Import (
"Database/sql"
"FMT"
_ "Github.com/mattn/go-sqlite3"
)

Func Main () {
DB, err: = SQL. Open ("Sqlite3", "./foo.db")
Checkerr (ERR)

//Insert Data stmt, err: = db. Prepare ("INSERT into UserInfo (username, departname, created) VALUES (?,?,?)") Checkerr (Err) res, err: = stmt. Exec ("Astaxie", "Development department", "2012-12-09") Checkerr (ERR) ID, err: = Res. Lastinsertid () Checkerr (err) fmt. PRINTLN (ID)//Update data stmt, err = db. Prepare ("Update userinfo set username=?") where uid=? ") Checkerr (Err) res, err = stmt. Exec ("astaxieupdate", id) checkerr (ERR) affect, err: = Res. Rowsaffected () Checkerr (err) fmt. PRINTLN (affect)//query data rows, err: = db. Query ("SELECT * from UserInfo") Checkerr (Err) for rows. Next () {var uid int var username string var department string var created string err = rows. Scan (&uid, &username, &department, &created) checkerr (err) fmt. PRINTLN (UID) fmt. PRINTLN (username) fmt. PRINTLN (department) FMT. Println (created)}//delete data 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)
}
}
```

More visit: Red Elephant ued

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.