-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
49 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package database_handler | ||
|
||
import ( | ||
"database/sql" | ||
_ "github.com/lib/pq" | ||
"flat-scraper/flat" | ||
"time" | ||
"fmt" | ||
"log" | ||
) | ||
|
||
func FlatInsert(flats []flat.Flat) { | ||
db, err := sql.Open("postgres", "user=postgres password='postgres' dbname=flats") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
err = db.Ping() | ||
if err != nil { | ||
log.Fatal("Error: Could not establish a connection with the database") | ||
} | ||
|
||
fmt.Printf("Connected to DB \n") | ||
|
||
t := time.Now() | ||
t_str := fmt.Sprintf("%d-%02d-%02d",t.Year(), t.Month(), t.Day()) | ||
for _,v := range flats { | ||
query := fmt.Sprintf("INSERT INTO flats(id,name,price,rooms,size,store,elevator,link,area,date_published)" + | ||
" VALUES (%d,'%s',%d,%d,%d,%d,%t,'%s','%s','%s');", v.Id, v.Name, v.Price, v.Rooms, v.Size, v.Store, | ||
v.Elevator, v.Link, v.Area, t_str) | ||
db.QueryRow(query) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
CREATE TABLE flats ( | ||
id integer CONSTRAINT firstkey PRIMARY KEY, | ||
name varchar(150) NOT NULL, | ||
price integer NOT NULL, | ||
rooms integer NOT NULL, | ||
size integer NOT NULL, | ||
store integer NOT NULL, | ||
elevator boolean NOT NULL, | ||
link varchar NOT NULL, | ||
area varchar(50) NOT NULL, | ||
date_published date NOT NULL | ||
); |