Starlio-web/functions/database.go

42 lines
874 B
Go
Raw Normal View History

2022-09-18 19:28:38 +03:00
package functions
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
func Database() {
db, openErr := sql.Open("sqlite3", "EveryNasa.db")
if openErr != nil {
panic(openErr)
}
var exists bool
QueryErr := db.QueryRow("SELECT EXISTS(SELECT name FROM sqlite_master WHERE type='table' AND name='settings')").Scan(&exists)
if QueryErr != nil {
panic(QueryErr)
}
if exists == false {
sqlTable := `
CREATE TABLE IF NOT EXISTS settings (
autostart INTEGER DEFAULT 0,
2022-09-19 16:55:33 +03:00
autochangewallpaper INTEGER DEFAULT 0
2022-09-18 19:28:38 +03:00
);`
_, CreateTableErr := db.Exec(sqlTable)
if CreateTableErr != nil {
panic(CreateTableErr)
}
2022-09-21 10:47:08 +03:00
stmt, InsertErr := db.Prepare("INSERT INTO settings(autostart, autochangewallpaper) values(?,?)")
2022-09-18 19:28:38 +03:00
if InsertErr != nil {
panic(InsertErr)
}
2022-09-21 10:47:08 +03:00
_, ExecErr := stmt.Exec(0, 0)
2022-09-18 19:28:38 +03:00
if ExecErr != nil {
panic(ExecErr)
}
}
}