Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for one liner example how to bind and insert a null value into a database using ODBC API

Tags:

c++

c

odbc

The function is

SQLRETURN SQLBindParameter(
      SQLHSTMT        StatementHandle,
      SQLUSMALLINT    ParameterNumber,
      SQLSMALLINT     InputOutputType,
      SQLSMALLINT     ValueType,
      SQLSMALLINT     ParameterType,
      SQLULEN         ColumnSize,
      SQLSMALLINT     DecimalDigits,
      SQLPOINTER      ParameterValuePtr,
      SQLLEN          BufferLength,
      SQLLEN *        StrLen_or_IndPtr);

The documentations I have seen is confusing. Do the arguments depend on the data type or not I found an example here http://support.microsoft.com/kb/248799 which does not seem to work on DB2. I thought odbc was consistent across databases. A specific code example would he helpful.

like image 449
user754425 Avatar asked Dec 27 '22 18:12

user754425


1 Answers

Its not one line as such but

SQLLEN ival;

ret = SQLBindParameter( stmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, 100, 0, NULL, 0, &ival );

/*
 * before execution is called
 */

ival = SQL_NULL_DATA;

That inserts a NULL value as a CHAR(100) datatype. Pick the actual datatype to match what your column type is, but the important thing is to set the indicator value to SQL_NULL_DATA before the SQLExecute or SQLExecDirect is called. And make sure its still set to that value at the execute point.

like image 98
Nick Gorham Avatar answered Jan 18 '23 22:01

Nick Gorham