2

I just want to know, if a value, like an int for example, is actually converted into a string before being sent as part of the command.

pstmt = con->prepareStatement("INSERT INTO test(id) VALUES (?)");

Usually, the ? is replaced with:

pstmt->setInt(1,155);

What I'm curious about is: could I simply make the command:

pstmt = con->prepareStatement("INSERT INTO test(id) VALUES (155)");

And execute it? Would it be the same as setInt? (basically that would mean that the int value is converted to a string before being set as part of the command)

1 Answer 1

2

While both methods are perfectly legitimate, there is one major difference.

  • When you prepare a SQL statement, you precompile it and substitute values using setInt().
  • When you hardcode the actually values, you have to precompile the SQL every time.

Please note when it comes to Com_stmt_prepare and Com_stmt_execute

  • only Com_stmt_execute would increment
  • both increment in the MySQL Server Status when you hardcode

Prepared statements are only to your advantage if you maintain persistent or pooled connections. This is the case because certain data structures must be allocated on the server-side. If you connect and disconnect all the time, you have to construct and destroy the prepared statement along with a host of other connection-based resources (See my old post How costly is opening and closing of a DB connection?)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.