Skip to content

Commit 20c4339

Browse files
author
BuildTools
committed
POSTGRES
0 parents  commit 20c4339

File tree

8 files changed

+117
-0
lines changed

8 files changed

+117
-0
lines changed

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/DATABASE TESTING.iml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/sqldialects.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module DATABASE_TESTING
2+
3+
go 1.22
4+
5+
require github.com/lib/pq v1.10.9 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
2+
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=

main.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package main
2+
3+
import (
4+
"database/sql"
5+
"fmt"
6+
"log"
7+
8+
_ "github.com/lib/pq"
9+
)
10+
11+
type Product struct {
12+
Name string
13+
Price float64
14+
Available bool
15+
}
16+
17+
func main() {
18+
connStr := "YOUR CONNECTION STRING HERE"
19+
20+
db, err := sql.Open("postgres", connStr)
21+
22+
defer db.Close()
23+
24+
if err != nil {
25+
log.Fatal(err)
26+
}
27+
28+
if err := db.Ping(); err != nil {
29+
log.Fatal(err)
30+
}
31+
32+
createProductTable(db)
33+
34+
product := Product{"Go", 5.55, true}
35+
pk := insertProduct(db, product)
36+
37+
fmt.Printf("ID = %d\n", pk)
38+
}
39+
40+
func createProductTable(db *sql.DB) {
41+
/* PRODUCT TABLE
42+
- ID
43+
- NAME
44+
- PRICE
45+
- AVAILABLE
46+
- DATE CREATED
47+
*/
48+
49+
query := `CREATE TABLE IF NOT EXISTS product (
50+
id SERIAL PRIMARY KEY,
51+
name VARCHAR(100) NOT NULL,
52+
price NUMERIC(6,2) NOT NULL,
53+
available BOOLEAN,
54+
created timestamp DEFAULT NOW()
55+
)`
56+
57+
_, err := db.Exec(query)
58+
if err != nil {
59+
log.Fatal(err)
60+
}
61+
}
62+
63+
func insertProduct(db *sql.DB, product Product) int {
64+
query := `INSERT INTO product (name, price, available)
65+
VALUES ($1,$2,$3 ) RETURNING id`
66+
67+
var pk int
68+
err := db.QueryRow(query, product.Name, product.Price, product.Available).Scan(&pk)
69+
if err != nil {
70+
log.Fatal(err)
71+
}
72+
return pk
73+
}

0 commit comments

Comments
 (0)