You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

51 lines
1.0 KiB

package main
import (
"database/sql"
"log"
"os"
_ "github.com/mattn/go-sqlite3"
"github.com/vartanbeno/go-reddit/v2/reddit"
)
func store() {
}
func InitDb(Dbname string) {
filename := Dbname + ".db"
if _, err := os.Stat(Dbname + ".db"); err == nil {
log.Printf("DB exists")
os.Remove(Dbname + ".db")
} else if os.IsNotExist(err) {
file, err := os.Create(filename)
if err != nil {
log.Printf("Unable to write file: %v", err)
}
defer file.Close()
}
db, err := sql.Open("sqlite3", Dbname+".db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
sqlStmt := `
create table posts (id integer not null primary key AUTOINCREMENT, title text, url text, created int);
delete from posts;
`
_, err = db.Exec(sqlStmt)
if err != nil {
log.Printf("%q: %s\n", err, sqlStmt)
return
}
}
func insert(db *sql.DB, st *reddit.Post) {
db.Exec("INSERT into posts (title, url, created) values (?, ?, ?);", st.Title, st.URL, st.Created.Time.Unix())
}
func get(db *sql.DB) {
db.Exec("SELECT url FROM posts;")
}