KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: INSERT Time values using SQL-compatible text formatted arrays
PRODUCT: 4D | VERSION: 12.1 | PLATFORM: Mac & Win
Published On: August 9, 2011

Assume the goal is to take advantage of the new feature introduced with v12.1 that being to take advantage of an external database. The restriction is that an external database can only be accessed via 4D SQL.

To populate this database the SQL command INSERT INTO will have to be used. The fast way to do this is to load up arrays and include these arrays in the VALUES clause of the SQL statement. Consider the example code below:

ARRAY LONGINT($ID_aL;0)
ARRAY TEXT($Name_aT;0)
ARRAY TEXT($Addr_aT;0)
ARRAY TEXT($User_aT;0)
ARRAY TEXT($Pass_aT;0)
ARRAY DATE($Date_aD;0)
ARRAY LONGINT($Time_aL;0)

ALL RECORDS([MyTable])
SELECTION TO ARRAY([MyTable]ID;$ID_aL\
    ;[MyTable]Name;$Name_aT\
    ;[MyTable]Address;$Addr_aT\
    ;[MyTable]UserName;$User_aT\
    ;[MyTable]Password;$Pass_aT\
    ;[MyTable]JoinDate;$Date_aD\
    ;[MyTable]JoinTime;$Time_aL)

$SOA:=Size of array($ID_aL)
ARRAY TEXT($Time_aT;$SOA)
For ($Ndx;1;$SOA)
    $Time_aT{$Ndx}:=Time string($Time_aL{$Ndx})
End for

Begin SQL
   USE DATABASE DATAFILE :$ExtDB_Path_T;

   INSERT INTO MyTable ( ID, Name, Address,
      UserName, Password, Res_Date, Res_Time )
   VALUES (:$ID_aL, :$Name_aT, :$Addr_aT,
       :$User_aT, :$Pass_aT, :$Date_aD, :$Time_aT );

   USE DATABASE SQL_INTERNAL;
End SQL


On the surface this is quite simple and fairly straight forward. The hurdle in this exercise is the time field "JoinTime." Using SELECTION TO ARRAY the values of this field are, as usual, captured into a longint array. But herein is the issue; 4D SQL does not directly translate a longint array into a time value.

The solution to the issue is to convert the longint values in $Time_aL into string using in text array $Time_aT using the command TIME STRING. TIME STRING produces a string in the format of HHJ:MM:SS. 4D SQL has no problem converting strings in this format into time values in the INSERT INTO statement.

Commented by Charlie Vass on August 29, 2011 at 7:50 AM
Basically, yes. This is standard behavior of SQL, not just 4D SQL.

For a immediate example of what all can be done for importing text as all kinds of data types look at the commands SQL EXPORT DATABASE, SQL EXPORT SELECTION, and SQL EXECUTE SCRIPT. The export and import is all in text. This Tech Tip simple expands to show how to use arrays in the process.
Commented by Johan Braun on August 29, 2011 at 4:10 AM
Do i read this that all data formats that can be represented can be inserted into the database as text and then converted by 4D to the appropriate format? Is there any documentation about this?