'check if user already in DB
System.DebugLog("select count(1) from user where username = '"+ txt_user.Text +"'")
Dim userdata as RecordSet = db.getSQL("select count(*) from user where username = '"+ txt_user.Text +"'")
Dim result as string = userdata.Field("count(*)")
msgbox result
if result = "1" then
msgbox "User already exists..."
return
else
There is a User with the name Tester in the DB… when i now use the name Tester0 the count return a 1… but this is impossible in SQLiteStudio this select returns a 0 of course. Then when i try Tester00 its ok…
Xojo puts this select to sql which should normally return a 0: select count(*) from user where username = ‘Tester0’
personally I dislike the use of indexed return values, what if you change the SQL later, and add or move field names within the statement?
I would suggest this instead… and while its not a big deal with this simple statement, I think its a good habit to get into
Dim userdata as RecordSet = db.getSQL("select count(*) AS CNT from user where username = '"+ txt_user.Text +"'")
Dim result as string = userdata.Field("CNT").stringvalue
also, since you are getting COUNT, which is a NUMBER, I’d return it as a NUMBER
Dim result as integer = userdata.Field("CNT").integerValue
[quote=279374:@Wayne Golding]If you only want to know if the user is in the database can’t you just use EOF?
Dim userdata as RecordSet = db.getSQL("select * from user where username = '"+ txt_user.Text +"'")
Dim result as Boolean = Not(userdata.EOF)
I would also use a prepared statement to avoid SQL Injection issues.[/quote]
using "" as you did, causes the Database engine to create a fully populated data vector when one is not required, plus if the result of the where statement results in more than ONE return result, then you have THAT overhead and time as well. Best practice is to never return more than you need…
and actually using COUNT(8) instead of COUNT() is even more effective.
Or just use a prepared statement that uses an exists clause
Then the db engine can short circuit the existence check on the first result rather than having to do a count of possibly many rows This way you avoid the aggregate entirely
select 1 from user where exists ( select 1 from user where username = ‘"+ txt_user.Text +"’")
all this will tell you is there IS one or more user(s) with that name
[quote=279397:@Norman Palardy]Or just use a prepared statement that uses an exists clause
Then the db engine can short circuit the existence check on the first result rather than having to do a count of possibly many rows This way you avoid the aggregate entirely
select 1 from user where exists ( select 1 from user where username = ‘"+ txt_user.Text +"’")
all this will tell you is there IS one or more user(s) with that name[/quote]
I’d be interested in seeing the “EXPLAIN PLAN” on both of those
[quote=279375:@Dave S]using "" as you did, causes the Database engine to create a fully populated data vector when one is not required, plus if the result of the where statement results in more than ONE return result, then you have THAT overhead and time as well. Best practice is to never return more than you need…
and actually using COUNT(8) instead of COUNT() is even more effective.[/quote]
You know what Dave, 99 times out of 100 I want to at least know the existing users id, so whilst your are totally correct in the overhead it would be worse if a second query requested that data.
connecting to databases that are far far away, I’m ok with Dave on that point !
every single connection must be returning something useful and only that.
otherwise you end up with slow apps…
I would use a prepared statement checking for the id.
Once you get used using prepared statements and have some methods to just use it easily, you never go back to plain SQL.