Pro-Test available Golang SQL routines and package management

Source: Internet
Author: User
Tags prepare stmt

SQLite and Golang

Package Mainimport ("Database/sql" "FMT" "Time" _ "Github.com/mattn/go-sqlite3") func main () {//Open database, if not saved In, then create DB, err: = SQL.  Open ("Sqlite3", "./foo.db") Checkerr (ERR)//CREATE TABLE sql_table: = ' CREATE table IF not EXISTS userinfo (UID INTEGER PRIMARY KEY autoincrement, username varchar (+) NULL, Departname varchar (+) NULL, created D    ATE NULL); ' Db. Exec (sql_table)//Insert stmt, err: = db.    Prepare ("INSERT into UserInfo (username, departname, created) VALUES (?,?,?)") Checkerr (Err) res, err: = stmt. Exec ("Wangshubo", "State Department", "2017-04-21") Checkerr (ERR) ID, err: = Res. Lastinsertid () Checkerr (err) fmt. PRINTLN (ID)//update stmt, err = db. Prepare ("Update userinfo set username=?")    where uid=? ") Checkerr (Err) res, err = stmt. Exec ("wangshubo_new", id) checkerr (ERR) affect, err: = Res. Rowsaffected () Checkerr (err) fmt. PRINTLN (affect)//query rows, err: = db. Query ("SELECT * from userInfo ") Checkerr (ERR) var uid int var username string var department string var created time. Time for rows. Next () {err = rows. Scan (&uid, &username, &department, &created) checkerr (err) fmt. PRINTLN (UID) fmt. PRINTLN (username) fmt. PRINTLN (department) FMT. Println (created)} rows. Close ()//delete 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)}}

1, package management, Apt-get install golang-glide and then glide init, will automatically generate GLIDE.YAML, note the need to configure the environment variable Gopath,gopath can be any directory, but Init will be prompted without the SRC directory, so you need to mkdir the SRC folder under Gopath.

2, Glide get xxxx, then will $gopath inside the package, CP to gopath the specified directory

Pro-Test available Golang SQL routines and package management

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.