Skip to content

Instantly share code, notes, and snippets.

@ardeshir
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save ardeshir/e53f5fac90033a59d642 to your computer and use it in GitHub Desktop.

Select an option

Save ardeshir/e53f5fac90033a59d642 to your computer and use it in GitHub Desktop.
package main
import (
"log"
"fmt"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
func main(){
db, err := sql.Open("mysql",
"username:password@tcp(xx.xx.xxx.xxx:3306)/databasename")
if err != nil {
log.Fatal(err)
}
err = db.Ping()
if err != nil {
fmt.Println("Ping Error\n")
}
var (
id int
name string
)
stmt, err := db.Prepare("select id, username from tbl_user where id =?")
if err != nil {
log.Fatal(err)
}
err = stmt.QueryRow(1).Scan(&id,&name)
if err != nil {
log.Fatal(err)
}
/*
stmt1, err := db.Prepare("INSERT INTO tbl_user(username) VALUES(?)")
if err != nil {
log.Fatal(err)
}
res, err := stmt1.Exec("Dolly3")
if err != nil {
log.Fatal(err)
}
lastId, err := res.LastInsertId()
if err != nil {
log.Fatal(err)
}
rowCnt, err := res.RowsAffected()
if err != nil {
log.Fatal(err)
}
log.Printf("ID = %d, affected = %d\n", lastId, rowCnt)
*/
fmt.Println(id, name)
_, err = db.Exec("DELETE FROM tbl_user where id=294")
if err != nil {
log.Fatal(err)
}
defer db.Close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment