Solved

connect to database

Posted on 2004-08-19
3
204 Views
Last Modified: 2010-04-15
Can we connect to a database using C ?
0
Comment
Question by:intrude
3 Comments
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 11848913
Yes.  Nearly all ODBC interface libraries are ANSI C.

0
 

Author Comment

by:intrude
ID: 11857407
Could you please give me a small example to show this.

thanks,
0
 
LVL 4

Accepted Solution

by:
pankajtiwary earned 75 total points
ID: 11867983
Well, it depends on what database you want to connect to. For example if the database is MySql, while installing the mysql client on your PC you will get the API for this very purpose. The library is called libmysqlclient.so.*

While connecting to the database (MySql), you just need to include the mysql.h header file provided as a part of the mysql client distribution and use the functions specified there with. You will find functions like mysql_connect() and mysql_real_connect() to connect to the database and then you can issue the query using the function mysql_real_query() and fetch the results using mysql_fetch_results() function. Just remember to link your client program with the mysqlclient library at the time of linking.

N.B. You can read a full details of the functions at the mysql website. Its just as good as calling normal functions and taking the return values.

Talking about oracle, things become much more complicated because it gives you much more control over the database transactions. The tougher method for fetching data from the database is by using oci library (again provided with the client) but hardly people use it. The best method is to use Pro*C. This is a kind of wrapper over the sqllib library(another client API for oracle but rather easy) in which you can directly enbed your sql statements within your C code. This C code (known as .pc file) is then given input to the Pro*C precompiler which converts all your Pro*C specific statements along with the sql ones into corresponding function calls (sqllib specifically). The output you get is a pure C program filled with normal oracle api calls. You can then compile this code using any of your C compiler to ptroduce the executable. To get more details about the Pro*C precompiler, visit oracle technoloy network Pro*C/C++ documentation site.

This is just a normal way of doing database transaction. Rest is all application dependent. Also this approach is platform independent I suppose. Any corrections by experts..??
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now