Connect SQLite from gnu COBOL in Windows

Submitted by Dickens A S on Mon, 07/12/2021 - 08:37

This article demonstrates how connect SQLite3 from COBOL using MSYS2 in WIndows

Download SQLite3 amalgamation https://www.sqlite.org/2021/sqlite-amalgamation-3360000.zip

get sqlite3.c from the above zip file and put it in the same place where you cobol is compiles

"test.db" is pre created with DBeaver plugin in eclipse

Install sqlite3 in MSYS2 using

$ pacman -S libsqlite libsqlite-devel mingw-w64-x86_64-sqlite3

Compile the C code

$ cobc -c -lsqlit3 sqlite3.c

The COBOL code with callback ENTRY (test_cobol.cob)

       IDENTIFICATION DIVISION.
       PROGRAM-ID. HELLO-WORLD.

       DATA DIVISION. 
       WORKING-STORAGE SECTION.
       01 DB                 POINTER.
       01 ERR                POINTER.
       01 RC                 PIC 9 COMP-5.
       01 DBNAME             PIC X(08).
       01 SQLQUERY           PIC X(100).
       01 CALLBACK USAGE PROCEDURE-POINTER.


       PROCEDURE DIVISION.
       
       SET DB         TO NULL
       SET ERR        TO NULL

       MOVE Z"test.db" TO DBNAME

       DISPLAY "RUNNING sqlite3_open"

       CALL "sqlite3_open" USING
           BY REFERENCE  DBNAME
           BY REFERENCE  DB
           RETURNING     RC
       END-CALL
        
       IF RC NOT = ZERO
           DISPLAY "ERROR OPENING DATABASE."
       ELSE
           DISPLAY "DATABASE OPENED."
       END-IF
        
       SET CALLBACK TO ADDRESS OF ENTRY "SQLITE-CALLBACK".
        
       MOVE "SELECT * FROM DEMO;" TO SQLQUERY
           
       CALL "sqlite3_exec" USING
           BY VALUE     DB
           BY REFERENCE SQLQUERY
           BY VALUE     CALLBACK
           BY VALUE     0
           BY REFERENCE ERR
           RETURNING RC
       END-CALL
        
       CALL "sqlite3_close" USING
           BY REFERENCE DB
       END-CALL

       STOP RUN.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. SQLITE-CALLBACK.
       DATA DIVISION. 
       WORKING-STORAGE SECTION.
       LINKAGE SECTION.
       01 COLUMN-ID         PIC 99.
       01 COLUMN-NAME       PIC X(20).
       01 ARGC              PIC 99 COMP-5.
       01 NOTUSED           POINTER.
       01 ARGV.
          03  FIRSTCOLUMN   POINTER.
          03  SECONDCOLUMN  POINTER.
       01 AZCOLNAME         POINTER.
       PROCEDURE DIVISION
            USING
           BY VALUE NOTUSED
           BY VALUE ARGC
           BY REFERENCE ARGV
           BY REFERENCE AZCOLNAME.
        SET ADDRESS OF COLUMN-ID   TO FIRSTCOLUMN
        SET ADDRESS OF COLUMN-NAME TO SECONDCOLUMN
       DISPLAY COLUMN-ID "|" COLUMN-NAME
       
       GOBACK.
       END PROGRAM SQLITE-CALLBACK.

Compile COBOL code

$ cobc -x -lsqlite3 test_cobol.cob sqlite3.o -conf=/mingw64/share/gnucobol/config/default.conf

Here sqlite3.o is the compiled binary for amalgamation 

Run the EXE

$ ./test_cobol
RUNNING sqlite3_open
DATABASE OPENED.
1|rama
2|seetha

The above values are retrieved from table named "demo" with two columns "id" and "name"

The SQL Query is "SELECT * FROM DEMO;"

Finished!

 

 

 

 

 

 

Add new comment