Working with MySQL database in Golang (1) Implement additions and deletions and change operations

Source: Internet
Author: User
Tags stmt
**golang Operation MySQL Introduction **golang operation MySQL Database feel a bit like PHP in the PDO to the MySQL operation, assuming you were originally phper transition to the Golang of the acceptance is very cordial, the overall feeling is very simple * * Golang operation MySQL Note point * * ' Golang implements the standard library for MySQL operation but does not implement MySQL driver ' so we need to download the Go-sql-driver driver package from GitHub (recommended in SRC directory), Use the command as follows: ' Gogo get github.com/go-sql-driver/mysql ' * * Create a table field in the test database as shown below * * ' gocreate table IF not EXISTS ' test '. ' User ' (' user_id ' INT (one) UNSIGNED not NULL auto_increment COMMENT ' user number ', ' user_name ' VARCHAR ' NOT null COMMENT ' user name ' , ' User_age ' TINYINT (3) UNSIGNED NOT null default 0 COMMENT ' user age ', ' User_sex ' TINYINT (3) UNSIGNED NOT NULL default 0 COMM ENT ' user sex ', PRIMARY KEY (' user_id ')) ENGINE = InnoDB auto_increment = 1 DEFAULT CHARACTER SET = UTF8 COLLATE = Utf8_genera L_ci COMMENT = ' User table ' * * for data increment (INSERT) operation * * ' Gopackage mainimport ("FMT" "database/sql"//import MySQL driver _ "github.com/ Go-sql-driver/mysql ") Func main () {//Use the Open Connection database in the Database/sql package db,err: = SQL. Open ("MySQL", "root:root@tcp (localhost:3306)/test?charset=utf8") if err! = Nil {fmt. PRINTLN ("Connection Database failed:", err) return}//using DB structInstance method prepare preprocessing is inserted, prepare returns a stmt object stmt,err: = db. Prepare ("INSERT INTO ' user ' (User_name,user_age,user_sex) VALUES (?,?,?)") If Err!=nil{fmt. Println ("Preprocessing failed:", err) return}//uses stmt object to perform preprocessing parameters Result,err: = stmt. Exec ("Pengjin", 33, "male") if err!=nil{fmt. Println ("Execution preprocessing failed:", err) return}else{rows,_: = result. Rowsaffected () fmt. Println ("Execution succeeded, affects rows", rows, "Rows")}} "" As on the code there is a way to manipulate the PDO in PHP ", as the above code can actually not write the prepare method directly through the Exec method directly to achieve the operation, equivalent to a shorthand method, We can try to use this method to implement the delete operation directly * * To achieve the deletion of the data (delete) operation * * "" "Gopackage mainimport (" FMT "" database/sql "//import MySQL driver _" github.com/ Go-sql-driver/mysql ") Func main () {//Use the Open Connection database in the Database/sql package db,err: = SQL. Open ("MySQL", "root:root@tcp (localhost:3306)/test?charset=utf8") if err! = Nil {fmt. PRINTLN ("Connection Database failed:", err) return}//directly calls the Exec method in the DB instance to implement preprocessing result,err: = db. Exec ("Delete from ' user ' where user_id=?", 1) if err!=nil{fmt. Println ("Preprocessing failed:", err) return}if err!=nil{fmt. Println ("Execution preprocessing failed:", err) return}else{rows,_: = result. Rowsaffected () fmt. Println ("Execution succeeded, affects rows", rows, "Rows")}} ' * * Implement UPDATE operation to database * * ' gopackageMainimport ("FMT" "database/sql"//import MySQL driver _ "Github.com/go-sql-driver/mysql") func main () {//Use database/ The Open Connection database in the SQL package db,err: = SQL. Open ("MySQL", "root:root@tcp (localhost:3306)/test?charset=utf8") if err! = Nil {fmt. PRINTLN ("Connection Database failed:", err) return}//directly calls the Exec method in the DB instance to implement preprocessing result,err: = db. Exec ("Update ' user ' set user_age=?,user_name=?") Where user_id=? "," Zhangsan ", 2) if err!=nil{fmt. Println ("Preprocessing failed:", err) return}if err!=nil{fmt. Println ("Execution preprocessing failed:", err) return}else{rows,_: = result. Rowsaffected () fmt. Println ("Execution succeeded, affects rows", rows, "Rows")}} "100 reads  
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.