Solved

connect to database

Posted on 2004-08-19
3
203 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Picking random number 8 170
Handling string inputs in C/Linux 23 168
Problem to scan all sheets 3 83
How to creat good software interface ? 1 57
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…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

743 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

12 Experts available now in Live!

Get 1:1 Help Now