Skip to content

Commit 896f67b

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

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

connect_with_a_database/main.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ func printAllRowsInTable(conn *pgx.Conn) {
3232
}
3333
}
3434

35+
func printRowAfterDeleting(conn *pgx.Conn) {
36+
const QUERY = "DELETE FROM dummy_table WHERE id=$1"
37+
38+
row, err := conn.Exec(context.Background(), QUERY, 15)
39+
40+
if err != nil {
41+
fmt.Fprintf(os.Stderr, "Error while deleting data into the table \n %v", err)
42+
os.Exit(1)
43+
}
44+
45+
fmt.Println(row.RowsAffected())
46+
fmt.Println("Row after deleting")
47+
printRowInTable(conn, 15)
48+
}
49+
3550
func printRowAfterInserting(conn *pgx.Conn) {
3651
const QUERY = "INSERT INTO dummy_table(id, name, age, email) values($1,$2,$3,$4);"
3752

@@ -44,7 +59,7 @@ func printRowAfterInserting(conn *pgx.Conn) {
4459

4560
fmt.Println(row.RowsAffected())
4661
fmt.Println("Row after inserting")
47-
printRowInTable(conn, 14)
62+
printRowInTable(conn, 15)
4863
}
4964

5065
func printRowAfterUpdating(conn *pgx.Conn) {
@@ -109,4 +124,7 @@ func main() {
109124
fmt.Println(("\nUpdating one row in dummy table"))
110125
printRowAfterUpdating(conn)
111126

127+
fmt.Println(("\nDeleting one row in dummy table"))
128+
printRowAfterDeleting(conn)
129+
112130
}

0 commit comments

Comments
 (0)