Just starting out? Need help? Post your questions and find answers here.
tutiplain
User
Posts: 43 Joined: Wed Jun 30, 2010 3:00 am
Post
by tutiplain » Mon Jan 14, 2013 4:05 pm
Hi all,
Can PB do a "select count(field) from table" type of query? I am trying it but it always reports 0. Here's the code:
Code: Select all
r = DatabaseQuery(db,"select count (id) from estudiantes;")
If r =0
MessageRequester("Error","Error en query: " + DatabaseError())
EndIf
cant_estud.i = GetDatabaseFloat(db,0)
MessageRequester("",Str(cant_estud))
Dim keys.i(cant_estud)
After execution of this code r =1 and cant_estud =0, and I don't understand why. The table has three records in it, so cant_estud should equal 3, not 0. Any help greatly appreciated.
ts-soft
Always Here
Posts: 5756 Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany
Post
by ts-soft » Mon Jan 14, 2013 4:12 pm
not tested, but i hope it works
Code: Select all
r = DatabaseQuery(db,"select count (id) from estudiantes;")
If r =0
MessageRequester("Error","Error en query: " + DatabaseError())
EndIf
NextDatabaseRow(db); <--
cant_estud.i = GetDatabaseLong(db,0); <--
MessageRequester("",Str(cant_estud))
Dim keys.i(cant_estud)
PureBasic 5.73 |
SpiderBasic 2.30 |
Windows 10 Pro (x64) |
Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
falsam
Enthusiast
Posts: 632 Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:
Post
by falsam » Mon Jan 14, 2013 7:20 pm
tutiplain wrote: Can PB do a "select count(field) from table" type of query? I am trying it but it always reports 0.
Yes. An example with a database in memory.
Code: Select all
UseSQLiteDatabase()
If OpenDatabase(0, ":memory:", "", "")
Debug "Connected"
;Create database
DatabaseUpdate(0, "CREATE TABLE estudiantes (idauto INTEGER PRIMARY KEY, score INTEGER);")
Debug DatabaseError()
;inserting 100 records with a score by student to illustrate
;"Select count" And why not "select Sum"
For i = 1 To 100
DatabaseUpdate(0, "insert into estudiantes (score) values('" + Str(Random(20))+ "')")
Next
;Control record
DatabaseQuery(0, "SELECT * FROM estudiantes")
While NextDatabaseRow(0)
Debug "IdAuto : "+ GetDatabaseString(0, 0) + " Score : "+ GetDatabaseString(0, 1)
Wend
;Select Count
If DatabaseQuery(0, "SELECT COUNT(idauto) FROM estudiantes") <> 0
NextDatabaseRow(0)
Debug "Number of records 'Select Count' : " + GetDatabaseString(0, 0)
EndIf
;Select Sum
If DatabaseQuery(0, "SELECT SUM(score) FROM estudiantes") <> 0
NextDatabaseRow(0)
Debug "Total Score 'Select Sum ' : " + GetDatabaseString(0, 0)
EndIf
Else
Debug DatabaseError()
EndIf
➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti
Sorry for my bad english and the Dunning–Kruger effect
jassing
Addict
Posts: 1885 Joined: Wed Feb 17, 2010 12:00 am
Post
by jassing » Mon Jan 14, 2013 9:04 pm
DatabaseQuery() doesn't return the count of results A query can be successful, but have zero results.
You have to use NextDatabaseRow()
flaith
Enthusiast
Posts: 704 Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:
Post
by flaith » Tue Jan 15, 2013 5:23 pm
This one works too :
Code: Select all
;Select Count
If DatabaseQuery(0, "Select Count(*) From estudiantes") <> 0
FirstDatabaseRow(0)
Debug "Number of records 'Select Count' : " + GetDatabaseString(0, 0)
EndIf
“Fear is a reaction. Courage is a decision.” - WC
Fred
Administrator
Posts: 18344 Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:
Post
by Fred » Tue Jan 15, 2013 5:26 pm
It's better to use NextDatabaseRow() as FirstDatabaseRow() isn't granted to work with every drivers.
flaith
Enthusiast
Posts: 704 Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:
Post
by flaith » Tue Jan 15, 2013 11:29 pm
Good to know, thanks Fred
“Fear is a reaction. Courage is a decision.” - WC
tutiplain
User
Posts: 43 Joined: Wed Jun 30, 2010 3:00 am
Post
by tutiplain » Wed Jan 16, 2013 1:35 am
Thanks for your replies. Using NextDatabaseRow() solved the problem.