Refactoring the code and add new table, for favorite img

This commit is contained in:
Данил 2022-12-15 21:04:53 +03:00
parent 365caaeed0
commit 9abfd9f864

View file

@ -11,14 +11,7 @@ func Database() {
Logger(err.Error()) Logger(err.Error())
} }
var existsSettings bool if !TableExists(db, "settings") {
err = db.QueryRow("SELECT EXISTS(SELECT name FROM sqlite_master WHERE type='table' AND name='settings')").Scan(&existsSettings)
if err != nil {
Logger(err.Error())
}
if existsSettings == false {
sqlTable := ` sqlTable := `
CREATE TABLE IF NOT EXISTS settings ( CREATE TABLE IF NOT EXISTS settings (
startup INTEGER DEFAULT 0, startup INTEGER DEFAULT 0,
@ -42,4 +35,41 @@ func Database() {
Logger(err.Error()) Logger(err.Error())
} }
} }
if !TableExists(db, "favorite") {
sqlTable := `
CREATE TABLE IF NOT EXISTS favorite (
title TEXT,
explanation TEXT,
copyright TEXT,
date TEXT,
url TEXT,
hdurl TEXT,
media_type TEXT
);`
_, err = db.Exec(sqlTable)
if err != nil {
Logger(err.Error())
}
}
}
func GetDatabase() *sql.DB {
db, err := sql.Open("sqlite3", "EveryNasa.db")
if err != nil {
Logger(err.Error())
}
return db
}
func TableExists(db *sql.DB, name string) bool {
var exists bool
err := db.QueryRow("SELECT EXISTS(SELECT name FROM sqlite_master WHERE type='table' AND name=?)", name).Scan(&exists)
if err != nil {
Logger(err.Error())
}
return exists
} }