LoginSignup
1
0

Golang: PostgreSQL のデータを作成 (Create)

Last updated at Posted at 2018-05-23

プログラム

postgre_create.go
// ----------------------------------------------------------------
//
//	postgre_create.go
//
//					Mar/03/2023
//
// ----------------------------------------------------------------
package main

import (
	"fmt"
	"database/sql"
	_ "github.com/lib/pq"
	"strconv"
)

// ----------------------------------------------------------------
func main() {
	fmt.Println ("*** 開始 ***")

	dict_aa := data_prepare_proc ()

	str_connect := "user=scott dbname=city password=tiger123 sslmode=disable"
	db, err := sql.Open("postgres", str_connect)
	defer db.Close ()

	_, err = db.Exec ("drop table cities")
	if err != nil {
		fmt.Println (err)
		}

	sql_str := "create table cities (id varchar(10),name varchar(20),population int,date_mod date)"
	fmt.Println (sql_str)

	_, err = db.Exec (sql_str)
	if err != nil {
		fmt.Println (err)
		}

	for key,_ := range dict_aa {
		name := dict_aa[key]["name"].(string)
		population := dict_aa[key]["population"].(int)
		date_mod := dict_aa[key]["date_mod"].(string)
		sql_str :="insert into cities values ('" + key + "','" + name + "','" + strconv.Itoa(population) + "','" + date_mod + "')"
		fmt.Println (sql_str)
		_, err = db.Exec (sql_str)
		if err != nil {
			fmt.Println (err)
			}
		}

	fmt.Println ("*** 終了 ***")
}


// ----------------------------------------------------------------
func data_prepare_proc () map[string](map[string]interface{}) {
	dict_aa := make (map[string](map[string]interface{}))

	dict_aa["t3461"] = unit_gen_proc ("広島",97452,"1921-11-12")
	dict_aa["t3462"] = unit_gen_proc ("福山",49271,"1921-8-25")
	dict_aa["t3463"] = unit_gen_proc ("東広島",63597,"1921-4-14")
	dict_aa["t3464"] = unit_gen_proc ("呉",95178,"1921-2-20")
	dict_aa["t3465"] = unit_gen_proc ("尾道",41749,"1921-6-24")
	dict_aa["t3466"] = unit_gen_proc ("竹原",74582,"1921-1-10")
	dict_aa["t3467"] = unit_gen_proc ("三次",48297,"1921-10-19")
	dict_aa["t3468"] = unit_gen_proc ("大竹",38591,"1921-12-28")
	dict_aa["t3469"] = unit_gen_proc ("府中",25978,"1921-5-14")

	return (dict_aa)
}

// ----------------------------------------------------------------
func unit_gen_proc (name string,population int,date_mod string) map[string]interface{} {
	unit_aa := make (map[string]interface{})
	unit_aa["name"] = name
	unit_aa["population"] = population
	unit_aa["date_mod"] = date_mod

	return (unit_aa)
}

// ----------------------------------------------------------------

実行の準備

go mod init create
go mod tidy

go.mod go.sum が作成される。

実行コマンド

go run postgre_create.go
1
0
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
1
0