LoginSignup
0
1

More than 3 years have passed since last update.

golang でpostgresにinsert

Last updated at Posted at 2020-05-13

 とりあえず一行をInsertしてみた。

package main                                                                                       

import (
    "database/sql"
    "fmt"

    _ "github.com/lib/pq"
)

const (
    host     = "localhost"
    port     = 5432
    user     = "postgres"
    password = "1234"
    dbname   = "sample"
)

func main() {
    psqlInfo := fmt.Sprintf("host=%s user=%s password=%s dbname=%s sslmode=disable", host,user,password,dbname)
    fmt.Println(psqlInfo)
    db, err := sql.Open("postgres", psqlInfo)
    if err != nil {
        panic(err)
    }   
    defer db.Close()

    sqlStatement := `
    INSERT INTO account (id) values (30)`
    _, err = db.Exec(sqlStatement)
    if err != nil {
        panic(err)
    }   

}

db.Queryで複数行のSelectをしたりする。

参考
Querying for multiple records with Go's sql package
[Go言語] database/sqlパッケージを使ってみた

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1