Embedded SQL in C

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; (1) main () {exec SQL BEGIN DECLARE SECTION; (2) char firstname [13]; char userid [9]; char passwd [19]; exec SQL end declare section; exec SQL connect to sample; (3) exec SQL SELECT FIRSTNME INTO: firstname (4) FROM employeeWHERE LASTNAME = 'johnson '; (4) printf ("First name = % s \ n", firstname); EX Ec SQL connect reset; (5) return 0;} The above is a simple static SQL statement embedded application. It includes the main part of Embedded SQL: (1) the include SQLCA statement in 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.