Print this page.  If your browser doesn't allow JavaScript, right-click this page and choose Print from the popup-menu.        
Embedded SQL in C
Category: SQL
Introduction

Embedded SQL means that you can use SQL statements through a host programming language, for example, C/C++. C/C++ is used as an example in this article.

You include SQL statements as part of the source code for an application. The source code is compiled and linked with the appropriate language-specific facilities. The SQL statements are executed in the context of the application.

Mimer SQL supports embedded SQL for C/C++ according to the ISO standard.

Function

The ESQL preprocessor processes SQL statements embedded in the host language. SQL statements are identified by the leading delimiter "exec sql" and terminated by a semicolon (;), e.g. "exec sql DELETE FROM HOTEL;".

You must declare host variables used in SQL statements within the SQL DECLARE SECTION, delimited by the statements BEGIN DECLARE SECTION and END DECLARE SECTION.

You can find detailed information on Embedded SQL in Mimer SQL Documentation Set, PDF-file. (See the Programmer's Manual.)

Example

The following is an example that connects to the database defined as the default. All rows are then fetched from the table INFORMATION_SCHEMA.TABLES. A table named "Straße" is created, and dropped afterwards, to show the multi lingual support.

The example is stored in a file called simple.ec.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>

#define BUFLEN 129
#define lengthof(x) (sizeof(x)/sizeof(x[0]))

exec sql BEGIN DECLARE SECTION;
static char SQLSTATE[6];
exec sql END DECLARE SECTION;

main()
{
exec sql BEGIN DECLARE SECTION;
nvarchar schema[129], table[129];
varchar type[21];
exec sql END DECLARE SECTION;

char print_buffer[BUFLEN*6]; /* Multibyte character buffer */

setlocale(LC_ALL, "");

exec sql DECLARE MYCURSOR CURSOR FOR
SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE
FROM INFORMATION_SCHEMA.TABLES;

/* Connect to default database */

exec sql CONNECT TO '' USER 'SYSADM' USING 'SYSADM';
exec sql CREATE TABLE Straße (col1 integer);
exec sql OPEN MYCURSOR;

while (1) {
exec sql FETCH MYCURSOR INTO :schema, :table, :type;
if (strcmp(SQLSTATE, "02000") == 0) break; /* No more rows */

wcstombs(print_buffer, schema, lengthof(print_buffer));
printf("%-25s\t", print_buffer);
wcstombs(print_buffer, table, lengthof(print_buffer));
printf("%-30s\t%-20s\n", print_buffer, type);
}

exec sql CLOSE MYCURSOR;
exec sql COMMIT;
exec sql DROP TABLE Straße CASCADE;
exec sql DISCONNECT ALL;
exit(0);
}


The program is preprocessed as follows:

esql −c simple.ec

The resulting file, "simple.c", is compiled using a C-compiler and then linked to the Mimer SQL shared library or DLL. Usually, as an aid to this step, an example makefile or a build procedure is available in the software distribution package.

When executing the resulting "simple" program, a list of the items selected will be displayed.

You can find a more complete example in distributed example program example.ec

Benefits

Embedded SQL is a straightforward way of writing applications that access databases.

Links

Example file: distributed example program example.ec

Mimer SQL Documentation Set, PDF-file. (See the Programmer's Manual.)

Mimer SQL Documentation Set, html navigation. (See the Programmer's Manual.)

Last updated: 2013-02-27

 

Powered by Mimer SQL

Powered by Mimer SQL