Confused about Multi-dimensional arrays

Just starting out? Need help? Post your questions and find answers here.
User avatar
purebuilt
User
User
Posts: 46
Joined: Sun Dec 17, 2006 5:30 pm
Location: Milwaukee, WI, USA

Confused about Multi-dimensional arrays

Post by purebuilt »

Hello,

I am trying to create a procedure that will fill an array with data rows from a SQL query. I am having trouble passing the array. I want to dimension it and then redim to the size of the number of rows. Something like this:

Code: Select all

Procedure.l zExecuteSQLQuery(SQL.s,Array dat(10))
Protected.l hndDB = zOpenSQLDatabase("","")
Protected.l ttlcol,col,count = -1

If IsDatabase(hndDB)
   If DatabaseQuery(hndDB,SQL) : Debug SQL
     ttlcol = DatabaseColumns(hndDB)    
       While NextDatabaseRow(hndDB)
         count + 1
         For  col = 0 To ttlcol - 1
           dat(count,col) = GetDatabaseString(hndDB,col)
         Next col
         ReDim dat(count,ttlcol)
       Wend
       CloseDatabase(hndDB)
     EndIf
EndIf  
 
  ProcedureReturn count  
EndProcedure
What I would like is to be able to dim an array to match the number of colums in my query and have this procedure do the query an fill the array. Is there a better way I could understand? PB has complained about everything I do to pass an array to a procedure. I don't think the help examples are too good at this point.

Help?

Thanks... DB
- DB
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Confused about Multi-dimensional arrays

Post by STARGÅTE »

you can only redim the last dimension!
use this one:

Code: Select all

Structure DatabaseRow
	Array Field.s(0)
EndStructure

Procedure.l zExecuteSQLQuery(SQL.s,Array dat.DatabaseRow(1))
Protected.l hndDB = zOpenSQLDatabase("","")
Protected.l ttlcol,col,count = -1

If IsDatabase(hndDB)
   If DatabaseQuery(hndDB,SQL) : Debug SQL
     ttlcol = DatabaseColumns(hndDB)
       While NextDatabaseRow(hndDB)
         ReDim dat(count)\Field(ttlcol-1)    
         For  col = 0 To ttlcol - 1
           dat(count)\Field(col) = GetDatabaseString(hndDB,col)
         Next col
         count + 1
         ReDim dat(count)
       Wend
       CloseDatabase(hndDB)
     EndIf
EndIf  

  ProcedureReturn count  
EndProcedure 

Dim Rows.DatabaseRow(0)
zExecuteSQLQuery("", Rows())
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
purebuilt
User
User
Posts: 46
Joined: Sun Dec 17, 2006 5:30 pm
Location: Milwaukee, WI, USA

Re: Confused about Multi-dimensional arrays

Post by purebuilt »

I will give it a try, but can you explain how it works?

DB
- DB
User avatar
purebuilt
User
User
Posts: 46
Joined: Sun Dec 17, 2006 5:30 pm
Location: Milwaukee, WI, USA

Re: Confused about Multi-dimensional arrays

Post by purebuilt »

It works (I don't know why) but I had to change it a bit cause I was getting an array out of bounds error.

Code: Select all

Procedure.l zExecuteSQLQuery(SQL.s,Array dat.DatabaseRow(1))
  Protected.l hndDB = zOpenSQLDatabase("","")
  Protected.l ttlcol,col,count = 0 
  
  If IsDatabase(hndDB)
    If DatabaseQuery(hndDB,SQL) : Debug SQL
      ttlcol = DatabaseColumns(hndDB)
      While NextDatabaseRow(hndDB)    
        ReDim dat(count)\Field(ttlcol-1)
        
        For  col = 0 To ttlcol - 1
          dat(count)\Field(col) = GetDatabaseString(hndDB,col)
        Next col
        
        count + 1 
        ReDim dat(count) 
      Wend
      CloseDatabase(hndDB)
    EndIf
  EndIf  
  
  ProcedureReturn count  
EndProcedure
How is the array dimension (columns) being expanded also?

DB
- DB
Post Reply