Hello community,

one year ago I presented here the possibility how to export SAP table content to SQLite containers. One way is via a COM library which is a wrapper around a few functions of the SQLite library. This library has only one purpose, to export SAP table content easily. Recently reached me a few emails with the suggestion to extend the COM library, it should be possible to execute queries and handle the result. I thought about it and came to the conclusion that this is not necessary because you can use the full bandwith with ActiveX Data Objects (ADO) and Open Database Connectivity (ODBC).

To use SQLite via ODBC you must install a special driver. You can find a very good one here, it is free and released under BSD-type license. Now you can use SQLite databases with ABAP.

Here an example how to create and use an SQLite database:

“-Begin—————————————————————–

Report ZODBC_SQLITE.

  “-Constants———————————————————–

    Constants adUseClient Type i Value 3.

  “-Variables———————————————————–

    Data oCon Type OLE2_OBJECT.

    Data oRecSet Type OLE2_OBJECT.

    Data oFields Type OLE2_OBJECT.

    Data oField Type OLE2_OBJECT.

    Data cntRec Type i.

    Data cntFields Type i.

    Data sSQL Type String.

    Data i Type i.

    Data j Type i.

    Data nameField Type String.

    Data valueField Type String.

  “-Main—————————————————————-

    Create Object oCon ‘ADODB.Connection’.

    If sy-subrc <> 0 Or  oCon-Handle <= 0 Or oCon-Type <> ‘OLE2’..

      Exit.

    EndIf.

    Set Property Of oCon ‘CursorLocation’ = adUseClient.

    Call Method Of oCon ‘Open’ Exporting

      #1 = ‘DRIVER=SQLite3 ODBC Driver;Database=C:\Dummy\MyDb.db3;’.

    sSQL = ‘DROP TABLE tblTest’.

    Call Method Of oCon ‘Execute’ Exporting #1 = sSQL.

    sSQL = ‘CREATE TABLE tblTest(ID INTEGER PRIMARY KEY, NAME VARCHAR(40))’.

    Call Method Of oCon ‘Execute’ Exporting #1 = sSQL.

    i = 1.

    While i <= 16.

      sSQL = ‘INSERT INTO tblTest VALUES(‘ && i && ‘, ”Name’ && i && ”’)’.

      Call Method Of oCon ‘Execute’ Exporting #1 = sSQL.

      i = i + 1.

    EndWhile.

    sSQL = ‘SELECT * FROM tblTest’.

    Call Method Of oCon ‘Execute’ = oRecSet Exporting #1 = sSQL.

    Get Property Of oRecSet ‘RecordCount’ = cntRec.

    i = 1.

    While i <= cntRec.

      Get Property Of oRecSet ‘Fields’ = oFields.

      Get Property Of oFields ‘Count’ = cntFields.

      j = 0.

      While j <= cntFields – 1.

        Get Property Of oFields ‘Item’ = oField Exporting #1 = j.

        Get Property Of oField ‘Name’ = nameField.

        Get Property Of oField ‘Value’ = valueField.

        Write: / nameField, ` `, valueField.

        j = j + 1.

      EndWhile.

      Call Method Of oRecSet ‘MoveNext’.

      i = i + 1.

    EndWhile.

    Call Method Of oCon ‘Close’.

    Free Object oCon.

“-End——————————————————————-

It is not necessary to build another library, with the existing possibilities like ADO and ODBC you can connect and use many different databases of different manufacturers with ABAP.

Enjoy it.

Cheers

Stefan

To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

Leave a Reply