Problem with C++ interface 2.5, prepared sql statement
Posted in 1999
Hi all. I'm having a weird error. I took one of the standard demo codes (C++) to use prepared statements and insert specially blobs, but my problem arises with all types. I boiled down the code to the below. It works on a table called test1 with a single integer column called "a". The code connects properly to the correct database, etc (verified by a separate normal insertion and checked on the server). Now, I prepare an SQL statement with one placeholder (originally it had a loop, thats why it looks weird). The goal is to ITValue the single (integer) placeholder and fill it in with the FromPrintable method from a string read from cin. When I give an integer, it always fails in line 90 saying that it could not set the parameter 0 to the value you gave as in: <login stuff> Preparing the following SQL: insert into test1 values ( ? ) There are 1 parameter markers in the SQL 3 Buffer is: `3' Parameter type is: integer Could not set parameter @ line (90) 0 to '3' Incidentally, it also did not work for Informixs demo code, I massaged and massaged it until the below form (removed all the code that deals with collections, blobs, etc.): #include <ctype.h> #include <string.h> #include <iostream.h> #include <it.h> ITCallbackResult errorHandler(const ITErrorManager &errorobject, void *userdata, long errorlevel) { // Cast the user data into a stream cerr << "my_error_handler: connstr=" << (char *) userdata << " errorlevel=" << errorlevel << (errorobject.Warn() == TRUE? "\\tALWAYS_DIFFERENT Warning" : " Error") << " sqlstate=" << errorobject.SqlState() << ' ' << (errorobject.Warn() == TRUE? errorobject.WarningText() : errorobject.ErrorText()) << endl; return IT_NOTHANDLED; } int main(int argc, char *argv[] ) { char dbname[128]; char dbuser[128]; char dbpassword[128]; // Set the database info ITDBInfo db_info; cout << "Datbase name: "; cin >> dbname; cout << "Datbase user: "; cin >> dbuser; cout << "Datbase password: "; cin >> dbpassword; db_info.SetDatabase(dbname); db_info.SetUser(dbuser); db_info.SetPassword(dbpassword); // Connect to default and make a statement. Attach errorHandler. ITConnection conn; conn.Open(db_info); ITStatement stmt( conn ); stmt.AddCallback( errorHandler, (void *)&cerr ); // Build and prepare insert SQL. ITString sql = "insert into test1 values ( ? )"; cout << "Preparing the following SQL: " << sql << endl; if( !stmt.Prepare( sql ) ) { cout << "Could not prepare sql statement" << endl; return -1; } // Read cin, set parameters and execute statement. // Data file consists of white space - separated columns. // Each column either contains a double-quoted string // (quotes are repeated inside the string) or // a sequence of non white space characters. // NULL is represented by non-quoted word NULL int nparams = stmt.NumParams(); // number of parameters in statement cout << "There are " << nparams << " parameter markers in the SQL" << endl; char buffer[32767]; // row data buffer // each input line has one row of parms cin.getline(buffer, sizeof buffer); cin.getline(buffer, sizeof buffer); int paramno=0; cout << "Buffer is: `" << buffer << "'" << endl; conn.SetTransaction( ITConnection::Begin ); ITValue *param = stmt.Param( paramno ); if ( !param ) { cerr << "Couldn't get parameter " << paramno << endl; return -1; } cout << "Parameter type is: " << param->TypeOf().Name() << endl; if ( !param->FromPrintable(buffer) ) { // <--- FAILS HERE cerr << "Could not set parameter @ line (" << __LINE__ << ") " << paramno << " to '" << buffer << "'" << endl; return -1; } param->Release(); if ( !stmt.Exec() ) { cerr << "Could not execute statement" << endl; return -1; } cout << "Number of rows affected was: " << stmt.RowCount() << endl; conn.SetTransaction( ITConnection::Commit ); return 0; } --- Michael Ortega-Binderberger miki@acm.org, miki@ics.uci.edu Department of Computer Science Office (949)824-7231, Fax (949)824-4056 U. of Illinois, Urbana Champaign http://www.ics.uci.edu/~miki On loan to University of California, Irvine ICQ: 25412074 "Any addition to the truth is a subtraction from it." Alexander Solzhenitsyn