embedded SQL in C

Discussion in 'C Programming' started by Fadi Komati, May 17, 2004.

  1. Fadi Komati

    Fadi Komati Guest

    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.
     
    Fadi Komati, May 17, 2004
    #1
    1. Advertising

  2. 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
     
    Thomas Matthews, May 17, 2004
    #2
    1. Advertising

  3. >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
     
    Gordon Burditt, May 17, 2004
    #3
  4. Fadi Komati

    Alan Balmer Guest

    Re: [OT] embedded SQL in C

    On 17 May 2004 08:56:10 -0700, (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
     
    Alan Balmer, May 17, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Colin Paul Gloster
    Replies:
    48
    Views:
    1,892
    Colin Paul Gloster
    Apr 10, 2007
  2. ecoolone
    Replies:
    0
    Views:
    768
    ecoolone
    Jan 3, 2008
  3. Thomas Dodds

    Databind an embedded control in an embedded datagrid

    Thomas Dodds, Jul 26, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    402
    Thomas Dodds
    Jul 26, 2004
  4. Trans
    Replies:
    11
    Views:
    303
    micathom
    Sep 5, 2007
  5. Num GG
    Replies:
    2
    Views:
    366
    Num GG
    Nov 17, 2008
Loading...

Share This Page