Can't pass array function result as parameter

James Spencer jspencer78 at mac.com
Sat Apr 1 11:40:29 EST 2006


On Apr 1, 2006, at 3:25 AM, David Burgun wrote:

> There's really no need to copy it entry by entry, you can just copy  
> the whole array as in:
>
> put LibGetArray() into myArray1
>
> put LibAnotherFunction(myArray1) into myArray2  --Here you could  
> put it back into Array1
>
> I do this all the time.
>
> What you can't do is this:
>
> put LibAnotherFunction (LibGetArray()) into myArrayX

Just to bring this full circle, I finally figured out what was going  
on.  I'm going to take the time to sum up what I figured out because  
it may save someone else some frustration.

Background:
Being more specific than I have been, I am testing for someone else a  
library of routines for accessing a MySQL database, something similar  
to Blue Mango's libDatabase (it's not mine which is why I've been a  
little careful about including any code from the library here).  The  
test involves a simple database containing records of books, things  
like Title, Author, ISBN, etc.  I was trying to get a record  
(representing a single book) from a database, modify it and then  
insert the modified record back into the database as a new record.   
The call to insert the record into the database was failing silently.

My original problem:
In my code, I was doing exactly what Dave suggests above.  I called  
the equivalent of LibGetArray(), specifically calling a library  
function which returned the current record in the form of an array  
where each element of the array is a field of the record.  I was  
putting this array into a local variable as Dave suggests above and  
the debugger showed that the local variable correctly contained a  
valid array.  I then modified one element, the title of the book, in  
the array (in the local variable) and then passed the local variable  
to the equivalent of LibAnotherFunction(), specifically a handler  
which inserts a record in the database with the values contained in  
the array passed to it.  My specific code which was failing:

on mouseUp

     -- duplicates the current record in the database, appending
     -- "(DUPLICATE)" to the book title.

     local tRecordSet, tCurrentRecord, tTitle
     local tErrorNo,tErrorString

     try
	-- cRecordSet is a custom property containing a reference to
	-- to the current database "cursor", i.e. a set of records
	-- from the database
         put the cRecordSet of this stack into tRecordSet
	
	-- the following puts the current record data into a
	-- Rev array using the database column names as the indices
         put dbGetRecordData(tRecordSet) into tCurrentRecord

         -- alter the title so we can see this is a duplicate book
         put tCurrentRecord["Title"] into tTitle
         put "(DUPLICATE)" after tTitle
         put tTitle into tCurrentRecord["Title"]

	-- dbInsert expects the name of a MySQL table and a Rev
	-- array containing the new record data, indexed by
	-- database column names.  This fails
         dbInsert "books", tCurrentRecord

         -- code to update the display omitted

     catch pException
	-- the above fails silently: we never catch an error here
         put item 1 of line 1 of pException into tErrorNo
         put item 2 of line 1 of pException into tErrorString
         answer "ERROR: Unable to copy the book.  Error = " &  
tErrorNo & \
                 " (" & tErrorString & ")!"
     end try

end mouseUp

What made this VERY frustrating was that I would walk through the  
above code and step into dbInsert in the debugger and what it showed  
was that tCurrentRecord was correctly being set by dbGetRecordData,  
the Title of the book was correctly getting changed in tCurrentRecord  
and tCurrentRecord was valid in the line calling dbInsert but in  
dbInsert, the second parameter was showing in the debugger as empty.

The solution:
In the end, the problem was NOT with the array passing but rather  
three unrelated bugs.

The first arose because I was using Constellation's debugger and  
variable display.  There is apparently a minor bug in how it shows  
arrays passed as  parameters: in dbInsert it was showing the second  
parameter as empty even though I knew I was passing a valid array.   
In fact, while Constellation's display showed the parameter as empty,  
Rev's variable watcher, which I finally opened this morning, shows  
that the parameter is valid containing exactly what I passed.

The other two bugs are related and I'm not sure if they are in the  
library I'm testing or in the Rev database library.  First, some of  
the elements in the array returned from dbGetCurrentRecord were empty  
and I was passing those empty elements back to dbInsert.  This causes  
the insertion to fail with a MySQL syntax error, although I haven't  
completely figured out why.  Whatever, just to complete the perfect  
storm, dbInsert has a bug in that it looks for "revdberr" in the  
result of the SQL command which did the insert and as the error  
returned by MySQL did not contain this, no error was being thrown.

Bottom line is that at this point, as I already discovered by  
happenstance, my code works by copying the array element by element  
checking that the element is not empty (I suppose I could also simply  
delete the empty elements from tCurrentRecord), and I will report the  
minor bugs that I found to both Constellation and the library author.

Thank you all for your help in working this out.

Spence

James P. Spencer
Rochester, MN

jspencer78 at mac.com

"Badges??  We don't need no stinkin badges!"




More information about the use-livecode mailing list