Connect database with C language

Source: Internet
Author: User
Connect to the sample database and query the firstname information of Johnson.
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include "util. H"
# Include <sqlca. h>''
Exec SQL include sqlca;

Main ()
{
Exec SQL begin declare section;

Char firstname [13];
Char userid [9];
Char passwd [19];
Exec SQL end declare section;
Exec SQL connect to sample;

Exec SQL select firstnme into: firstname

from employee
where lastname = 'johnson ';
printf ("First name = % s/n", firstname );
exec SQL connect reset;
return 0;
}< br> the above is a simple application of static embedded SQL statements Program . It includes the main part of Embedded SQL:
(1) the include sqlca statement defines and describes the structure of sqlca. Sqlca is used for communication between applications and databases. The sqlcode returns the result status after the SQL statement is executed.
(2) The host variable is defined between begin declare section and end declare section. Host variables can be referenced by SQL statements or C language statements. It is used to pass data in the program to the Database Manager through SQL statements, or receive query results from the Database Manager. In SQL statements, there is a ":" mark before the main variable to show the difference.
(3) connect to a database before each database access. In this case, make sure that the database instance has been started.
(4) is a selection statement. It identifies the firstname of the row data whose lastname is "Johnson" in the table employee and places it in the firstname variable. This statement returns a result. Multiple results can be returned through a cursor. Of course, it can also contain update, insert, and delete statements.
(5) disconnect the database.
as shown in the preceding example, each embedded SQL statement starts with exec SQL, indicating that it is an SQL statement. This also tells the pre-compiler to embed SQL statements between exec SQL and. If an embedded SQL statement occupies multiple rows, you can use the "/" extension in the C program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.