Database Programming with C

Submitted by: 

C language is known to be one of the most popular programming languages since 1972. It is known as the general purpose structured programming language that can be used to develop various applications. Since then many applications have been and are developed using C programming language.

As we all know that even a small application nowadays needs to store, retrieve and manipulate some data in the database. This has increased the need for database programming capability to be included in every language. C is a language that has adopted this feature and includes methods to connect to the database and perform various database manipulating functions.

To perform this task C programming language uses embedded SQL method and Pro*C compiler. Whenever such programs are written, the SQL statements in C program are replaced with function calls from SQL runtime library by the Pro*C compiler. In this way, the Pro*C compiler generates a pure C code that can be processed by the C compiler.

All the SQL statements are included within the C program or code lines. For example, if you want to select the salary of an employee with employee number 123 into an integer variable “sal” then the statements would be as:

Int sal;

EXEC SQL SELECT salary INTO :sal FROM Emp WHERE empno=123;

printf("Salary of employee number 123 is %d\n", sal);

SQL statements that are included in the C program should start with EXEC SQL which instructs the compiler to execute the SQL statements function and call the appropriate method for the same. Like all other C statements, the SQL statements included in the program should end with a semicolon.

style="display:block; text-align:center;"

However, you may declare host variables as per the C programming language, it is important to note that the host variable you want to use with the database should be referred with a colon in the SQL statements. Like in the example we have used “:sal” instead of “sal”. It is important to note that the use of colon with host variable is limited to SQL statement and variable should be used without colon when using in C statements as we have used in the following statement of the code above:

printf("Salary of employee number 123 is %d\n", sal);

Similarly, if you want to insert some values using SQL statements in C programming language then you should use the same format. For example:

EXEC SQL INSERT INTO emp(empno, name, dept, manager)VALUES(:a, :s, :d, :k);

You should be careful while using host variables to deal with SQL statements in C programming language. Like, register storage-class specifier cannot be used as host variables. One of the problem in using database programming with C is that many a times pointer arithmetic expressions results in error even if used as host variable references even if they have a lvalue. This makes the task cumbersome sometimes.

One of the other important point to note is that preprocessor directives defined by use of  “#define” are not  recognized by Pro*C compiler and hence cannot be used with SQL statements. Therefore, if you are about to use preprocessor directives to define a variable then make sure that you are not willing to use it in any of the SQL statement otherwise it would result in an error.

SQL statements can also be used with various other C programming language features such as structures, unions, arrays, loop control structures, functions and pointers  to achieve other database programming tasks.


nice description


have example for this system

How to add a user in mysql using c or c++ code

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe>
  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.