Re: Retrieving stored functions return value
Posted by: Hasani Blackwell
Date: January 05, 2006 09:11PM

Here's an example of getting an out parameters:

http://www.estorecorp.com/mysql/apisample2.html

//Add the following procedure to the database
//
//CREATE PROCEDURE get_hi_msg(OUT msg VARCHAR(10))
//BEGIN
//SET msg = 'HI!';
//END
#include "stdafx.h"
#include <stdio.h>
#include <winsock.h>
#include <mysql.h>

int main(int argc, char* argv[])
{
MYSQL handle;
mysql_init(&handle);

if(&handle == NULL)
return -1;

//connect to the server
if(!mysql_real_connect(&handle, "127.0.0.1", "root", "SET_PASSWORD_HERE", "test", 0, NULL, CLIENT_MULTI_STATEMENTS | CLIENT_MULTI_RESULTS))
{
mysql_close(&handle);
return -1;
}

//execute the query
const char* sqlQuery = "SET @msg = NULL; CALL test.get_hi_msg(@msg); SELECT @msg;";
if(mysql_real_query(&handle, sqlQuery, (unsigned long) strlen(sqlQuery)))
{
mysql_close(&handle);
return -1;
}
printf("mysql_real_query: %s\n", sqlQuery);

MYSQL_RES* result = NULL;
do
{
printf("=================================\n");
//store the resultset
result = mysql_store_result(&handle);
if(result == NULL)
{
//if the resultset was null, 'continue' and get next resultset.
printf("Empty resultset retrieved\n");
continue;
}
//retrieve the first row
MYSQL_ROW row = mysql_fetch_row(result);
if(row != NULL)
{
//get the name of the first column in the resultset.
MYSQL_FIELD *mysqlFields = mysql_fetch_fields(result);
const char* field_name = mysqlFields[0].name;

//get the length of the data of the first row in the 1st column of the resultset.
unsigned long* field_lengths = mysql_fetch_lengths(result);
unsigned long field_length = field_lengths[0];

//copy the data to a null terminated string buffer.
char* fieldData = new char[field_length + 1];
memset(fieldData, '\0', field_length + 1);
memcpy(fieldData, row[0], field_length);

//output the results.
printf("%s: %s.\n", field_name, fieldData);

//perform memory cleanup.
delete[] fieldData;
}
mysql_free_result(result);
}//continue loop if there is another resultset avaialble.
while(mysql_next_result(&handle) == 0);
//free the resultset.
mysql_free_result(result);

//close the connection.
mysql_close(&handle);
return 0;
}

If you want to be fancy and use mysql binds as parameters to pass into stored procedures and process the results, the code will be very tedious.

1) You have to first upload the binds to the server using a set statement which will store the binds as session variables.
E.X: SET @param1:=?, @param2:=?, @param3:=?, ..., @paramN=?;

2) invoke the stored procedure using the
E.x.: CALL some_proc(@param1, @param2, @param3, ..., @paramN);

3) process the returned resultset(s).

4) optionally retrieve any OUT/INOUT parameters
E.x.: SELECT @param1 AS 'param1', @param2 AS 'param2', @param3 AS 'param3', ..., @paramN AS 'paramN'

5) optionally set the variables to null, reset the connection or close the connection
because session variables will probably take up server memory which might be troublesome if they contain a lot of text or binary data.
E.X: SET @param1=NULL, @param2=NULL, @param3=NULL, ..., @paramN=NULL;

Options: ReplyQuote


Subject
Views
Written By
Posted
Re: Retrieving stored functions return value
471
January 05, 2006 09:11PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.