External Data Source Commands

4D - Documentation   Français   English   German   4D v11 SQL, Command Theme List   4D v11 SQL, Command Alphabetical List   4D v11 SQL, Constant Theme List   Back   Previous   Next

version 11 (Modified)


Overview

This theme contains commands that allow 4D to access, via standard protocols, data stored in other applications. In the current version of 4D, only ODBC commands are available.

The ODBC (Open DataBase Connectivity) standard specifies a library of standardized functions. These functions allow an application such as 4D to access any ODBC-compatible data management system (databases, spreadsheets, etc.) via SQL language.

Note: 4D also allows data to be imported from and exported to an ODBC source in Design mode. For more information, please refer to the 4D Design Reference manual.

The high-level ODBC commands in the "External Data Source" theme of 4D can be used to implement simple solutions allowing 4D applications to communicate with ODBC data sources. If your applications require more extensive support of ODBC standards, you will need to have the "low level" ODBC plug-in for 4D, 4D ODBC Pro.

How built-in ODBC commands work

The built-in ODBC commands of 4D implement the following principles:

The scope of a connection is the process. If you want to manage several simultaneous connections, you must start a process by ODBC LOGIN.

The ODBC CANCEL LOAD command can be used to execute several SELECT requests in the same connection.

You can use most of the commands of this theme with the 4D internal SQL kernel.

You can intercept any ODBC errors generated during the execution of one of the ODBC commands using the ON ERR CALL command. The ODBC GET LAST ERROR command can be used in this case to obtain additional information.

Correspondence of data types

The following table lists the correspondences that are automatically established by 4D between 4D and SQL data types:

4D TypeSQL Type
C_STRINGSQL_C_CHAR
C_TEXTSQL_C_CHAR
C_REALSQL_C_DOUBLE
C_DATESQL_C_TYPE_DATE
C_TIMESQL_C_TYPE_TIME
C_BOOLEANSQL_C_BIT
C_INTEGERSQL_C_SHORT
C_LONGINTSQL_C_SLONG
C_BLOBSQL_C_BINARY
C_PICTURESQL_C_BINARY
C_GRAPHSQL_C_BINARY

Referencing 4D expressions in ODBC requests

4D provides two ways for inserting 4D expressions (variables, arrays, fields, pointers, valid expressions) into ODBC requests: direct association and the setting of parameters using ODBC SET PARAMETER.

Direct association can be carried out in two ways:

Insertion of the name of the 4D object between the << and >> characters in the text of the request.

Precede the reference with a colon ":".

Examples:

   ODBC EXECUTE("INSERT INTO emp (empnum,ename) VALUES (<<vEmpnum>>,<<vEname>>)")
   ODBC EXECUTE("SELECT age FROM People WHERE name= :vName")

In these examples, the current values of the 4D vEmpnum, vEname and vName variables will replace the parameters when the request is executed. This solution also works with 4D fields and arrays.

This easy-to-use syntax nevertheless has the drawback of not being compliant with the SQL standard and of not allowing the use of output parameters. To remedy this, you can use the ODBC SET PARAMETER command. This command can be used to set each 4D object to be integrated into a request as well as its mode of use (input, output or both). The syntax produced is thus standard. For more information, please refer to the description of the ODBC SET PARAMETER command.

1. This example executes an ODBC request that directly uses the associated 4D arrays:

   ARRAY TEXT(MyTextArray;10)
   ARRAY LONGINT(MyLongintArray;10)

   For(vCounter;1;Size of array(MyTextArray))
      MyTextArray{vCounter}:="Text"+String(vCounter)
      MyLongintArray{vCounter}:=vCounter
   End for
   ODBC LOGIN("mysql";"root";"")
   SQLStmt:="insert into app_testTable (alpha_field, longint_field) VALUES (<<MyTextArray>>, <<MyLongintArray>>)"
   ODBC EXECUTE(SQLStmt)

2. This example can be used to execute an ODBC request that directly uses the associated 4D fields:

   ALL RECORDS([Table 2])
   ODBC LOGIN("mysql";"root";"")
   SQLStmt:="insert into app_testTable (alpha_field, longint_field) VALUES (<<[Table 2]Field1>"+">,<<[Table 2]Field2>>)"
   ODBC EXECUTE(SQLStmt)

3. This example lets you execute an ODBC query by directly passing a variable via a dereferenced pointer:

   C_LONGINT($vLong)
   C_POINTER($vPointer)
   $vLong:=1
   $vPointer:=->$vLong
   ODBC LOGIN("mysql";"root";"")
   SQLStmt:="SELECT Col1 FROM TEST WHERE Col1=:$vPointer"
   ODBC EXECUTE(SQLStmt)

Retrieving values in 4D

Retrieving values in 4D that result from ODBC requests is carried out in two ways:

Using the additional parameters of the ODBC EXECUTE command (recommended solution).

Using the INTO clause in the SQL query itself (solution reserved for special cases).



4D - Documentation   Français   English   German   4D v11 SQL, Command Theme List   4D v11 SQL, Command Alphabetical List   4D v11 SQL, Constant Theme List   Back   Previous   Next