2

As PostgreSQL connector I import following package:

_ "github.com/lib/pq"

The query I run is:

res, err := db.Query("SELECT id FROM applications WHERE email='" + email + "'")

where email is naturally a string. One way to count the number of rows in res is by following snippet

count:=0
for res.Next() {
  count++
  //some other code
}

but there should be some simpler (and quicker) way. It seems RowsAffected() is not the way to go. So, what is your suggestion?

1

1 Answer 1

0

Use the COUNT function:

"SELECT count(*) FROM applications WHERE email='" + email + "'"
Sign up to request clarification or add additional context in comments.

4 Comments

Please only post answers in English
You should NOT be constructing SQL queries like this. This is prone to SQL injection, you should use prepared statements.
Kulix, I'm not totally agree with you in this case. Certainly this type of query is prone to SQL injection, but if you validate that the email var is a valid email you avoid that problem. There's different ways to solve problems, as a commentary is good your point but I don't think this is a wrong answer at all.
What happens when your validation is bugged? SQL injection. This is absolutely the wrong way to construct a SQL query in all cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.