SQL using C

M

MJK

Hello everybody,

I am almost a new C programmer and now I need to use SQL within my C
codes. Does anybody have any idea about good examples on SQL using C?
I have not any experience using SQL before.

Thanks,
MJK
 
K

Kenneth Brody

MJK said:
Hello everybody,

I am almost a new C programmer and now I need to use SQL within my C
codes. Does anybody have any idea about good examples on SQL using C?
I have not any experience using SQL before.

You can't, using only C as discussed here. (ie: as defined by the
standard.)

You can, however, do so by using the platform-specific extensions
that are provided by your SQL implementation. How to do so depends
on your SQL server, which also determines where you need to go to
get answers.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
J

jacob navia

MJK said:
Hello everybody,

I am almost a new C programmer and now I need to use SQL within my C
codes. Does anybody have any idea about good examples on SQL using C?
I have not any experience using SQL before.

Thanks,
MJK

All the databases now provide a C interface.
They usually provide a lot of examples with their documentation.
You just look at the documentation and you will find
a lot of examples.
 
M

Mark McIntyre

Hello everybody,

I am almost a new C programmer and now I need to use SQL within my C
codes. Does anybody have any idea about good examples on SQL using C?
I have not any experience using SQL before.

You should ask this again in a newsgroup dedicated to your chosen
flavour of database. Each one has its own programming interface, and
none are particularly standardised. Any answer you get here (other
than 'read the manual') is likely to be wrong for a different RDBMS.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
T

Tor Rustad

MJK said:
Hello everybody,

I am almost a new C programmer and now I need to use SQL within my C
codes. Does anybody have any idea about good examples on SQL using C?
I have not any experience using SQL before.

Now, before starting any C programming towards a SQL database, I suggest
you start out with setting up e.g. MySQL, create database and table.

Then try out INSERT, SELECT, UPDATE and DELETE via the command line
interface.

I just uploaded a simple program, demonstrating the MySQL C API:

http://www.pg2.moo.no/C/index.html#C_snippets
 
A

Al Balmer

Hello everybody,

I am almost a new C programmer and now I need to use SQL within my C
codes. Does anybody have any idea about good examples on SQL using C?
I have not any experience using SQL before.
Depends too much on the database. Ask in a forum which deals with the
database you're writing for.
 
S

SM Ryan

# Hello everybody,
#
# I am almost a new C programmer and now I need to use SQL within my C
# codes. Does anybody have any idea about good examples on SQL using C?
# I have not any experience using SQL before.

If you want to use one particular system, you should consult its
documentation. Some include preprocessors that can do much of the
interface for you. (Every database system has different interface
and a different version of SQL. If you want to use multiple systems,
you have to deal with the fact there is no standard interface
and no standard SQL.)

And, yes, I know what the S of SQL stands for. It's a lie.
 
U

user923005

Hello everybody,

I am almost a new C programmer and now I need to use SQL within my C
codes. Does anybody have any idea about good examples on SQL using C?
I have not any experience using SQL before.

The people who are telling you to use a C API are out of their mind.
You should use:
1. an ODBC or SQL/CLI interface
http://en.wikipedia.org/wiki/Open_Database_Connectivity
http://en.wikipedia.org/wiki/Call_Level_Interface
http://www.sqlsummit.com/ODBCVend.HTM

OR
2. an OLEDB or .NET interface
http://en.wikipedia.org/wiki/OLE_DB
http://www.sqlsummit.com/DataProv.htm
http://www.sqlsummit.com/oledbVen.htm

It is also possible to use Embedded SQL in C, or Module Language:
http://www.itl.nist.gov/div897/ctg/sql_form.htm

If you use a proprietary database API, it is the least standardized
interface that you can choose (it will work only with that specific
database vendor) and may not age gracefully either. The behavior will
not be as cleary defined as with a well accpeted standard.

At any rate, your question is not really a C question, but a database
question.

A newsgroup dedicated to the database of your choice and/or the API of
your choice will be much more suitable.

HTH
 
K

karthikbalaguru

MJK said:
Hello everybody,

I am almost a new C programmer and now I need to use SQL within my C
codes. Does anybody have any idea about good examples on SQL using C?
I have not any experience using SQL before.

Interesting ..... :):)
Why do you want to do like that ? Can you explain your actual
requirement ?

Karthik Balaguru
 
T

Tor Rustad

user923005 said:
The people who are telling you to use a C API are out of their mind.


What part of "new C programmer" and "not any experience using SQL"
didn't you understand? :)


If MJK is playing around with the common LAMP stack (Linux + Apache +
MySQL + PHP), then the MySQL C API is a rather good option to get
started with minimum trouble, note that the C and PHP API's are very
similar.

Pulling in extra dependencies via e.g. ODBC, is a *really* bad advice
for beginners IMO. AFAIK, the PHP ODBC API doesn't even support MySQL.

Even for a professional programming project, I might see little problems
with using a vendor specific C API for some cases. Any non-trivial
application, will anyway be written with an abstraction DB layer, hiding
the actual DB interface, whatever it might be... ODBC, C API or embedded
SQL. Writing a new DB layer, to support another DB brack-end, is no
rocket science and will not affect the business logic anyway.

Where complications are not needed, I prefer KISS.
 
U

user923005

What part of "new C programmer" and "not any experience using SQL"
didn't you understand? :)

None of it.
If MJK is playing around with the common LAMP stack (Linux + Apache +
MySQL + PHP), then the MySQL C API is a rather good option to get
started with minimum trouble, note that the C and PHP API's are very
similar.

This C API:
http://dev.mysql.com/doc/refman/5.0/en/c.html
is not less complicated than ODBC and ties the user to a single
vendor.
Going to another database will require a complete rewrite.
Pulling in extra dependencies via e.g. ODBC, is a *really* bad advice
for beginners IMO.

Dependencies needed for ODBC:
1. ODBC lib
2. ODBC headers

Dependencies needed for Proprietary API:
1. Proprietary lib
2. Proprietary headers

If there is some clear improvement, I must have overlooked something.
AFAIK, the PHP ODBC API doesn't even support MySQL.
http://phplens.com/phpeverywhere/node/view/9

Even for a professional programming project, I might see little problems
with using a vendor specific C API for some cases. Any non-trivial
application, will anyway be written with an abstraction DB layer, hiding
the actual DB interface, whatever it might be... ODBC, C API or embedded
SQL. Writing a new DB layer, to support another DB brack-end, is no
rocket science and will not affect the business logic anyway.

If you use standards based APIs then the database becomes practically
irrelevant. (There is often some porting effort, but it is trivial
compared to changing from one proprietary API to another). If you use
a proprietary API, then the database is cast in stone. Migration
becomes a complete rewrite.
Where complications are not needed, I prefer KISS.

I don't think your example simplifies anything.

I also guess that if the OP was using LAMP (which requires some
sophistication), they would not have posted the entry level question
that was posted here.
 
T

Tor Rustad

user923005 said:
None of it.

Agreed. :p
This C API:
http://dev.mysql.com/doc/refman/5.0/en/c.html
is not less complicated than ODBC and ties the user to a single
vendor.

Getting started with MySQL C API require less fuzz, it works right out
of the box, and perhaps even more important, lots of samle PHP code
exist demonstrating usage of the API.

Going to another database will require a complete rewrite.

Why do you think this is relevant for a beginner?

When he has got everything up and running, he need to play around, get
familiar with the SQL cmd-line interface, learn SQL, write some very
simple C programs, and perhaps first use PHP as an easy entry to C
programming.

Changing the DB server...will surely *not* be on his mind, for a rather
long time!

Dependencies needed for ODBC:
1. ODBC lib
2. ODBC headers

Dependencies needed for Proprietary API:
1. Proprietary lib
2. Proprietary headers

If there is some clear improvement, I must have overlooked something.

ODBC will be designed *on top* of some proprietary API, as such this is
an extra layer, which might introduce bugs, require extra installation
and configuration.

Not long ago, I installed MySQL on a Windows box, now let us take a look:

C:\Programfiler\MySQL\MySQL Server 5.0\include>dir
[...]

28.08.2007 08:25 <DIR> .
28.08.2007 08:25 <DIR> ..
06.07.2007 13:24 13 170 config-win.h
06.07.2007 13:24 4 050 decimal.h
06.07.2007 13:24 3 866 errmsg.h
06.07.2007 13:24 6 729 keycache.h
06.07.2007 13:24 2 845 libmysql.def
06.07.2007 13:24 33 759 mysql.h
06.07.2007 13:24 17 026 mysqld_ername.h
06.07.2007 13:24 17 083 mysqld_error.h
06.07.2007 13:24 17 249 mysql_com.h
06.07.2007 13:24 1 175 mysql_embed.h
06.07.2007 13:24 2 097 mysql_time.h
06.07.2007 13:24 833 mysql_version.h
06.07.2007 13:24 1 810 my_alloc.h
06.07.2007 13:24 1 941 my_attribute.h
06.07.2007 13:24 3 872 my_dbug.h
06.07.2007 13:24 3 460 my_dir.h
06.07.2007 13:24 2 916 my_getopt.h
06.07.2007 13:24 43 277 my_global.h
06.07.2007 13:24 1 466 my_list.h
06.07.2007 13:24 3 853 my_net.h
06.07.2007 13:24 1 669 my_no_pthread.h
06.07.2007 13:24 27 965 my_pthread.h
06.07.2007 13:24 36 770 my_sys.h
06.07.2007 13:24 1 958 my_xml.h
06.07.2007 13:24 20 806 m_ctype.h
06.07.2007 13:24 8 120 m_string.h
06.07.2007 13:24 5 796 raid.h
06.07.2007 13:24 1 804 sql_common.h
06.07.2007 13:24 11 137 sql_state.h
06.07.2007 13:24 1 006 sslopt-case.h
06.07.2007 13:24 2 163 sslopt-longopts.h
06.07.2007 13:24 1 107 sslopt-vars.h
06.07.2007 13:24 1 300 typelib.h

the <mysql.h> is there, but where the heck is your ODBC header?!

Now, perhaps I have better luck on a Linux box, let see....

$ ls /usr/include/mysql
errmsg.h my_getopt.h mysqld_ername.h ndb
keycache.h my_global.h mysqld_error.h raid.h
m_ctype.h my_list.h mysql_embed.h sql_common.h
m_string.h my_net.h mysql.h sql_state.h
my_alloc.h my_no_pthread.h mysql_time.h sslopt-case.h
my_config.h my_pthread.h mysql_version.h sslopt-longopts.h
my_dbug.h my_semaphore.h my_sys.h sslopt-vars.h
my_dir.h mysql_com.h my_xml.h typelib.h

nooooooooooo!



Well, the PHP manual says this:

"The following databases are supported by the Unified ODBC functions: »
Adabas D, » IBM DB2, » iODBC, » Solid, and » Sybase SQL Anywhere. "

http://www.php.net/manual/en/ref.uodbc.php

If you use standards based APIs then the database becomes practically
irrelevant. (There is often some porting effort, but it is trivial
compared to changing from one proprietary API to another). If you use
a proprietary API, then the database is cast in stone. Migration
becomes a complete rewrite.

*IF* a new DB backend is needed, this can be done in a well-designed
program, I wouldn't consider swapping from e.g. MySQL API to an ODBC
back-end such a big deal.

Some 10 years ago, I wrote an ODBC back-end towards a SQL DB located on
a mainframe, that was no free lunch, buggy ODBC driver(s) made the
debugging a challenge. Back then, even after an ODBC patch was installed
on the mainframe, it didn't work as expected. Connections where dropped,
blocking network calls elsewhere, made my code freeze... different
behavoir on test and prod systems.

My code was part of a CA system, and was managing certificate
requests/resposes, certificate status and one-time passwords etc. In a
desperate attempt to reach the delivery date, we put the prod system in
debug, to find a work-around for the crappy ODBC stuff... late Friday,
in a small CA room, behind multiple locks, two people got trapped, when
an alarm went off and the doors refused to open. Perfect! There was
phone installed, but neither of us knew the number to the guard. LOL

In retrospect, I should have written a mainframe server myself, and used
TCP/IP to communicate with it. For sure, no blocking network calls would
have been made then, and any errors could have been fixed in time. OTOH,
ODBC must be perfect for Java and C++ programmers. *ironic*
 
U

user923005

Agreed. :p



Getting started with MySQL C API require less fuzz, it works right out
of the box, and perhaps even more important, lots of samle PHP code
exist demonstrating usage of the API.


Why do you think this is relevant for a beginner?

Because ODBC is extremely well documented. You can find thousands of
sample programs to get you started and many freely available or
commercial tool sets using ODBC to browse or discover against your
data source.
When he has got everything up and running, he need to play around, get
familiar with the SQL cmd-line interface, learn SQL, write some very
simple C programs, and perhaps first use PHP as an easy entry to C
programming.

Changing the DB server...will surely *not* be on his mind, for a rather
long time!
Dependencies needed for ODBC:
1. ODBC lib
2. ODBC headers
Dependencies needed for Proprietary API:
1. Proprietary lib
2. Proprietary headers
If there is some clear improvement, I must have overlooked something.

ODBC will be designed *on top* of some proprietary API, as such this is
an extra layer, which might introduce bugs, require extra installation
and configuration.

Not long ago, I installed MySQL on a Windows box, now let us take a look:

C:\Programfiler\MySQL\MySQL Server 5.0\include>dir
[...]

28.08.2007 08:25 <DIR> .
28.08.2007 08:25 <DIR> ..
06.07.2007 13:24 13 170 config-win.h
06.07.2007 13:24 4 050 decimal.h
06.07.2007 13:24 3 866 errmsg.h
06.07.2007 13:24 6 729 keycache.h
06.07.2007 13:24 2 845 libmysql.def
06.07.2007 13:24 33 759 mysql.h
06.07.2007 13:24 17 026 mysqld_ername.h
06.07.2007 13:24 17 083 mysqld_error.h
06.07.2007 13:24 17 249 mysql_com.h
06.07.2007 13:24 1 175 mysql_embed.h
06.07.2007 13:24 2 097 mysql_time.h
06.07.2007 13:24 833 mysql_version.h
06.07.2007 13:24 1 810 my_alloc.h
06.07.2007 13:24 1 941 my_attribute.h
06.07.2007 13:24 3 872 my_dbug.h
06.07.2007 13:24 3 460 my_dir.h
06.07.2007 13:24 2 916 my_getopt.h
06.07.2007 13:24 43 277 my_global.h
06.07.2007 13:24 1 466 my_list.h
06.07.2007 13:24 3 853 my_net.h
06.07.2007 13:24 1 669 my_no_pthread.h
06.07.2007 13:24 27 965 my_pthread.h
06.07.2007 13:24 36 770 my_sys.h
06.07.2007 13:24 1 958 my_xml.h
06.07.2007 13:24 20 806 m_ctype.h
06.07.2007 13:24 8 120 m_string.h
06.07.2007 13:24 5 796 raid.h
06.07.2007 13:24 1 804 sql_common.h
06.07.2007 13:24 11 137 sql_state.h
06.07.2007 13:24 1 006 sslopt-case.h
06.07.2007 13:24 2 163 sslopt-longopts.h
06.07.2007 13:24 1 107 sslopt-vars.h
06.07.2007 13:24 1 300 typelib.h

the <mysql.h> is there, but where the heck is your ODBC header?!

Now, perhaps I have better luck on a Linux box, let see....

$ ls /usr/include/mysql
errmsg.h my_getopt.h mysqld_ername.h ndb
keycache.h my_global.h mysqld_error.h raid.h
m_ctype.h my_list.h mysql_embed.h sql_common.h
m_string.h my_net.h mysql.h sql_state.h
my_alloc.h my_no_pthread.h mysql_time.h sslopt-case.h
my_config.h my_pthread.h mysql_version.h sslopt-longopts.h
my_dbug.h my_semaphore.h my_sys.h sslopt-vars.h
my_dir.h mysql_com.h my_xml.h typelib.h

nooooooooooo!

Well, the PHP manual says this:

"The following databases are supported by the Unified ODBC functions: »
Adabas D, » IBM DB2, » iODBC, » Solid, and » Sybase SQL Anywhere. "

http://www.php.net/manual/en/ref.uodbc.php

I admit that it is a lot harder to use on Linux than Windows (where
typically you will just click on the installer for your driver and
then connect to your data source).

Procedures to use ODBC connection to MySQL on Linux:
http://dbcusers.com/showthread.php?p=161
*IF* a new DB backend is needed, this can be done in a well-designed
program, I wouldn't consider swapping from e.g. MySQL API to an ODBC
back-end such a big deal.

If the project is small, it won't be too traumatic but any savings is
worth while.
Some 10 years ago, I wrote an ODBC back-end towards a SQL DB located on
a mainframe, that was no free lunch, buggy ODBC driver(s) made the
debugging a challenge. Back then, even after an ODBC patch was installed
on the mainframe, it didn't work as expected. Connections where dropped,
blocking network calls elsewhere, made my code freeze... different
behavoir on test and prod systems.

My code was part of a CA system, and was managing certificate
requests/resposes, certificate status and one-time passwords etc. In a
desperate attempt to reach the delivery date, we put the prod system in
debug, to find a work-around for the crappy ODBC stuff... late Friday,
in a small CA room, behind multiple locks, two people got trapped, when
an alarm went off and the doors refused to open. Perfect! There was
phone installed, but neither of us knew the number to the guard. LOL

In retrospect, I should have written a mainframe server myself, and used
TCP/IP to communicate with it. For sure, no blocking network calls would
have been made then, and any errors could have been fixed in time. OTOH,
ODBC must be perfect for Java and C++ programmers. *ironic*

I guess your experience with a buggy driver has left a bad taste in
your mouth.
And I have had similar experiences with database APIs (since I write
ODBC drivers, I usually start with the API myself to get access). I
have found a few cases where things were not properly documented or
did not work as documented, etc.

Probably, since I work with standards based drivers on a daily basis
my view is tainted so that I really cannot see the situation in a
completely neutral manner.
 
T

Tor Rustad

user923005 wrote:

[...]
I admit that it is a lot harder to use on Linux than Windows (where
typically you will just click on the installer for your driver and
then connect to your data source).

Procedures to use ODBC connection to MySQL on Linux:
http://dbcusers.com/showthread.php?p=161

I haven't used ODBC on Linux before, but if such a need appeared, I
would rather use GUI of a package manager, and access the compiled
version of program & libraries.

Compiling sources yourself, is the old way... using Linux these days is
much simpler. For example on a Debian/Ubuntu installation, all needed is
a one liner:

$ apt-get install unixodbc

there is also a package named 'libmyodbc', which might be better:

$ dpkg -l '*odbc*'
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Installed/Config-files/Unpacked/Failed-config/Half-installed
|/ Err?=(none)/Hold/Reinst-required/X=both-problems (Status,Err:
uppercase=bad)
||/ Name Version Description
+++-==============-==============-============================================
un gda2-odbc <none> (no description available)
un libmyodbc <none> (no description available)
un libqt3-mt-odbc <none> (no description available)
un libsqliteodbc <none> (no description available)
un odbc-postgresq <none> (no description available)
un php4-odbc <none> (no description available)
un php5-odbc <none> (no description available)
un tdsodbc <none> (no description available)
un unixodbc <none> (no description available)


Nevertheless, I think the C API ('libmysqlclient' package), is a better
starting point for beginners.

I guess your experience with a buggy driver has left a bad taste in
your mouth.
And I have had similar experiences with database APIs (since I write
ODBC drivers, I usually start with the API myself to get access). I
have found a few cases where things were not properly documented or
did not work as documented, etc.

Yes, some really bad memories. Others bugs, are a lot more irritating
than your own. :)

Probably, since I work with standards based drivers on a daily basis
my view is tainted so that I really cannot see the situation in a
completely neutral manner.

Our decision to use ODBC back then, was not really to be able interface
another SQL DB, companies rarely replace their mainframes, the CA system
was unplugged years ago, but the mainframe is still there.

The point was to avoid writing a mainframe server (using embedded SQL),
plus writing and test the network code needed to communicate with it, so
using ODBC looked as less work.

IIRC, my work-around involved, doing some of the ODBC calls in a
separate thread, and kill the thread if a dead-lock/timeout occurred,
clearly an ugly solution, which wouldn't be needed for a more robust
ODBC driver.
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top