Golang Database Operations

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

Using go-sql-driver to manipulate MYQL databases

Package Mainimport ("Database/sql" _ "Github.com/go-sql-driver/mysql" "FMT") func main () {//Connect database, get connection handle db, err: = SQL. Open ("MySQL", "root:123456@tcp (localhost:3306)/huifang5?charset=utf8") if err! = Nil {panic (err. Error ())}defer db. Close ()//executes the SQL statement to return multiple rows of result set results, err: = db. Query ("select * from Hf_test where keshiid=?", 0) if err! = Nil {panic (err). Error ())}defer result. Close ()//scan required Container keys, _: = result. Columns ()             //Field name values: = Make ([][]byte, Len (keys))     //Field value scans: = Make ([]interface{}, Len (keys))//scan container for I: = Range scans {Scans[i] = &values[i]//handling of the container so that each field value can be collected after the scan}//loop out the data for each row var datas []map[string]stringfor Result. Next () {result. Scan (Scans ...) Data: = Make (map[string]string) for k, V: = Range values {key: = Keys[k]data[key] = string (v)}datas = append (datas, data)}f Mt. Println (Datas)}



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.