Day 9 content, connecting to database
Build and copy to prod / build-and-copy (push) Successful in 1m31s
Build and copy to prod / build-and-copy (push) Successful in 1m31s
This commit is contained in:
1 parent
d3ee10669e
commit
49d33b2366
6 files changed
+73
No files matched your search
@@ -0,0 +1,41 @@
|
||||
package sites
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const createSitesTable = `CREATE TABLE IF NOT EXISTS sites (
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
name TEXT NOT NULL,
|
||||
url TEXT NOT NULL
|
||||
);`
|
||||
|
||||
func CreateTable(db *sql.DB) {
|
||||
if _, err := db.Exec(createSitesTable); err != nil {
|
||||
fmt.Println(fmt.Errorf(err.Error()))
|
||||
}
|
||||
}
|
||||
|
||||
type Site struct {
|
||||
id uint64
|
||||
created_at uint64
|
||||
Name string
|
||||
Url string
|
||||
}
|
||||
|
||||
func (s *Site) Save(db *sql.DB) {
|
||||
if s.id != 0 {
|
||||
query := `UPDATE SITES
|
||||
SET
|
||||
name=?,
|
||||
url=?
|
||||
WHERE id =?
|
||||
`
|
||||
db.Exec(query, s.Name, s.Url, s.id)
|
||||
} else {
|
||||
query := `INSERT INTO SITES (name, url) VALUES (?, ?)`
|
||||
db.Exec(query, s.Name, s.Url)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user