embedded SQL in C

F

Fadi Komati

Dear C Gurus,
I am currently working on finalysing my final project for Masters
degree.
However, i have stumbled into a tiny technical detail.
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)

Thank you for your help.
 
T

Thomas Matthews

Fadi said:
Dear C Gurus,
I am currently working on finalysing my final project for Masters
degree.
However, i have stumbled into a tiny technical detail.
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)

Thank you for your help.

The _standard_ method to do this is to place your SQL
statements into a string then send them to their destination.
Perhaps through a stream interface.

A non-portable method is to consult your OS and see how
to send commands to another application, such as a database
driver. How to do this is off-topic in so you will have to research a platform specific newsgroup.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
G

Gordon Burditt

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
 
A

Alan Balmer

Dear C Gurus,
I am currently working on finalysing my final project for Masters
degree.
However, i have stumbled into a tiny technical detail.
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)

Thank you for your help.
{OT}
What you probably want is "Embedded SQL", or ESQL. This is
accomplished by using a preprocessor supplied by the database
developer. While the SQL itself is standardized, I don't think the
embedding method is, so you'll need documentation from the vendor.

What database are you using? Ask in a newsgroup which discusses that
database.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top