Quantcast
Channel: MySQL Forums - Connector/C++
Viewing all articles
Browse latest Browse all 527

Prepared Statements - Incorrect behaviour (1 reply)

$
0
0
OS: Windows Vista, 32-bit
Compiler: Visual Studio 9 2008
Connecter: 1.0.5 binary and a local compiled from source.

// Statements
// **********

static void test_statements(Connection *con)
{
Statement *stmt;
stmt = con -> createStatement();

for (int index = 1; index <= 10; index++)
{
stringstream out;
out << index;
string query = "UPDATE test_2_events SET Name=";
query += "'T";
query += out.str();
query += "' WHERE ID=";
query += out.str();
stmt -> executeUpdate(query);
}

delete stmt;
}

// Prepared Statements
// **********

static void test_statements_prepared(Connection *con)
{
PreparedStatement *prep_stmt;
prep_stmt = con -> prepareStatement("UPDATE test_2_events SET Name=? WHERE ID=?");

for (int index = 5; index <= 10; index++)
{
stringstream out;
out << index;
string name = "T";
name += out.str();

// Nothing happens!
// Changing ID=? to ID=1 results in the name of ID=1 becomes 0 and not T(index).
prep_stmt -> setString(1, name);
prep_stmt -> setInt(2, index);
prep_stmt -> executeUpdate();
}

delete prep_stmt;
}

// End of code
// **********

Statements:
- This works correctly. It sets the name to T(index) for all IDs in the range 1-10.

Prepared Statements:
- Nothing happens unless ID=? gets changed to ID=1 etc. where the name will be set to 0 instead of T1.

I have used both the precompiled and compiled the connector from the source, but the result is the same.

Viewing all articles
Browse latest Browse all 527

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>