Browse Source

Updated db to use QueryRow (for single result).

Removed break, you can't break from the loop (from the switch).
Steve Thielemann 3 years ago
parent
commit
f7c94e4200
2 changed files with 8 additions and 33 deletions
  1. 7 14
      db.go
  2. 1 19
      space-ace.go

+ 7 - 14
db.go

@@ -17,6 +17,7 @@ func (db *DBData) Open(filename string) {
 	if err != nil {
 		log.Panicln("Error Opening Sqlite3:", err)
 	}
+	db.Create()
 }
 
 func (db *DBData) Close() {
@@ -43,22 +44,14 @@ func (db *DBData) Create() {
 }
 
 func (db *DBData) GetSetting(setting string, ifMissing string) string {
-	rows, err := db.DB.Query("SELECT value FROM settings WHERE username=? AND setting=?;", db.User, setting)
+	row := db.DB.QueryRow("SELECT value FROM settings WHERE username=? AND setting=?;", db.User, setting)
+	var value string
+	log.Printf("row: %#v\n", row)
+	err := row.Scan(&value)
 	if err != nil {
-		log.Panicln("Query GetSetting", err)
+		return ifMissing
 	}
-	defer rows.Close()
-	for rows.Next() {
-		var value string
-		err = rows.Scan(&value)
-		if err != nil {
-			log.Panicln("Scan GetSetting", err)
-		}
-		// log.Printf("GetSetting %s %s : %s\n", db.User, setting, value)
-		return value
-	}
-	// log.Printf("GetSetting %s %s :(", db.User, setting)
-	return ifMissing
+	return value
 }
 
 func (db *DBData) SetSetting(setting string, value string) {

+ 1 - 19
space-ace.go

@@ -22,17 +22,6 @@ func pctUpdate(pct *int64) func() int64 {
 	}
 }
 
-// Can I add a function to Door?
-// NO:  cannot define new methods on non-local type door.Door
-
-/*
-func (d *door.Door) PressAKey() {
-	d.Write(door.Reset + door.CRNL + "Press a key to continue...")
-	d.Key()
-	d.Write(door.CRNL)
-}
-*/
-
 func find_words(text string) [][]int {
 	word, _ := regexp.Compile("([A-Za-z]+)")
 	return word.FindAllStringIndex(text, -1)
@@ -301,13 +290,6 @@ func panel_demo(d *door.Door) {
 	d.Write(p.Output() + door.CRNL)
 }
 
-/*
-func createTable(db *sql.DB) {
-	db.Exec("CREATE TABLE IF NOT EXISTS settings(username TEXT, setting TEXT, value TEXT, PRIMARY KEY(username, setting));")
-	db.Exec("CREATE TABLE IF NOT EXISTS scores ( \"username\" TEXT, \"when\" INTEGER, \"date\" INTEGER, \"hand\" INTEGER, \"won\" INTEGER, \"score\" INTEGER, PRIMARY KEY(\"username\", \"date\", \"hand\"));")
-}
-*/
-
 func main() {
 	var message string
 
@@ -449,7 +431,7 @@ func main() {
 			press_a_key(&d)
 		case 'Q':
 			choice = -1
-			break
+			// break
 		}
 
 	}