Golang links operate Mysql


package mainimport ( "database/sql" "fmt"_ "github.com/go-sql-driver/mysql" )func main() { // Open the database The db, err: = SQL. Open (" mysql ", "root: a root @ (127.0.0.1:3306)/test") if err ! = nil { fmt.Println(err) } // Delay shutting down the database defer db.Close() // Connect to the database db.Ping()

// Get all the data rows, _ := db.Query("select * from test where id > ?" 1), var id, name string for rows.Next() { rows.Scan(&id, &name) fmt.Println(id, name) }

// Get a piece of data row := db.QueryRow("select * from test where id = ?" And 5) row.Scan(&id, &name) fmt.Println(id, name)

/ / pretreatment stmt, _ := db.Prepare("insert into test (id, name) values (? ,?) ") result, _ := stmt.Exec(10, "haha") insert_id, _ := result.LastInsertId() fmt.Println(insert_id) }

Leave a Reply

Your email address will not be published. Required fields are marked *

en_USEnglish