SHARE
TWEET

Untitled

a guest Jan 26th, 2019 125 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.         _ "github.com/lib/pq"
  7. )
  8.  
  9. var Db *sql.DB
  10.  
  11. func init() {
  12.         var err error
  13.         // DB名やユーザー、テーブルは適宜適切な値を入れてください
  14.         Db, err = sql.Open("postgres", "user=gwp dbname=postgres password=gwp sslmode=disable")
  15.         if err != nil {
  16.                 panic(err)
  17.         }
  18. }
  19.  
  20. type Member struct {
  21.         Id         int
  22.         FirstName  string
  23.         LastName   string
  24.         Email      string
  25.         AccessPrev bool
  26. }
  27.  
  28. func main() {
  29.         fmt.Println("----- Query 1 -----")
  30.         rows, err := Db.Query("SELECT * FROM members ORDER BY id")
  31.         if err != nil {
  32.                 return
  33.         }
  34.         for rows.Next() {
  35.                 m := Member{}
  36.                 rows.Scan(&m.Id, &m.FirstName, &m.LastName, &m.Email, &m.AccessPrev)
  37.                 fmt.Println(m)
  38.         }
  39.         fmt.Println()
  40.  
  41.         fmt.Println("----- Query 2 -----")
  42.         rows, err = Db.Query("SELECT * FROM members WHERE accessprev = $1 ORDER BY id", "false")
  43.         if err != nil {
  44.                 return
  45.         }
  46.         for rows.Next() {
  47.                 m := Member{}
  48.                 rows.Scan(&m.Id, &m.FirstName, &m.LastName, &m.Email, &m.AccessPrev)
  49.                 fmt.Println(m)
  50.         }
  51.         fmt.Println()
  52.  
  53.         fmt.Println("----- QueryRow -----")
  54.         m := Member{}
  55.         err = Db.QueryRow("SELECT * FROM members WHERE accessprev = $1 ORDER BY id", "true").Scan(&m.Id, &m.FirstName, &m.LastName, &m.Email, &m.AccessPrev)
  56.         if err != nil {
  57.                 return
  58.         }
  59.         fmt.Println(m)
  60.         fmt.Println()
  61.  
  62.         fmt.Println("----- Prepare -----")
  63.         statement := "SELECT * FROM members WHERE accessprev = $1 ORDER BY id"
  64.         stmt, err := Db.Prepare(statement)
  65.         if err != nil {
  66.                 return
  67.         }
  68.         defer stmt.Close()
  69.  
  70.         rows, err = stmt.Query("false")
  71.         for rows.Next() {
  72.                 m := Member{}
  73.                 rows.Scan(&m.Id, &m.FirstName, &m.LastName, &m.Email, &m.AccessPrev)
  74.                 fmt.Println(m)
  75.         }
  76.         fmt.Println()
  77.  
  78.         fmt.Println("----- Exec -----")
  79.         _, err = Db.Exec("INSERT INTO members VALUES ($1, $2, $3, $4, $5)", 4, "takashi", "yamamoto", "yamamoto@yahoo.co.jp", "FALSE")
  80.         if err != nil {
  81.                 return
  82.         }
  83.         rows, err = Db.Query("SELECT * FROM members ORDER BY id")
  84.         if err != nil {
  85.                 return
  86.         }
  87.         for rows.Next() {
  88.                 m := Member{}
  89.                 rows.Scan(&m.Id, &m.FirstName, &m.LastName, &m.Email, &m.AccessPrev)
  90.                 fmt.Println(m)
  91.         }
  92.         fmt.Println()
  93.  
  94. }
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