SQL Count function not supported?

Just starting out? Need help? Post your questions and find answers here.
tutiplain
User
User
Posts: 43
Joined: Wed Jun 30, 2010 3:00 am

SQL Count function not supported?

Post by tutiplain »

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.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: SQL Count function not supported?

Post by ts-soft »

not tested, but i hope it works :wink:

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.
Image
User avatar
falsam
Enthusiast
Enthusiast
Posts: 632
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: SQL Count function not supported?

Post by falsam »

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
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: SQL Count function not supported?

Post by jassing »

DatabaseQuery() doesn't return the count of results A query can be successful, but have zero results.
You have to use NextDatabaseRow()
User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

Re: SQL Count function not supported?

Post by flaith »

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
Administrator
Posts: 18344
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: SQL Count function not supported?

Post by Fred »

It's better to use NextDatabaseRow() as FirstDatabaseRow() isn't granted to work with every drivers.
User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

Re: SQL Count function not supported?

Post by flaith »

Good to know, thanks Fred :D
“Fear is a reaction. Courage is a decision.” - WC
tutiplain
User
User
Posts: 43
Joined: Wed Jun 30, 2010 3:00 am

Re: SQL Count function not supported?

Post by tutiplain »

Thanks for your replies. Using NextDatabaseRow() solved the problem.
Post Reply