LiveCode LessonsData GridData Grid Tips & TricksDatabasesConverting a Database Cursor To a Data Grid Array

Converting a Database Cursor To a Data Grid Array

This lesson demonstrates a handler that will convert a database cursor into an array that you can use to set the dgData property of a data grid.

The Handler

command ConvertSQLCursorToArray pCursor, @pOutArrayA
	local i,j
	local theFields
	local theError
    
	put revDatabaseColumnNames(pCursor) into theFields
	if theFields begins with "revdberr," then
		put item 2 to -1 of theFields into theError
	end if
    
	if theError is empty then
		put 0 into i
		repeat until revQueryIsAtEnd(pCursor)
			add 1 to i
             
			repeat for each item theField in theFields
				put revDatabaseColumnNamed(pCursor, theField) into pOutArrayA[i][ theField ]
			end repeat
             
			revMoveToNextRecord pCursor
		end repeat
	end if
     
	return theError
end ConvertSQLCursorToArray

Example

You can call the command as follows. In this example theCursor is a database cursor that you have opened using revQueryDatabase.

local theDataA
ConvertSQLCursorToArray theCursor, theDataA
put the result into theError

 

If theError is empty then dimension 1 of theDataA will contain integers from 1 to the number of records in the cursor (revNumberOfRecords). Each 1st dimension, in turn, has a key for each column in the cursor (revDatabaseColumnNames).

Here is what the array might look like if your cursor had 2 columns (id and name) and 3 rows.

theDataA [1] ["id"]
theDataA [1] ["name"]
theDataA [2] ["id"]
theDataA [2] ["name"]
theDataA [3]["id"]
theDataA [3] ["name"]

You can use this array to set the dgData property of a data grid.

set the dgData of group "DataGrid 1" to theDataA

0 Comments

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.