Execute an INSERT, UPDATE or DELETE command

INSERT, UPDATE or DELETE are treated in the same way as the SELECT command (see the HOWTO about executing a SELECT command), except that the function to execute the command is different because the returned value is not a data model but a success/failure value.

The following codes illustrates parsing and executing an UPDATE statement:

GdaSqlParser *parser;
GdaStatement *stmt;
GError *error = NULL;

parser = gda_connection_create_parser (cnc);
if (!parser) {
    /* the database provider used by cnc does not implement its own parser,
     * let's use a generic one */
    parser = gda_sql_parser_new ();
stmt = gda_sql_parser_parse_string (parser, "UPDATE customers set name='Joe' WHERE id=123", NULL, &error);
g_object_unref (parser);
if (!stmt) {
    /* there was an error while parsing */
else {
    gint res;
    res = gda_connection_statement_execute_non_select (cnc, stmt, NULL, NULL, &error);
    if (res == -1) {
        /* there was an error while executing the statement */
    else if (res >= 0) 
        g_print ("Command Ok, %d row(s) impacted\n", res);
        g_print ("Command Ok, number of rows impacted is not reported\n");
g_object_unref (stmt);

Note that the example above includes in the SQL statement the values of the data to use in the UPDATE (namely 'Joe' and '123'). Even though it's simple, a better practice is to use variables, as it prevents SQL injection and avoids formatting problems. For more intormation, see the GdaSqlParser object's documentation; or the section about convenience functions