Resources | Resources |



Creating a table entry/inserting data

Preparing to insert data in a table

Prepared statements, created by using dbc_IConnection_CreatePrepare, are much like statements except that they allow variables to be bound after statement creation time. Here a prepared statement is created that will insert a new row into the messages table. Notice that variables in the statement text are represented with a "?" character.

char* stmt = “INSERT INTO messages table VALUES (NULL, ?, ?)”;

/* Create the Prepared Statement */
nResult = dbc_IConnection_CreatePreparedStatement(piConnection, stmt, &piStatement);

Inserting data in a table

Before executing the PreparedStatement, all variables must be bound to values. In this example a text value is bound to the first variable and an integer value to the second variable.

/* Bind values*/

nResult = dbc_PreparedStatement_BindText(piStatement, 1, “Having fun with dbc!”);
nResult = dbc_PreparedStatement_BindInt(piStatement, 2, 1);

After the variables have been bound, the statement is ready for execution. Results are not expected from this particular statement, so ExecuteUpdate is used to execute the command.

/* Execute the statement*/
nResult = dbc_IPreparedStatement_ExecuteUpdate(piStatement);

/* Bind more values here */
nResult = dbc_IPreparedStatement_BindText(piStatement, 1, “Building my application”);

Note: The compiled statement can be re-used to insert another row into the table. However, the statement needs to be reset and new variables have to be bound to the statement before calling ExecuteUpdate again.