Golang Simple package based on go-sql-driver and additions and deletions

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

Build a simple test table


CREATE TABLE ' Test ' (' a ' int (one) not null auto_increment, ' b ' int (one) DEFAULT NULL, PRIMARY KEY (' a ')) Engine=innodb a uto_increment=157


Package Mainimport ("Database/sql" "Fmt" _ "Github.com/go-sql-driver/mysql") func main () {db, err: = SQL. Open ("MySQL", "Root: @tcp (127.0.0.1:3306)/test?charset=utf8") if err! = Nil {panic (err. Error ())}defer db. Close () rows, _: = Fetchrows (db, "SELECT * from Test") for _, V: = Range *rows {fmt. Println (v["A"], v["B"])}fmt. PRINTLN (Insert (DB, "INSERT into Test (b) VALUES (?)", 1)) row, _: = Fetchrow (db, "select * from Test where a =?", 1) fmt. Println (*row)}//inserts a func insert (db *sql. DB, sqlstr string, args ... interface{}) (Int64, error) {stmtins, err: = db. Prepare (SQLSTR) if err! = Nil {panic (err. Error ())}defer stmtins.close () result, err: = Stmtins.exec (args ...) If err! = Nil {panic (err. Error ())}return result. Lastinsertid ()}//modifies and removes the Func exec (db *sql. DB, sqlstr string, args ... interface{}) (Int64, error) {stmtins, err: = db. Prepare (SQLSTR) if err! = Nil {panic (err. Error ())}defer stmtins.close () result, err: = Stmtins.exec (args ...) If err! = Nil {panic (err. Error ())}return result. Rowsaffected ()}//take a row of data, note that suchThe result of the extraction is the string func fetchrow (db *sql. DB, sqlstr string, args ... interface{}) (*map[string]string, error) {stmtout, err: = db. Prepare (SQLSTR) if err! = Nil {panic (err. Error ())}defer stmtout.close () rows, err: = Stmtout.query (args ...) If err! = Nil {panic (err. Error ())}columns, err: = rows. Columns () if err! = Nil {panic (err. Error ())}values: = Make ([]sql. Rawbytes, Len (columns)) Scanargs: = Make ([]interface{}, Len (values)) RET: = Do (map[string]string, Len (Scanargs)) for I: = Range values {scanargs[i] = &values[i]}for rows. Next () {err = rows. Scan (Scanargs ...) If err! = Nil {panic (err. Error ())}var value stringfor I, col: = range values {if Col = = Nil {value = "NULL"} else {value = string (col)}ret[columns[ I]] = Value}break//get the first row Only}return &ret, nil}//take multiple lines, <span style= "font-family:arial, Helvetica, sans- serif; " > Note that this type of result is a string </span>func fetchrows (db *sql. DB, sqlstr string, args ... interface{}) (*[]map[string]string, error) {stmtout, err: = db. Prepare (SQLSTR) if Err! = Nil {panic (err. Error ())}defer stmtout.close () rows, err: = Stmtout.query (args ...) If err! = Nil {panic (err. Error ())}columns, err: = rows. Columns () if err! = Nil {panic (err. Error ())}values: = Make ([]sql. Rawbytes, Len (columns)) Scanargs: = Make ([]interface{}, Len (values)) RET: = Do ([]map[string]string, 0) for I: = Range Valu es {Scanargs[i] = &values[i]}for rows. Next () {err = rows. Scan (Scanargs ...) If err! = Nil {panic (err. Error ())}var value Stringvmap: = Make (Map[string]string, Len (Scanargs)) for I, col: = range values {if Col = = Nil {value = "NULL"} else {value = string (col)}vmap[columns[i]] = Value}ret = Append (ret, vmap)}return &ret, nil}


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.