I'm writing code to manage users in a sqlite database with Go.
I'm trying to check if a username is taken, but my code is ugly.
My table looks like:
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE,
password TEXT
And I check if a username is taken with:
func UserExists(db * sql.DB, username string) bool {
sqlStmt := `SELECT username FROM userinfo WHERE username = ?`
count := 0
rows, err := db.Query(sqlStmt, username)
Check(err)
for rows.Next() { // Can I just check if rows is non-zero somehow?
count++
}
return len(rows) != 0
}
Is there a better query I could use that would tell me if the username value exists in the table in a more staight forward way? Or is there a nicer way to check if rows
is non-zero?