Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

connect to database

Posted on 2004-08-19
3
Medium Priority
?
211 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 300 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

886 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