SHARE
TWEET

Untitled

a guest Jun 16th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "database/sql"
  5.     "fmt"
  6.     "io"
  7.     "net/http"
  8.  
  9.     _ "github.com/go-sql-driver/mysql"
  10. )
  11.  
  12. var db *sql.DB
  13. var err error
  14.  
  15. func main() {
  16.     // user:password@tcp(localhost:5555)/dbname?charset=utf8
  17.     db, err = sql.Open("mysql", "awsuser:mypassword@tcp(mydbinstance1.cahupqyjjqa0.us-east-2.rds.amazonaws.com:3306)/test?charset=utf8")
  18.     check(err)
  19.     defer db.Close()
  20.  
  21.     err = db.Ping()
  22.     check(err)
  23.  
  24.     http.HandleFunc("/", index)
  25.     http.HandleFunc("/amigos", amigos)
  26.     http.HandleFunc("/create", create)
  27.     http.HandleFunc("/insert", insert)
  28.     http.HandleFunc("/read", read)
  29.     http.HandleFunc("/update", update)
  30.     http.HandleFunc("/delete", del)
  31.     http.HandleFunc("/drop", drop)
  32.     http.Handle("/favicon.ico", http.NotFoundHandler())
  33.     err := http.ListenAndServe(":8080", nil)
  34.     check(err)
  35. }
  36. func check(err error) {
  37.     if err != nil {
  38.         fmt.Println(err)
  39.     }
  40. }
  41. func index(res http.ResponseWriter, req *http.Request) {
  42.     _, err := io.WriteString(res, "at index")
  43.     check(err)
  44. }
  45. func amigos(res http.ResponseWriter, req *http.Request) {
  46.     rows, err := db.Query(`SELECT aName FROM amigos;`)
  47.     check(err)
  48.     defer rows.Close()
  49.  
  50.     var s, name string
  51.     s = "RETRIEVED RECORDS:\n"
  52.  
  53.     for rows.Next() {
  54.         err = rows.Scan(&name)
  55.         check(err)
  56.         s += name + "\n"
  57.     }
  58.     fmt.Fprintln(res, s)
  59. }
  60. func create(res http.ResponseWriter, req *http.Request) {
  61.     stmt, err := db.Prepare(`CREATE TABLE customer (name VARCHAR(20));`)
  62.     check(err)
  63.     defer stmt.Close()
  64.  
  65.     r, err := stmt.Exec()
  66.     check(err)
  67.  
  68.     n, err := r.RowsAffected()
  69.     check(err)
  70.  
  71.     fmt.Fprintln(res, "CREATED TABLE customer", n)
  72. }
  73. func insert(res http.ResponseWriter, req *http.Request) {
  74.     stmt, err := db.Prepare(`INSERT INTO customer VALUES("James");`)
  75.     check(err)
  76.     defer stmt.Close()
  77.  
  78.     r, err := stmt.Exec()
  79.     check(err)
  80.  
  81.     n, err := r.RowsAffected()
  82.     check(err)
  83.  
  84.     fmt.Fprintln(res, "INSERTED RECORD", n)
  85. }
  86. func read(res http.ResponseWriter, req *http.Request) {
  87.     rows, err := db.Query(`SELECT * FROM customer;`)
  88.     check(err)
  89.     defer rows.Close()
  90.  
  91.     var name string
  92.     for rows.Next() {
  93.         err = rows.Scan(&name)
  94.         check(err)
  95.         fmt.Fprintln(res, "RETRIEVED RECORD:", name)
  96.     }
  97. }
  98. func update(res http.ResponseWriter, req *http.Request) {
  99.     stmt, err := db.Prepare(`UPDATE customer SET name="Jimmy" WHERE name="James";`)
  100.     check(err)
  101.     defer stmt.Close()
  102.  
  103.     r, err := stmt.Exec()
  104.     check(err)
  105.  
  106.     n, err := r.RowsAffected()
  107.     check(err)
  108.  
  109.     fmt.Fprintln(res, "UPDATED RECORD", n)
  110. }
  111. func del(res http.ResponseWriter, req *http.Request) {
  112.     stmt, err := db.Prepare(`DELETE FROM customer WHERE name="Jimmy";`)
  113.     check(err)
  114.     defer stmt.Close()
  115.  
  116.     r, err := stmt.Exec()
  117.     check(err)
  118.  
  119.     n, err := r.RowsAffected()
  120.     check(err)
  121.  
  122.     fmt.Fprintln(res, "DELETED RECORD", n)
  123. }
  124. func drop(res http.ResponseWriter, req *http.Request) {
  125.     stmt, err := db.Prepare(`DROP TABLE customer;`)
  126.     check(err)
  127.     defer stmt.Close()
  128.  
  129.     _, err = stmt.Exec()
  130.     check(err)
  131.  
  132.     fmt.Fprintln(res, "DROPPED TABLE customer")
  133. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top