Golang using Go-sql-driver to implement MySQL additions and deletions operation

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

These two days with Golang writes the service side to do all is interacts with the Redis, because later also will use MySQL's one table to do the verification. Learn the Golang to MySQL first!

Mymysql and Go-mysql-driver are two now popular go MySQL driver, read the following document, the latter comparison of sound, listen to the introduction of Daniel, looks like Go-mysql-driver more sound.

The installation configuration is still simple, just go get it.

All right, let's start with go-sql-driver and additions.

First Use Go-sql-driver link mysql!!!


A look at it, account: password @ Mode database, and the accompanying parameters

Python <textarea wrap="soft" class="crayon-plain print-no" data-settings="" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">User@unix (/path/to/socket)/dbnameroot:pw@unix (/tmp/mysql.sock)/mydatabase?loc=localuser:password@tcp ( localhost:5555)/dbname?tls=skip-verify&autocommit=true</textarea>
1234 User@Unix(/Path/ to/Socket)/dbnameRoot:PW@Unix(/tmp/MySQL.sock)/MyDatabase?Loc=LocalUser:Password@TCP(localhost:5555)/dbname?TLS=Skip-Verify&autocommit=true

A complete example!

Python Xiaorui.cc#xiaorui.ccpackage mainimport ("Database/sql"//This package must be referenced, is the underlying SQL driver "FMT" _ "github.com/go-sql-driver/ MySQL "" StrConv "//This is to convert int to string) func main () {//main function db, err: = SQL. Open ("MySQL", "Root: @tcp (localhost:3306)/dbname?charset=utf8")//database connection string, don't tell me I can't read it. Port must write/if err! = Nil {//connection succeeded err must be nil otherwise it is error panic (err. Error ())//Throw exception FMT. Println (Err. Error ())//Only show exception} defer db. Close ()///Only in front of use panic then defer can function, if the link data when the problem, he will write data to err Rows, err: = db. Query ("Select Id,lvs from Xiaorui")//To determine if Err has incorrect data, there is err data to display panic data if err! = Nil {panic (err. Error ()) fmt. Println (Err. Error ()) return} defer rows. Close () var ID int//define an ID variable var LVS string//define the LVS variable for rows. Next () {//start loop Rerr: = rows. Scan (&id, &lvs)//data pointer will take the obtained data toward just the ID and LVs introduced if Rerr = = Nil {fmt.PRINTLN ("ID number is", StrConv.) Itoa (ID) + "LVs LVs is" + LVS)//output comes only, see}} insert_sql: = "INSERT into Xiaorui (LVS) VALUES (?)" _, E4: = db. Exec (Insert_sql, "Nima") fmt. Println (E4) db. Close ()//Shut down the database}
12345678910111213141516171819202122232425262728293031323334353637383940 //Xiaorui.cc#xiaorui. CC PackageMainImport (    "Database/sql" //This package must be quoted, is the bottom of theSQLDriven    "FMT"    _ "Github.com/go-sql-driver/mysql"    "StrConv" //This is to getintConverted tostring)funcMain() {  //MainFunction    DB, Err := SQL.Open("MySQL", "root: @tcp (localhost:3306)/dbname?charset=utf8")    //Database connection string, don't tell me I can't read it. The port must be written/       if Err != Nil {  //Connection Successful ErrMust beNilOtherwise it's an error.                Panic(Err.Error()) //Throw exception        FMT.Println(Err.Error())//Just show the exception    }    deferDB.Close()  //Only in the front. Panic ThendeferIf there is a problem linking the data, he will go toErrWrite Data                                                                                     rows, Err := DB.Query("Select Id,lvs from Xiaorui")    //JudgeErrWhether there is a wrong data, there isErrThe data is displayedPanicof data    if Err != Nil {                Panic(Err.Error())        FMT.Println(Err.Error())        return    }    deferrows.Close()    varID int  //Define aID Variable    varLVsstring //DefinedLVs Variable     for rows.Next() { //Start loop        Rerr := rows.Scan(&ID, &LVs)  //Data pointer, will get the data, to justID And LVsIntroduced        if Rerr == Nil {            FMT.Println("ID number is",StrConv.Itoa(ID) + "LVs LVs is"+ LVs) //Output come on, look.        }    }        Insert_sql := "INSERT into Xiaorui (LVS) VALUES (?)"        _, e4 := DB.Exec(Insert_sql,"Nima")                                                                    FMT.Println(e4)       DB.Close() //Close the database}
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.