Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

connect to database

Can we connect to a database using C ?
0
intrude
Asked:
intrude
1 Solution
 
brettmjohnsonCommented:
Yes.  Nearly all ODBC interface libraries are ANSI C.

0
 
intrudeAuthor Commented:
Could you please give me a small example to show this.

thanks,
0
 
pankajtiwaryCommented:
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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now