Passing strings with commas as arguments/parameters
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Passing strings with commas as arguments/parameters
Hello there.
I'm unable to figure out a way to pass a string containing a comma as an argument to a custom handler. I wrapped up some database code and pass the query as a parameter, and sometimes the query contains a comma. When I pass this, it splits the argument at the comma and treats it as multiple arguments rather than one.
Any ideas? I've looked around for a few hours, so thought I'd ask!
I'm unable to figure out a way to pass a string containing a comma as an argument to a custom handler. I wrapped up some database code and pass the query as a parameter, and sometimes the query contains a comma. When I pass this, it splits the argument at the comma and treats it as multiple arguments rather than one.
Any ideas? I've looked around for a few hours, so thought I'd ask!
-
- VIP Livecode Opensource Backer
- Posts: 977
- Joined: Sat Apr 08, 2006 7:47 am
- Contact:
Please post the part of your script where you're calling the custom handler - it's difficult to diagnose the problem when we don't know if you're using 'call', 'send', 'do' or just executing he custom handler directly.
Best regards,
Jan Schenkel.
Best regards,
Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com
www.quartam.com
Here you go:Janschenkel wrote:Please post the part of your script...
Code: Select all
on dbQueryToArray a_queryString, a_connectionId
-- BEGIN: STRING PROBLEM WORKAROUND
if ((the paramCount > 1) and (param(the paramCount) is not an integer)) then
put param(1) into newQueryString
repeat with index = 2 to (the paramCount)
put (newQueryString & "," & param(index)) into newQueryString
end repeat
put newQueryString into a_queryString
put empty into a_connectionId
else if ((the paramCount > 1) and (param(the paramCount) is an integer)) then
put param(1) into newQueryString
repeat with index = 2 to (the paramCount - 1)
put (newQueryString & "," & param(index)) into newQueryString
end repeat
put newQueryString into a_queryString
put param(the paramCount) into a_connectionId
end if
-- END: STRING PROBLEM WORKAROUND
if ((a_connectionId is not empty) and (a_connectionId is an integer)) then
put a_connectionId into connectionId
else
put database["connection_id"] into connectionId
end if
if (connectionId is an integer) then
put (a_queryString) into sqlQuery
put revQueryDatabase(connectionId, sqlQuery) into sqlResultSetId
if (sqlResultSetId is an integer) then
put (revNumberOfRecords(sqlResultSetId)) into recordCount
if recordCount < 1 then
return empty
end if
put revDatabaseColumnNames(sqlResultSetId) into columnNames
split columnNames by comma
revMoveToFirstRecord(sqlResultSetId)
repeat with resultRowIndex = 1 to recordCount
repeat for each element columnName in columnNames
put revDatabaseColumnNamed(sqlResultSetId, columnName) into rows[resultRowIndex, columnName]
end repeat
revMoveToNextRecord(sqlResultSetId)
end repeat
put recordCount into rows["count"]
else
return "zberr>The result set is invalid."
end if
end if
return rows
end dbQueryToArray
This is saved in the stack script. I call this from various cards throughout the stack as follows:
Code: Select all
put "SELECT id, type, name FROM my_table GROUP BY type" into sqlQuery
call "dbQueryToArray" && sqlQuery
-
- VIP Livecode Opensource Backer
- Posts: 977
- Joined: Sat Apr 08, 2006 7:47 am
- Contact:
You don't have to use the 'call' command to run your handler - as long as it's in the message path, Revolution will find it for you.
Instead, just use
For more information on the message path, read this excellent article by Richard Gaskin: http://www.fourthworld.com/embassy/arti ... _path.html as well as page 109 through 122 of the Revolution User Guide.
Hope this helped,
Jan Schenkel.
Instead, just use
Code: Select all
put "SELECT id, type, name FROM my_table GROUP BY type" into sqlQuery
dbQueryToArray sqlQuery
Hope this helped,
Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com
www.quartam.com
-
- VIP Livecode Opensource Backer
- Posts: 1005
- Joined: Sat Apr 08, 2006 3:06 pm
- Contact:
While you should use Jan's suggestion for your particular needs here is a little tip for times when you need to use send/call.
After assigning a value to the variable that you want to pass as a parameter, you should include the variable as part of the string you send. So rather than
you would want to do this:
In the first example you are appending the value of sqlQuery to the string being passed to call. Since the string contains two commas you are actually passing three parameters to dbQueryToArray.
In the second example the variable name is passed to call. The engine treats this as one parameter and passes the value of the variable to dbQueryToArray. You could pass multiple values as well:
Using this technique you can safely pass values when using send and call.
After assigning a value to the variable that you want to pass as a parameter, you should include the variable as part of the string you send. So rather than
Code: Select all
put "SELECT id, type, name FROM my_table GROUP BY type" into sqlQuery
call "dbQueryToArray" && sqlQuery
Code: Select all
put "SELECT id, type, name FROM my_table GROUP BY type" into sqlQuery
call "dbQueryToArray sqlQuery"
In the second example the variable name is passed to call. The engine treats this as one parameter and passes the value of the variable to dbQueryToArray. You could pass multiple values as well:
Code: Select all
put "SELECT id, type, name FROM my_table GROUP BY type" into sqlQuery
put 12 into theConnectionID
call "dbQueryToArray sqlQuery, theConnectionID"
Trevor DeVore
ScreenSteps - https://www.screensteps.com
LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder
ScreenSteps - https://www.screensteps.com
LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder