Matrix/utils/databaseController.go
2020-09-29 09:44:35 +02:00

54 lines
980 B
Go

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 {
panic("DB couldn't be initialized")
}
// 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()
return db
}
/*
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()
*/