如何从COBOL开始使用SQLite

时间:2016-10-10 11:53:38

标签: sqlite cobol

我是COBOL开发人员,我需要连接到SQLite数据库。我正在使用NetExpress。 我已经看到了guide以及此post,但我需要前一步。我不知道如何将这些c调用传递给COBOL。 该指南解释说:

sqlite3_open("test.db", &db)

sqlite3_exec(sqlite3*, const char *sql, sqlite_callback, void *data, char **errmsg)

我有一个数据库' test.db',有一个表&tabala'有2列(类型文本)。 我尝试使用这个COBOL解决方案进行打开/插入/选择:

  *
  *----------------------------------------------------------------*
  *                                                                *
  *                                                                *
  *                                                                *
  * SYSTEM                                                         *
  *                                                                *
  * PROGRAM-ID    Pxxxxxx
  *                                                                *
  * AUTHOR                                                         *
  *                                                                *
  * DATE          10/2016                                          *
  *                                                                *
  *                                                                *
  *                                                                *
  *                                                                *
  *----------------------------------------------------------------*
  *                                                                *
  *                                                                *
  *                                                                *
  *----------------------------------------------------------------*
   IDENTIFICATION DIVISION.
   PROGRAM-ID.  Pxxxxxx.

   ENVIRONMENT DIVISION.
   CONFIGURATION SECTION.

  /----------------------------------------------------------------*
   INPUT-OUTPUT SECTION.
   FILE-CONTROL.

  *----------------------------------------------------------------*



  /----------------------------------------------------------------*
   DATA DIVISION.
   FILE SECTION.
  *----------------------------------------------------------------*

  /----------------------------------------------------------------*
   WORKING-STORAGE SECTION.
  *----------------------------------------------------------------*



   01 proc-ptr usage procedure-pointer.


   01 sqlite3-db         pointer.

   01 err-msg            pointer.
   01 sqlite             pointer.
   01 res                pointer.



   01 rc                 pic 9 comp-5.
   01 dbName             pic x(08).






   01 sqlQuery       pic x(100).

   01 result         pic x(100).
   01 argv.
       03  firstColumn   pointer.
       03  secondColumn  pointer.

   01 azColName          pointer.
   01 argc               pic 99 comp-5.
   01 notused            pointer.

   01 Writefunction-Ptr  procedure-pointer.


   01 PERSON-POST.
       03 NAMN     PIC X(60).
       03 AGE      PIC 9(3).
       03 ADRESS   PIC X(100).
       03 TELNR    PIC X(20).
   01 Column-Id       pic X(3).
   01 Column-Name     pic X(20).



  *-----------------------------------------------------------------
   Linkage Section.





  /-----------------------------------------------------------------
   procedure division.
  *-----------------------------------------------------------------

  *
            set proc-ptr to entry "sqlite3.dll"

            display sqlQuery


            set sqlite3-db to null
            set err-msg    to null
            set res        to null

            move z"test.db" to dbName

            display "Running sqlite3_open"

            call "sqlite3_open" using
                    by reference  z"test.db"
                    by reference  sqlite3-db
                    returning     rc
            end-call


            if rc not = zero
               display "error opening database."

            else
               display "database opened."

            end-if


            move "INSERT INTO tabla VALUES ('020', '8855');"
               to sqlQuery

           initialize Column-Id
           initialize Column-Name





  *
  *
  *
            call "sqlite3_exec" using
               by value sqlite3-db
               by reference sqlQuery
               by value     0
               by value     0
               by reference err-msg
               returning rc
            end-call


          set Writefunction-Ptr to entry "sqlite-callback".

          initialize sqlQuery
          move "SELECT * FROM tabla;" to sqlQuery


          call "sqlite3_exec" using
             by value sqlite3-db
             by reference sqlQuery
             by value Writefunction-Ptr
             by value 0
             by reference err-msg
           returning rc
          end-call
  *
          entry "sqlite-callback" using
             by value notused
             by value argc
             by reference argv
             by reference azColName.

  *    how to get column1
  *    how to get column2


       display Column-Id "|" Column-Name

  *


          call "sqlite3_close" using
                  by reference sqlite3-db
          end-call

          display "sqlite3_close"

    .

  *----------------------------------------------------------------
    stop run.


   Entry-Termination.

现在,打开&插入工作正常,但我需要知道如何从Select语句中获取记录。我需要获得几行并显示它们。

1 个答案:

答案 0 :(得分:0)

最后,这是在netExpress中打开/插入/选择/关闭的代码:

  *
  *----------------------------------------------------------------*
  *                                                                *
  *                                                                *
  *                                                                *
  * SYSTEM                                                         *
  *                                                                *
  * PROGRAM-ID    Pxxxxxx
  *                                                                *
  * AUTHOR                                                         *
  *                                                                *
  * DATE          10/2016                                          *
  *                                                                *
  *                                                                *
  *                                                                *
  *                                                                *
  *----------------------------------------------------------------*
  *                                                                *
  *                                                                *
  *                                                                *
  *----------------------------------------------------------------*
   IDENTIFICATION DIVISION.
   PROGRAM-ID.  Pxxxxx.

   ENVIRONMENT DIVISION.
   CONFIGURATION SECTION.

  /----------------------------------------------------------------*
   INPUT-OUTPUT SECTION.
   FILE-CONTROL.

  *----------------------------------------------------------------*



  /----------------------------------------------------------------*
   DATA DIVISION.
   FILE SECTION.
  *----------------------------------------------------------------*

  /----------------------------------------------------------------*
   WORKING-STORAGE SECTION.
  *----------------------------------------------------------------*



   01 proc-ptr usage procedure-pointer.


   01 sqlite3-db         pointer.

   01 err-msg            pointer.
   01 sqlite             pointer.
   01 res                pointer.



   01 rc                 pic 9 comp-5.
   01 dbName             pic x(08).






   01 sqlQuery       pic x(100).

   01 result         pic x(100).
   01 argv.
       03  firstColumn   pointer.
       03  secondColumn  pointer.

   01 azColName          pointer.
   01 argc               pic 99 comp-5.
   01 notused            pointer.

   01 Writefunction-Ptr  procedure-pointer.







  *-----------------------------------------------------------------
   Local-storage Section.
   Linkage Section.
   01 Column-Id       pic X(3).
   01 Column-Name     pic X(20).





  /-----------------------------------------------------------------
   procedure division.
  *-----------------------------------------------------------------

  *
            set proc-ptr to entry "sqlite3.dll"

            display sqlQuery


            set sqlite3-db to null
            set err-msg    to null
            set res        to null

            move z"test.db" to dbName

            display "Running sqlite3_open"

            call "sqlite3_open" using
                    by reference  z"test.db"
                    by reference  sqlite3-db
                    returning     rc
            end-call


            if rc not = zero
               display "error opening database."

            else
               display "database opened."

            end-if


            move "INSERT INTO tabla VALUES ('002', '8855');"
               to sqlQuery
  *
  *
  *
            call "sqlite3_exec" using
               by value sqlite3-db
               by reference sqlQuery
               by value     0
               by value     0
               by reference err-msg
               returning rc
            end-call
  *
         set Writefunction-Ptr to entry "sqlite-callback".

          initialize sqlQuery
          move "SELECT * FROM tabla;" to sqlQuery


          call "sqlite3_exec" using
             by value sqlite3-db
             by reference sqlQuery
             by value Writefunction-Ptr
             by value 0
             by reference err-msg
           returning rc
          end-call

          call "sqlite3_close" using
                  by reference sqlite3-db
          end-call

          display "sqlite3_close"

            .
  * -------------------------------------------------------
   stop run.
         entry "sqlite-callback"
         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.
   Entry-Termination.