Thread overview | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
December 11, 2015 How to check if result of request to DB is empty? | ||||
---|---|---|---|---|
| ||||
I am using https://github.com/buggins/ddbc string query_string = (`SELECT user, password FROM otest.myusers where user LIKE ` ~ `'%` ~ request["username"].to!string ~ `%';`); auto rs = db.stmt.executeQuery(query_string); string dbpassword; string dbuser; while (rs.next()) { dbuser = rs.getString(1); dbpassword = rs.getString(2); writeln(dbuser); writeln("Place seems unreachable"); // if SQL result is empty ... How I can check if SQL request returned empty result? |
December 11, 2015 Re: How to check if result of request to DB is empty? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | On 11.12.2015 22:05, Suliman wrote: > I am using https://github.com/buggins/ddbc > > string query_string = (`SELECT user, password FROM otest.myusers where > user LIKE ` ~ `'%` ~ request["username"].to!string ~ `%';`); Don't piece queries together without escaping the dynamic parts. Imagine what happens when the user enters an apostrophe in the username field. Also, are you using LIKE when authenticating the user? O_O > How I can check if SQL request returned empty result? When the result is empty, then rs.next() returns false on the first call, I presume. |
December 12, 2015 Re: How to check if result of request to DB is empty? | ||||
---|---|---|---|---|
| ||||
Posted in reply to anonymous | >> string query_string = (`SELECT user, password FROM otest.myusers where >> user LIKE ` ~ `'%` ~ request["username"].to!string ~ `%';`); > > Don't piece queries together without escaping the dynamic parts. Imagine what happens when the user enters an apostrophe in the username field. Do you mean to wrap: request["username"].to!string in quotes? > Also, are you using LIKE when authenticating the user? O_O Yes, my issue :) >> How I can check if SQL request returned empty result? > > When the result is empty, then rs.next() returns false on the first call, I presume. SO I can wrap it in `if`? Like: `if(rs.next())` ? |
December 12, 2015 Re: How to check if result of request to DB is empty? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | it's seems that next block is execute even if is rs.next() is false:
writeln("rs.next()-->", rs.next());
if(!rs.next()) //if user do not in DB
{
// is execute even if rs.next() is false
writeln("Executed, but rs.nst was set to false");
}
The output:
> rs.next()-->false
> Executed, but rs.nst was set to false
Why?
|
December 12, 2015 Re: How to check if result of request to DB is empty? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | 12.12.2015 13:28, Suliman пишет:
> it's seems that next block is execute even if is rs.next() is false:
>
> writeln("rs.next()-->", rs.next());
> if(!rs.next()) //if user do not in DB
> {
> // is execute even if rs.next() is false
> writeln("Executed, but rs.nst was set to false");
> }
>
> The output:
>> rs.next()-->false
>> Executed, but rs.nst was set to false
>
> Why?
>
>
That's right, because you have `if(!rs.next())`. If you change this to `if(rs.next())` this code block won't be executed.
|
December 12, 2015 Re: How to check if result of request to DB is empty? | ||||
---|---|---|---|---|
| ||||
Posted in reply to drug | On Saturday, 12 December 2015 at 10:36:12 UTC, drug wrote: > 12.12.2015 13:28, Suliman пишет: >> it's seems that next block is execute even if is rs.next() is false: >> >> writeln("rs.next()-->", rs.next()); >> if(!rs.next()) //if user do not in DB >> { >> // is execute even if rs.next() is false >> writeln("Executed, but rs.nst was set to false"); >> } >> >> The output: >>> rs.next()-->false >>> Executed, but rs.nst was set to false >> >> Why? >> >> > > That's right, because you have `if(!rs.next())`. If you change this to `if(rs.next())` this code block won't be executed. http://img.ctrlv.in/img/15/12/12/566c03c0657df.png |
December 12, 2015 Re: How to check if result of request to DB is empty? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | Oh sorry! I used wrong host! All ok! |
December 12, 2015 Re: How to check if result of request to DB is empty? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | On Saturday, 12 December 2015 at 11:31:18 UTC, Suliman wrote:
> Oh sorry! I used wrong host! All ok!
Yes, there was issue with host name, but it's do not solve problem. Second DB have same fields and I still getting false instead moving into while loop
|
December 12, 2015 Re: How to check if result of request to DB is empty? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | On Saturday, 12 December 2015 at 11:53:51 UTC, Suliman wrote:
> On Saturday, 12 December 2015 at 11:31:18 UTC, Suliman wrote:
>> Oh sorry! I used wrong host! All ok!
>
> Yes, there was issue with host name, but it's do not solve problem. Second DB have same fields and I still getting false instead moving into while loop
It's look like it do `next` step before return something.
So if in DB 1 value it will work like:
do step
if no value after it --> return false
if yes --> return true
but I need any way to check if first value in DB is exists. Any suggestion?
|
December 12, 2015 Re: How to check if result of request to DB is empty? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | On Saturday, 12 December 2015 at 12:06:21 UTC, Suliman wrote:
> On Saturday, 12 December 2015 at 11:53:51 UTC, Suliman wrote:
>> On Saturday, 12 December 2015 at 11:31:18 UTC, Suliman wrote:
>>> Oh sorry! I used wrong host! All ok!
>>
>> Yes, there was issue with host name, but it's do not solve problem. Second DB have same fields and I still getting false instead moving into while loop
>
> It's look like it do `next` step before return something.
> So if in DB 1 value it will work like:
> do step
> if no value after it --> return false
> if yes --> return true
>
> but I need any way to check if first value in DB is exists. Any suggestion?
If you expect to have single or zero rows in result, use
if (rs.next()) {
dbuser = rs.getString(1);
dbpassword = rs.getString(2);
writeln(dbuser);
} else {
writeln("user not found");
}
|
Copyright © 1999-2021 by the D Language Foundation