UltraLite User's Guide
Tutorial: Build an Application Using Embedded SQL
Writing source files in embedded SQL
Although too simple to be useful, this example contains elements that must be present in every embedded SQL source file used for database access. The following describes the key elements in the sample program. Use these steps as a guide when creating your own embedded SQL UltraLite application.
Include the appropriate header files.
The sample program utilizes standard I/O, therefore the stdio.h header file has been included.
Define the SQL communications area, sqlca.
Use the following command:
EXEC SQL INCLUDE SQLCA;
This definition must be your first embedded SQL statement, so place it at the end of your include list.
Prefix SQL statementsAll SQL statements must be prefixed with the keywords EXEC SQL and must end with a semicolon. |
Define host variables by creating a declaration section.
Host variables are used to send values to the database server or receive values from the database server. Create a declaration section as follows:
EXEC SQL BEGIN DECLARE SECTION; long pid=1; long cost; char pname[31]; EXEC SQL END DECLARE SECTION;
For information on host variables, see Using host variables.
Call the embedded SQL library function db_init to initialize the UltraLite runtime library.
Call this function as follows:
db_init(&sqlca);
Connect to the database using the CONNECT statement.
To connect to the UltraLite sample database, you must supply the login user ID and password. Connect as user DBA with password SQL as follows:
EXEC SQL CONNECT "DBA" IDENTIFIED BY "SQL";
Insert data into database tables.
When an application is first started, its database tables are empty. Only when you choose to synchronize the remote database with the consolidated database will the tables be filled with values so that you may execute select, update or delete commands. Rather than using synchronization, however, you may also directly insert data into the tables. Directly inserting data is a useful technique during the early stages of UltraLite development.
If you use synchronization and your application fails to execute a query, it can be due to a problem in the synchronization process or due to a mistake in your program. To locate the source of failure may be difficult. On the other hand, if you directly fill tables with data in your source code rather than perform synchronization, then, if your application fails, you will know automatically that the failure is due to a mistake in your program.
After you have tested that there are no mistakes in your program, remove the insert statements and replace them with a call to the ULSynchronize function to synchronize the remote database with the consolidated database.
For information on adding synchronization to an UltraLite application, see Adding synchronization to your application.
Execute your SQL query.
The sample program executes a select query that returns one row of results. The results are stored in the previously defined host variables cost
and pname
.
Perform error handling.
The sample program executes a select request that returns an error code, sqlcode
. This code is negative if an error occurs; SQL_NOTFOUND is returned if there are no query results. The sample program handles these errors by returning -1.
Disconnect from the database.
You should rollback or commit any outstanding changes before disconnecting.
To disconnect, use the DISCONNECT statement as follows:
EXEC SQL DISCONNECT;
End your SQL work with a call to the library function db_fini:
db_fini(&sqlca);