Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fcd5f81

Browse files
chore: query to add data in tables
1 parent a50e2d1 commit fcd5f81

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed

‎backend/sql_connection/main.go‎

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,53 @@ func handleConnection(res http.ResponseWriter, req *http.Request) {
1616

1717
}
1818

19+
func showAllDataFromTables(db *sql.DB) {
20+
printTable := func(query string, name string, cols []string) {
21+
rows, err := db.Query(query)
22+
if err != nil {
23+
log.Fatalf("Error fetching data from %s: %v", name, err)
24+
}
25+
defer rows.Close()
26+
27+
fmt.Printf("\n--- %s ---\n", name)
28+
29+
for rows.Next() {
30+
// Create a slice of interface{} to hold values
31+
values := make([]interface{}, len(cols))
32+
valuePtrs := make([]interface{}, len(cols))
33+
for i := range cols {
34+
valuePtrs[i] = &values[i]
35+
}
36+
37+
if err := rows.Scan(valuePtrs...); err != nil {
38+
log.Fatal(err)
39+
}
40+
41+
// Print row as key=value pairs
42+
for i, col := range cols {
43+
fmt.Printf("%s=%v ", col, values[i])
44+
}
45+
fmt.Println()
46+
}
47+
}
48+
49+
// Show Branch
50+
printTable("SELECT branch_name, branch_city, assets FROM branch", "Branch",
51+
[]string{"branch_name", "branch_city", "assets"})
52+
53+
// Show Account
54+
printTable("SELECT accno, branch_name, balance FROM account", "Account",
55+
[]string{"accno", "branch_name", "balance"})
56+
57+
// Show Customer
58+
printTable("SELECT customer_name, customer_street, customer_city FROM customer", "Customer",
59+
[]string{"customer_name", "customer_street", "customer_city"})
60+
61+
// Show Depositor
62+
printTable("SELECT customer_name, accno FROM depositor", "Depositor",
63+
[]string{"customer_name", "accno"})
64+
}
65+
1966
func executeSQLOperations() {
2067
db, err := sql.Open("mysql", "username:password@(127.0.0.1:3306)/bank?parseTime=true")
2168

@@ -108,7 +155,94 @@ func executeSQLOperations() {
108155
}
109156
}
110157
fmt.Println("Tables Creation Done.. ")
158+
fmt.Println("Data insertion started into specific tables")
159+
160+
// query to insert data
161+
162+
// insert data into Branch
163+
{
164+
fmt.Println("Data insertion started into Branch table....")
165+
166+
branchDataInsertionQuery := `
167+
INSERT INTO branch (branch_name, branch_city, assets)
168+
VALUES
169+
('Main', 'New York', 1000000),
170+
('Downtown', 'Boston', 750000),
171+
('Uptown', 'Chicago', 500000);
172+
`
173+
174+
if _, err = db.Exec(branchDataInsertionQuery); err != nil {
175+
log.Fatal("Branch insert failed:", err)
176+
} else {
177+
fmt.Println("Data insertion successful into Branch table....")
178+
}
179+
}
180+
181+
// insert data into Account
182+
{
183+
fmt.Println("Data insertion started into Account table....")
184+
185+
accountDataInsertionQuery := `
186+
INSERT INTO account (accno, branch_name, balance)
187+
VALUES
188+
(101, 'Main', 5000),
189+
(102, 'Main', 7000),
190+
(201, 'Downtown', 8000),
191+
(301, 'Uptown', 6000);
192+
`
193+
194+
if _, err = db.Exec(accountDataInsertionQuery); err != nil {
195+
log.Fatal("Account insert failed:", err)
196+
} else {
197+
fmt.Println("Data insertion successful into Account table....")
198+
}
199+
}
200+
201+
// insert data into Customer
202+
{
203+
fmt.Println("Data insertion started into Customer table....")
204+
205+
customerDataInsertionQuery := `
206+
INSERT INTO customer (customer_name, customer_street, customer_city)
207+
VALUES
208+
('Alice', '5th Ave', 'New York'),
209+
('Bob', 'Main St', 'Boston'),
210+
('Charlie', 'Lake View', 'Chicago');
211+
`
212+
213+
if _, err = db.Exec(customerDataInsertionQuery); err != nil {
214+
log.Fatal("Customer insert failed:", err)
215+
} else {
216+
fmt.Println("Data insertion successful into Customer table....")
217+
}
218+
}
219+
220+
// insert data into Depositor
221+
{
222+
fmt.Println("Data insertion started into Depositor table....")
223+
224+
depositorDataInsertionQuery := `
225+
INSERT INTO depositor (customer_name, accno)
226+
VALUES
227+
('Alice', 101),
228+
('Alice', 102),
229+
('Bob', 201),
230+
('Charlie', 301);
231+
`
232+
233+
if _, err = db.Exec(depositorDataInsertionQuery); err != nil {
234+
log.Fatal("Depositor insert failed:", err)
235+
} else {
236+
fmt.Println("Data insertion successful into Depositor table....")
237+
}
238+
}
239+
240+
fmt.Println("Data insertion into specific tables completed")
241+
242+
showAllDataFromTables(db)
243+
111244
fmt.Println(err)
245+
112246
}
113247

114248
func main() {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /