Share Your Source Code or Article

Do you have source code, articles, tutorials, web links, and books to share? You can write your own content here. You can even have your own blog.

Submit now...

Database Programming Made Easy

This tutorial will teach you step by step on how to connect and manipulate database. If you'd like to suggest a tutorial please write a comment at the bottom of this article.

Read more...

Hire Us to Do Your Work

Do you want a customized system? Do you want to setup your own website to do business? Then we are here to help you in your programming needs.

Read more...

Search

Database Programming with C
in
Language: C/C++
Category: Databases

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.

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.

0
Your rating: None


NICE

NICE

nice description

nice description

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <java>, <java5>, <javascript>, <mysql>, <php>, <sql>, <vb>, <vbnet>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • You may use [inline:xx] tags to display uploaded files or images inline.
  • Links to specified hosts will have a rel="nofollow" added to them.

  • You may insert videos with [video:URL]

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.

Step by Step Java Tutorial

In this tutorial you will learn how to program with Java. It has a rich of information to be educated well with Java.

Read more...

Do You Have Question?

Do you have any question related to computer programming? Visit our forum and post new topic on the category you like. Be gentle when asking a question.

Ask now...

Point of Sale

Point of Sale is very useful especially for supermarkets or restaurants. I have included a barcode scanner in this program. Please check it out.

Read more...