StarlioX/functions/database.go

44 lines
828 B
Go
Raw Normal View History

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