>I need to execute SQL queries , create, and update statements from
within C.
I have been searching for two weeks to find an easy way of doing it
without success.
Does anyone has a small running sample C Code that execute SQL queries
???
or if you can point me in the right direction
I am using MS Visual C++ as a developping environment on windows !!!
(although my program is a C application)
Some databases provide an interface using standard C with an
additional library added, with no fancy tricks, preprocessors, or
extended syntax beyond that of C. (How you execute SQL is going
to be *HIGHLY* dependent on what database you are using, even given
a specific C compiler on a specific platform.) You make a string
with a query in it and then execute it. sprintf() is often useful
here:
#include <stdio.h>
#include <mysql.h>
MYSQL *m;
char querybuf[10240]; /* hope this is big enough */
char *host;
m = mysql_connect( /* appropriate args here */);
... /* check that m is not NULL */
... /* host points at something valid here */
sprintf(querybuf,
"INSERT INTO spammers(ip, date) VALUES ('%s', now())",
host);
mysql_query(m, querybuf); /* may want to test return value */
Note that if host points (or could point) to a string with funny
characters in it, you are vulnerable to a SQL injection attack.
Quoting those characters is beyond the scope of this post.
Gordon L. Burditt