Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

connect to database

Posted on 2004-08-19
3
Medium Priority
?
210 Views
Last Modified: 2010-04-15
Can we connect to a database using C ?
0
Comment
Question by:intrude
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

661 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