-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
73 lines (60 loc) · 2.12 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"database/sql"
"errors"
"fmt"
"log"
"os"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
type City struct {
ID int `json:"id,omitempty" db:"ID"`
Name string `json:"name,omitempty" db:"Name"`
CountryCode string `json:"countryCode,omitempty" db:"CountryCode"`
District string `json:"district,omitempty" db:"District"`
Population int `json:"population,omitempty" db:"Population"`
}
func main() {
db, err := sqlx.Connect("mysql", fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", os.Getenv("DB_USERNAME"), os.Getenv("DB_PASSWORD"), os.Getenv("DB_HOSTNAME"), os.Getenv("DB_PORT"), os.Getenv("DB_DATABASE")))
if err != nil {
log.Fatalf("Cannot Connect to Database: %s", err)
}
fmt.Println("Connected!")
var city City
cities := []City{}
query_city := "SELECT * FROM city WHERE Name=?"
query_cities := "SELECT * FROM city WHERE CountryCode=?"
countryCity := "INSERT INTO city (CountryCode, Name) VALUES (?, ?)"
delete_city := "DELETE FROM city WHERE Name=?"
switch os.Args[1] {
case "city":
input_city := os.Args[2]
if err := db.Get(&city, query_city, input_city); errors.Is(err, sql.ErrNoRows) {
log.Printf("no such city Name = %s", input_city)
} else if err != nil {
log.Fatalf("DB Error: %s", err)
}
fmt.Printf(input_city+"の人口は%d人です\n", city.Population)
case "cities":
input_country := os.Args[2]
if err := db.Select(&cities, query_cities, input_country); errors.Is(err, sql.ErrNoRows) {
log.Printf("no such country Name = %s", input_country)
} else if err != nil {
log.Fatalf("DB Error: %s", err)
}
fmt.Println("日本の都市一覧")
for _, city := range cities {
fmt.Printf("都市名: %s, 人口: %d人\n", city.Name, city.Population)
}
case "add":
input_city_country := os.Args[2]
input_city_name := os.Args[3]
db.Exec(countryCity, input_city_country, input_city_name)
case "delete":
input_city_name := os.Args[2]
if err := db.Select(&city, delete_city, input_city_name); errors.Is(err, sql.ErrNoRows) {
log.Printf("no such country Name = %s", input_city_name)
}
}
}