Skip to content

Commit feca16d

Browse files
committed
Added crud operations with individual methods for insert, update and view row and view all rows
1 parent f3fa397 commit feca16d

File tree

1 file changed

+67
-26
lines changed

1 file changed

+67
-26
lines changed

connect_with_a_database/main.go

Lines changed: 67 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,57 +15,98 @@ type DummyTable struct {
1515
Email string `json:"email"`
1616
}
1717

18-
func connectHandler() {
18+
func printAllRowsInTable(conn *pgx.Conn) {
19+
rows, err := conn.Query(context.Background(), "select * from dummy_table")
1920

20-
// env := os.Environ()
21+
if err != nil {
22+
fmt.Fprintf(os.Stderr, "There is a error while querying \n %v", err)
23+
os.Exit(1)
24+
}
2125

22-
// for _, val := range env {
23-
// fmt.Println(val)
24-
// }
25-
dbUrl := fmt.Sprintf(
26-
"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", "localhost", 5432, os.Getenv("PG_USERNAME_V1"), os.Getenv("PG_PASS_V1"), "postgres",
27-
)
26+
defer rows.Close()
2827

29-
os.Setenv("DATABASE_CONFIG", dbUrl)
28+
datas, _ := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[DummyTable])
3029

31-
connection, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_CONFIG"))
30+
for _, data := range datas {
31+
fmt.Println("data is", data)
32+
}
33+
}
34+
35+
func printRowAfterInserting(conn *pgx.Conn) {
36+
const QUERY = "INSERT INTO dummy_table(id, name, age, email) values($1,$2,$3,$4);"
37+
38+
row, err := conn.Exec(context.Background(), QUERY, 15, "Aniket", 27, "[email protected]")
3239

3340
if err != nil {
34-
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
41+
fmt.Fprintf(os.Stderr, "Error while inserting data into the table \n %v", err)
3542
os.Exit(1)
3643
}
3744

38-
defer connection.Close(context.Background())
45+
fmt.Println(row.RowsAffected())
46+
fmt.Println("Row after inserting")
47+
printRowInTable(conn, 14)
48+
}
3949

40-
// rows, err := connection.Query(context.Background(), "select * from dummy_table;")
50+
func printRowAfterUpdating(conn *pgx.Conn) {
51+
const QUERY = "UPDATE dummy_table set name=$1 WHERE id=$2;"
4152

42-
// err = pgxscan.Select(context.Background(), connection, &rows, "select * from dummy_table;")
53+
row, err := conn.Exec(context.Background(), QUERY, "Ankush", 15)
4354

44-
// if err != nil {
45-
// fmt.Fprintf(os.Stderr, "The error is \n %v", err)
46-
// os.Exit(1)
47-
// }
55+
if err != nil {
56+
fmt.Fprintf(os.Stderr, "Error while update data on the table \n %v", err)
57+
os.Exit(1)
58+
}
4859

49-
rows, err := connection.Query(context.Background(), "select * from dummy_table")
60+
fmt.Println("Rows affected", row.RowsAffected())
61+
fmt.Println("Row after updating")
62+
printRowInTable(conn, 14)
63+
}
64+
65+
func printRowInTable(conn *pgx.Conn, id int) {
66+
67+
var dummy DummyTable
68+
err := conn.QueryRow(context.Background(), "SELECT * from dummy_table where id=$1", id).Scan(&dummy.Id, &dummy.Name, &dummy.Age, &dummy.Email)
5069

5170
if err != nil {
52-
fmt.Fprintf(os.Stderr, "There is a error while querying \n %v", err)
71+
fmt.Fprintf(os.Stderr, "Error occoured while scanning row \n %v", err)
5372
os.Exit(1)
5473
}
74+
fmt.Println("Dummy table row is", dummy)
75+
}
5576

56-
defer rows.Close()
77+
func getDBConnectionHandler() *pgx.Conn {
5778

58-
datas, _ := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[DummyTable])
79+
dbUrl := fmt.Sprintf(
80+
"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", "localhost", 5432, os.Getenv("PG_USERNAME_V1"), os.Getenv("PG_PASS_V1"), "postgres",
81+
)
5982

60-
for _, data := range datas {
61-
fmt.Println("data is", data.Name)
83+
os.Setenv("DATABASE_CONFIG", dbUrl)
84+
85+
connection, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_CONFIG"))
86+
87+
if err != nil {
88+
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
89+
os.Exit(1)
6290
}
91+
92+
return connection
6393
}
6494

6595
func main() {
6696

67-
connectHandler()
97+
conn := getDBConnectionHandler()
98+
defer conn.Close(context.Background())
99+
100+
fmt.Println("Getting all rows in dummy table:")
101+
printAllRowsInTable(conn)
102+
103+
fmt.Println("\nGetting one row in dummy table:")
104+
printRowInTable(conn, 8)
105+
106+
fmt.Println(("\nInserting one row in dummy table"))
107+
printRowAfterInserting(conn)
68108

69-
fmt.Println("Here is a go file")
109+
fmt.Println(("\nUpdating one row in dummy table"))
110+
printRowAfterUpdating(conn)
70111

71112
}

0 commit comments

Comments
 (0)