2020-09-28 20:37:02 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func InitDB(filepath string) *sql.DB {
|
|
|
|
log.Printf("Init DB")
|
|
|
|
db, err := sql.Open("sqlite3", filepath)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if db == nil {
|
2020-09-29 07:44:35 +00:00
|
|
|
panic("DB couldn't be initialized")
|
2020-09-28 20:37:02 +00:00
|
|
|
}
|
2020-09-29 07:44:35 +00:00
|
|
|
|
|
|
|
// TODO: Change to correct DB-Structure
|
|
|
|
statement, _ := db.Prepare("CREATE TABLE IF NOT EXISTS people (id INTEGER PRIMARY KEY, firstname TEXT, lastname TEXT)")
|
|
|
|
statement.Exec()
|
|
|
|
|
2020-09-28 20:37:02 +00:00
|
|
|
return db
|
|
|
|
}
|
2020-09-29 07:44:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
sqlStmt := fmt.Sprintf(`INSERT INTO data
|
|
|
|
(id, content)
|
|
|
|
VALUES
|
|
|
|
(?, ?)`)
|
|
|
|
|
|
|
|
tx, err := db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt, err := tx.Prepare(sqlStmt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
for i := 1; i < 10; i++ {
|
|
|
|
id := fmt.Sprintf("%d", i)
|
|
|
|
content := fmt.Sprintf("content #%d %s", i, shortuuid.New())
|
|
|
|
log.Printf("Inserting %s: %s", id, content)
|
|
|
|
_, err := stmt.Exec(id, content)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tx.Commit()
|
|
|
|
*/
|