DataBlade API Programmer's Manual
Chapter 6: Sending SQL Statements
Home
Contents
Index
Master Index
New Book
SQL Statements Within the Same Function
In a client application, the effects of one part of an
mi_exec()
function are not visible to other parts. Therefore, if one SQL statement depends on an earlier one, they should not both be in the same call to
mi_exec()
. For example, the following statement causes an error:
mi_exec(myconn, "create table foo(a int, b int);
insert into foo values (1,2);", 0);
The error occurs because the
INSERT
statement cannot see the result of the
CREATE TABLE
statement. The solution is to call
mi_exec()
twice, as follows:
/* get a connection in a server LIBMI program */
myconn = mi_open(NULL, NULL, NULL);
mi_exec(myconn,"create table foo (a integer, b integer);", 0);
mi_query_finish(myconn);
mi_exec(myconn, "insert into foo values (1,2);", 0);
mi_query_finish(myconn);
Example: send_statement
The following user-defined function,
send_statement()
, takes an existing open connection and an SQL statement string as arguments and executes the statement in the database server with the
mi_exec()
function. It calls another user-defined function,
get_results()
, to examine the results. For more information on how to process results, see
"Processing Results"
.
/*
* Called from within a server function to
* handle a query to be passed to database server
*/
int
send_statement( MI_CONNECTION * conn, char * cmd)
{
int count;
/* send the statement */
if ( MI_ERROR == mi_exec( conn, cmd, 0 ) )
{
mi_db_error_raise( conn, MI_MESSAGE, "Cannot send: query\n" );
return (-1);
}
else
{
; /* continue */
}
/* get the results of the statement */
count = get_results( conn );
/* make sure it is finished */
if (mi_query_finish( conn ) == MI_ERROR)
{
mi_db_error_raise( conn, MI_MESSAGE, "Cannot finish: query\n" );
return (-1);
}
else
{
; /* continue */
}
return ( 0 );
}
DataBlade API Programmer's Manual
, version 9.1
Copyright © 1998, Informix Software, Inc. All rights reserved.