As seen in the Executing Queries page, queries are performed by calling
mcosql_execute_query()
to execute the SQL select statement. However, SQL statementsinsert
,update
,delete
andcreate
are performed by calling the functionmcosql_execute_statement()
as in the following examples:/* Add a Person record */ rs = mcosql_execute_statement(db, NULL, NULL, "insert into Person (name,address,position,salary,rating,manager) values (%s,%s,%s,%u,%f,%p)", name, address, position, salary, rating, &manager_id ); /* Update the salary and rating for the record with the specified name */ rs = mcosql_execute_statement(db, NULL, &rc, "update Person set salary=%u, rating=%f where name=%s", salary, rating, name ); /* Delete all Person records */ rs = mcosql_execute_statement(db, NULL, NULL, "delete from Person" ); /* Create a table and a hash index */ rs = mcosql_execute_statement(db, NULL, NULL, "create table t1(s string, i integer)" ); rs = mcosql_execute_statement(db, NULL, NULL, "create unique index t1_pk on t1(i) using hash" );Note that, in the above examples, the application variables
name
,address
andposition
are character strings,salary
is an unsigned integer value,rating
is a floating point value, and&manager_id
is the address of anint64_t
value. These values are substituted into the SQL statement according to rules, similar to those for standardprintf()
format strings, that are elaborated in the SQL Statement Argument Substitution page.The value returned by
mcosql_execute_statement()
indicates success or failure with the return codes defined in header file “include/sql/sqlc.h
”, and it takes a variable length argument list consisting of :
db
– the database enginetrans
- a transaction handle orNULL
if the statement is to be executed in auto-commit mode.n_records
– the number of records affected (anint64_t
value)statement
– the SQL select statement to be executed…
- a list of 0 or more arguments to be inserted into the statement. (The rules for substitution of values into the SQL statement are elaborated in the SQL Statement Argument Substitution page.)