Skip to content
Advertisement

Checking if a value exists in sqlite db with Go

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:

And I check if a username is taken with:

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?

Advertisement

Answer

Use QueryRow to query at most one row. If the query doesn’t return any row, it returns sql.ErrNoRows.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement