thanks martin...it seems with no query handling the database Next func does
not run anyway as the rows evaluate to false in the for loop...
// tell other players in the room you have entered
rows_users, err := database.Query("SELECT username FROM users WHERE room =
? AND username != ?", room, username)
// if the user from the db is connected then send them a message
var user string
for rows_users.Next() {
rows_users.Scan(&user)
if conn, ok := m[user]; ok {
conn.Write([]byte(string(username + " has entered the room\n# ")))
}
}
I should probably print something in the loop to confirm but I certainly
don't get any null pointer connection errors so may just comment this for
clarity and leave it. I guess it saves more code ;-)
On Saturday, September 12, 2020 at 9:53:28 PM UTC+1 mb0 wrote:
> hi Andy,
>
> when you take a look at the documentation of the Rows type returned by
> Query you will see a helpful example of its common use, appending the
> scanned results into a slice. my recommendation would be to follow this
> example and then check if the len(slice) == 0 to detect an empty set.
>
> if you don't actually need the results scanned into a go slice, consider
> using query row with a sql select count(*) … query and scan, then check
> the resulting integer.
>
> have fun!
>
> On 12.09.20 21:16, Andy Hall wrote:
> > the database.Query func does not return ErrNoRows unlike the
> > database.QueryRow func so how to I handle an empty set when I wish to
> > run a query which returns multiple rows...
> >
> > // tell other players in the room you have entered
> > rows_users, err := database.Query("SELECT username FROM users WHERE room
> > = ? AND username != ?", room, username)
> > // handle an empty set
> > if err != nil {
> > if err == sql.ErrNoRows {
> > fmt.Println("EMPTY SET")
> > } else {
> > fmt.Println(err)
> > return
> > }
> >
> > The string "EMPTY SET" is never printed as the condition is never
> > set...so continues to run through the if statement.
> >
> > Surely there is is way to handle no rows with Query ??
> >
> > --
> > You received this message because you are subscribed to the Google
> > Groups "golang-nuts" group.
> > To unsubscribe from this group and stop receiving emails from it, send
> > an email to [email protected]
> > <mailto:[email protected]>.
> > To view this discussion on the web visit
> >
> https://groups.google.com/d/msgid/golang-nuts/6d6c7d54-1a4b-4f07-bf22-8523045ce543n%40googlegroups.com
>
> > <
> https://groups.google.com/d/msgid/golang-nuts/6d6c7d54-1a4b-4f07-bf22-8523045ce543n%40googlegroups.com?utm_medium=email&utm_source=footer
> >.
>
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/0a03fd9c-b614-49ee-9d07-a7f44299fd7en%40googlegroups.com.