Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / C++
Technical Blog

Oracle C++ API non-sense

Rate me:
Please Sign up or sign in to vote.
2.00/5 (1 vote)
29 Oct 2012CPOL 8.5K   3
A solution to the problem.

I spent two hours trying to figure out this nonsense. Here is the problem:

Environment *env = Environment::createEnvironment();
Connection *conn = env->createConnection("username", "password",
connectionString);
string sqlQueryText = "UPDATE myTable SET field4 =:p4, field3 =:p3
WHERE field2 :=p2 AND field1 :=p1";
Statement* updateStatement = conn->createStatement(sqlQueryText);

updateStatement.setInt(1, field1Var);
updateStatement.setString(2, field2Var);
updateStatement.setInt(3, field3Var);
updateStatement.setString(4, field4Var);

updateStatement.executeUpdate(conn);

What’s wrong with the code above? Not a thing in my opinion. Yet, if you run it, you’ll get a pretty cryptic invalid number exception.

Turns out that the way the query is put together is order specific and when the values are converted at run time, an exception is thrown.

The following is how I made it work:

Environment* env = Environment::createEnvironment();
Connection* conn = env->createConnection("username", "password",
connectionString);
string sqlQueryText = "UPDATE myTable SET field4 =:p1, field3 =:p2
WHERE field2 :=p3 AND field1 :=p4";
Statement* updateStatement = conn->createStatement(sqlQueryText);

updateStatement.setString(1, field4Var);
updateStatement.setInt(2, field3Var);
updateStatement.setString(3, field2Var);
updateStatement.setInt(4, field1Var);

updateStatement.executeUpdate(conn);

Why they implemented it like that is beyond me.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
I've been developing software for about 5 years now, specializing mostly in industrial and scientific programming. I'm experienced in C/C++ and C# both on Windows and Linux. I'm also fairly experienced in SQL as well.

Comments and Discussions

 
GeneralMy vote of 2 Pin
emartinho29-Oct-12 5:05
emartinho29-Oct-12 5:05 
GeneralRe: My vote of 2 Pin
VentsyV3-Nov-12 12:28
VentsyV3-Nov-12 12:28 
The documentation/examples I found do not make it clear that the parameters need to be in order. When I saw the parameter holders used were p1 ... pn, I assumed that the parsing would work similarly to what C# is doing with the String.Format function:

C#
String sqlStatement = String.Format("Update Table Set field1 = {1}, field2 = {2} Where field0 = {0}", value0, value1, value2);


Why wouldn't that be a reasonable expectation?
This is very easy to implement - add all values to a list, then replace each Pn with the Nth value from the list. The complexity is still O(n), but it's much more flexible solution.

modified 3-Nov-12 22:58pm.

GeneralRe: My vote of 2 Pin
emartinho9-Nov-12 17:29
emartinho9-Nov-12 17:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.