dbSetParameter()

Database function for setting name/value pairs for use as a parameter in the subsequent commands.

Synopsis

int dbSetParameter(dbCommand cmd,string paramIdentifier,unsigned paramType,anytype paramValues);

Parameters

Parameter Meaning
cmd Command handle (see dbStartCommand()).
paramIdentifier "Unique Bind-parameter identifier (see below for limitations).
paramType

DB_PARAM_IN

DB_PARAM_OUT - not implemented

DB_PARAM_INOUT - not implemented

paramValues Value of the parameter.

Return Value

The return value is an error code. 0 indicates the operation has been performed successfully. Use dbGetError() to obtain detailed error information in case of other values.

Errors

An error is returned if an invalid record set reference variable, command reference or a wrong parameter data type is passed.

Description

The call of dbSetParameter() sets a parameter as name/value pair. A multiple call is supported and allows providing of many parameters for one of the subsequent function or command calls. There is a distinction between position and name parameters. The syntax of position parameters is "?, ?" and ":1, :2".

Bind parameters cannot be used to edit zero values (insert / update / delete)!

WINDOWS:

Generally, OLE-DB does not support name parameters at the moment. The decision takes place only via the position of the parameter. I.e. in a query like "WHERE id = :param1 OR id = :param2" the parameters must be set in the correct order they will be used, whereby the names of them can be different in the function and in the query. When overwriting with the same name, i.e.dbSetParameter() is called twice, the parameters maintain their position. This means that if "param1" is set as first parameter, then"param2" and then "param1" again, the value of "param1" is used as first parameter.

The query syntax depends on the database and the call syntax on stored procedures (e.g. "{ Call proc1 }"forOracle).

Limitations:

The maximum number of possible parameters is approximately 2000.

LINUX:

In Qt, the parameter names must be the same as the parameter names in the query.

Limitations:

The maximum number of possible parameters is approximately 2000.

Example

#uses "ctrlADO.dll"
main()
{
  int i, max;
  dyn_string tmp;
  string data;
  // Store data as string with a length up to 33k
  max = 2000 + (rand() % 1000);
  for (i = 1; i <= max; i++)
  tmp[i] = "TEST ITEM #" + i;
  data = (string) tmp;
  dbConnection dbConn; //Declaration of the connection
  if (dbOpenConnection("DSN=pc_db11g;UID=scott;PWD=tiger;",
  dbConn) == 0)
  { 
    // Open connection to data source
    dbCommand dbCmd;
    string plsqlCall = "UPDATE schema.MY_TEST_DATA SET my_data = ?
    WHERE my_id = 'TEST RECORD'";
    if (dbBeginTransaction(dbConn) != 0)
    {
      DebugTN("dbBeginTransaction() failed");
    }
    else if (dbStartCommand(dbConn, plsqlCall, dbCmd) != 0)
    {
      DebugTN("dbStartCommand() failed for " + plsqlCall);
    }
    else if (dbSetParameter(dbCmd, 1, 0, data) != 0)
    // Set the string as first parameter
    {
      DebugTN("dbSetParameter() failed for " + plsqlCall);
    }
    else if (dbExecuteCommand(dbCmd) != 0)
    // Add the parameter to the database
    {
      DebugTN("dbExecuteCommand() failed for " + plsqlCall);
    }
    else if (dbFinishCommand(dbCmd) != 0)
    {
      DebugTN("dbFinishCommand() failed for " + plsqlCall);
    }
    else if (dbCommitTransaction(dbConn) != 0)
    {
      DebugTN("dbCommitTransaction() failed");
    }
    dbCloseConnection(dbConn);
  }
}
#uses "ctrlADO.dll"

main()

{

 string sConnectString;
 dbConnection dbConn;
 dbCommand dbCmd;
 dyn_string ds;
 int rc = 0;

DebugN("SetParamTest");

 sConnectString =
                                    "DSN=eisux077_db11g;UID=owokl;PWD=std;InputStringLimit=16384;";
 DebugN("Open ", sConnectString);
if (dbOpenConnection(sConnectString, dbConn) == 0 &&
dbBeginTransaction(dbConn) == 0)

{

 dbRecordset rs, rs2;

anytype fld;

string sql = "SELECT a FROM ado_test WHERE B = :1 OR B = :2";

rc = dbStartCommand(dbConn, sql, dbCmd);

rc = dbSetParameter(dbCmd, 2, DB_PARAM_IN, 255);

rc = dbSetParameter(dbCmd, 1, DB_PARAM_IN, 23);

rc = dbExecuteCommand(dbCmd, rs2);

while(!rc && !dbEOF rs2)) // As long as end of file not reached

{

rc = dbGetField(rs2, 0, fld); // Current column

DebugN("out(1): ", fld);

rc = dbMoveNext(rs2);// Go to next record set

}

dbCloseRecordset(rs2);// Close subset

// Change parameters

rc = dbSetParameter(dbCmd, 1, DB_PARAM_IN, 47);

rc = dbSetParameter(dbCmd, 2, DB_PARAM_IN, 255);

rc = dbExecuteCommand(dbCmd, rs);

while (!rc && !dbEOF (rs))

{

rc = dbGetField(rs, 0, fld);// Current column

DebugN("out(2): ", fld );

rc = dbMoveNext(rs);// Go to next record set

}

dbCloseRecordset(rs); // Close subset

DebugN("test complete, committing and closing database connection");

dbCommitTransaction(dbConn);

dbCloseConnection(dbConn);

}

exit(0);

}

Assignment

ADO and Qt, Database functions

Availability

CTRL