Page 1 of 1

Confused about Multi-dimensional arrays

Posted: Sat Jul 09, 2011 3:50 pm
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

Re: Confused about Multi-dimensional arrays

Posted: Sat Jul 09, 2011 3:58 pm
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())

Re: Confused about Multi-dimensional arrays

Posted: Sat Jul 09, 2011 4:13 pm
by purebuilt
I will give it a try, but can you explain how it works?

DB

Re: Confused about Multi-dimensional arrays

Posted: Sat Jul 09, 2011 4:42 pm
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