Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   embedded SQL in C (http://www.velocityreviews.com/forums/t318284-embedded-sql-in-c.html)

Fadi Komati 05-17-2004 03:56 PM

embedded SQL in C
 
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.

Thomas Matthews 05-17-2004 04:04 PM

Re: embedded SQL in C
 
Fadi Komati wrote:
> 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 news:comp.lang.c
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


Gordon Burditt 05-17-2004 06:21 PM

Re: embedded SQL in C
 
>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

Alan Balmer 05-17-2004 08:43 PM

Re: [OT] embedded SQL in C
 
On 17 May 2004 08:56:10 -0700, fadi.komati@act.com.lb (Fadi Komati)
wrote:

>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.

--
Al Balmer
Balmer Consulting
removebalmerconsultingthis@att.net


All times are GMT. The time now is 09:29 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.