Page 1 of 1

Sqlite cipher

PostPosted: Fri Mar 08, 2019 9:58 pm
by carlos vargas
Alguien ha trabajado con esto?
necesito leer una base de datos sqlite encriptada, en el mobil uso sqlite cipher
pero en la desktop, solo tengo esto para leer sqlite sin encriptacion.
Code: Select all  Expand view

/*-------------------------------------------------------------------------------------------------*/

#include "dmolina.ch"
#include "hbsqlit3.ch"

/*-------------------------------------------------------------------------------------------*/

FUNCTION SqlLite_Open( cDB, lCreateIfNotExist )
   LOCAL pDB

   IF ! Empty( cDB )
      pDB := sqlite3_open( cDB, lCreateIfNotExist )
   ENDIF

RETURN pDB

/*-------------------------------------------------------------------------------------------*/

FUNCTION SqlLite_Execute( pDB, cQry )
   LOCAL cError

   IF !Empty( pDB ) .and. !Empty( cQry )
      IF sqlite3_exec( pDB, cQry ) == SQLITE_OK
         RETURN TRUE
      ELSE
         cError := sqlite3_errmsg( pDB )
         IF !Empty( cError )
            MsgAlert( cError, "Error SqlLite" )
         ENDIF
      ENDIF
   ENDIF

RETURN FALSE

/*-------------------------------------------------------------------------------------------*/

FUNCTION SqlLite_Query( pDB, cQuery )
   LOCAL pStmt, nFields, n
   LOCAL aRet, aRec
   LOCAL cError
   LOCAL nCType

   IF !Empty( pDB )
      pStmt := sqlite3_prepare( pDB, cQuery )
      IF !Empty( pStmt )
         aRet := {}
         DO WHILE sqlite3_step( pStmt ) == SQLITE_ROW
            aRec := {}
            nFields := sqlite3_column_count( pStmt )
            IF nFields>0
               FOR n := 1 TO nFields
                  nCType := sqlite3_column_type( pStmt, n )
                  DO CASE
                  CASE nCType == SQLITE_NULL     //5
                     AAdd( aRec, "NULL" )
                  CASE nCType == SQLITE_FLOAT    //2
                     AAdd( aRec, sqlite3_column_double( pStmt, n ) )
                  CASE nCType == SQLITE_INTEGER  //1
                     AAdd( aRec, sqlite3_column_int( pStmt, n ) )
                  CASE nCType == SQLITE_TEXT     //3
                     AAdd( aRec, sqlite3_column_text( pStmt, n ) )
                  CASE nCType == SQLITE_BLOB     //4
                     AAdd( aRec, sqlite3_column_blob( pStmt, n ) )
                  ENDCASE
               NEXT
            ENDIF
            aadd( aRet, aRec )
         ENDDO
         sqlite3_reset( pStmt )
      ELSE
         cError := sqlite3_errmsg( pDB )
         IF !Empty( cError )
            MsgAlert( cError, "Error SqlLite" )
         ENDIF
      ENDIF
   ENDIF

RETURN aRet

/*-------------------------------------------------------------------------------------------*/

FUNCTION SqlLite_QueryValue( pDB, cQuery )
   LOCAL aRet

   IF !Empty( pDB ) .and. !Empty( cQuery )
      aRet := SqlLite_Query( pDB, cQuery )
      IF HB_IsArray( aRet ) .and. Len( aRet ) == 1
         RETURN aRet[ 1, 1 ]
      ENDIF
   ENDIF

RETURN NIL

/*-------------------------------------------------------------------------------------------*/

FUNCTION SqlLite_ListTables( pDB )
   LOCAL aRet := {}
   LOCAL cQry

   IF !Empty( pDB )
      cQry := "SELECT name FROM sqlite_master "      +;
              "WHERE type IN ('table','view') "      +;
              "AND name NOT LIKE 'sqlite_%' "        +;
              "UNION ALL "                           +;
              "SELECT name FROM sqlite_temp_master " +;
              "WHERE type IN ('table','view') "      +;
              "ORDER BY 1;"

      aRet := SqlLite_Query( pDB, cQry )
   ENDIF

RETURN aRet

/*-------------------------------------------------------------------------------------------*/

FUNCTION SqlLite_TableExist( pDB, cTable )
   LOCAL aRet := {}, cQry

   cQry := "SELECT name FROM sqlite_master WHERE type='table' AND tbl_name='" + cTable + "'"

   IF !Empty( pDB ) .and. !Empty( cTable )
      aRet := SqlLite_Query( pDB, cQry )
   ENDIF

RETURN ( Len( aRet ) > 0 )

/*-------------------------------------------------------------------------------------------*/

FUNCTION SqlLite_Pack( pDB )

   IF !Empty( pDB )
      IF sqlite3_exec( pDB, "VACUUM" ) == SQLITE_OK
         RETURN TRUE
      ENDIF
   ENDIF

RETURN FALSE

/*-------------------------------------------------------------------------------------------*/

FUNCTION SqlLite_Fields( pDB, cTable )
   LOCAL aRet := {}, nFields, n
   LOCAL cError
   LOCAL pStmt

   IF !Empty( pDB ) .and. !Empty( cQry )
      pStmt := sqlite3_prepare( pDB, "SELECT * FROM " + cTable )
      IF !Empty( pStmt )
         sqlite3_step( pStmt )
         nFields := sqlite3_column_count( pStmt )
         IF nFields > 0
            FOR n:=1 TO nFields
               AAdd( aRet, sqlite3_column_name( pStmt, n ) )
            NEXT
         ENDIF
         sqlite3_reset( pStmt )
      ELSE
         cError := sqlite3_errmsg( pDB )
         IF !Empty( cError )
            MsgAlert( cError, "Error SqlLite" )
         ENDIF
      ENDIF
   ENDIF

RETURN aRet

/*-------------------------------------------------------------------------------------------*/

FUNCTION SqlLite_Columns( pDB, cTable )
   LOCAL nCType, aCType :=  { "SQLITE_INTEGER", "SQLITE_FLOAT", "SQLITE_TEXT", "SQLITE_BLOB", "SQLITE_NULL" }
   LOCAL aRet := {}, nFields, n
   LOCAL cError
   LOCAL pStmt

   IF !empty( pDB ) .and. !empty( cQry )
      pStmt := sqlite3_prepare( pDB, "SELECT * FROM " + cTable )
      IF !empty( pStmt )
         sqlite3_step( pStmt )
         nFields := sqlite3_column_count( pStmt )
         IF nFields > 0
            FOR n := 1 TO nFields
               nCType := sqlite3_column_type( pStmt, n )
               aadd( aRet, { sqlite3_column_name( pStmt, n ), aCType[ nCType ] } )
            NEXT
         ENDIF
         sqlite3_reset( pStmt )
      ELSE
         cError := sqlite3_errmsg( pDB )
         IF !Empty( cError )
            MsgAlert( cError, "Error SqlLite" )
         ENDIF
      ENDIF
   ENDIF

RETURN aRet

/*-------------------------------------------------------------------------------------------*/
/*EOF*/
/*-------------------------------------------------------------------------------------------*/

 

Re: Sqlite cipher

PostPosted: Sat Mar 09, 2019 1:55 am
by Lailton
Hola Carlos,

Yo estoy usando SQLite con "SQLCIPHER" pero uso con library HDO ( Harbour Data Objects ) de Manuel Exposito <xmessoft@gmail.com>,
con HDO trabajo con SQLITE, MySQL y MariaDB con lo miesmo codigo!

Funciona muy bien!

Re: Sqlite cipher

PostPosted: Sat Mar 09, 2019 2:59 am
by cnavarro
Carlos, para empezar, necesitas la libreria sqlcipher.lib, que has de construirla a partir de su DLL sqlcipher.DLL para el compilador que vayas a utilizar
A partir de ahí, ( te lo resumo )
local dbName := ...
local lCreate := .F.
local cKey := "mykey"

oDB := sqlite3_open( dbname, lCreate )
sqlite3_exec( oDB, 'pragma key = ' + cKey )

if sqlite3_errcode(dbo1) > 0 // error
.../...

Re: Sqlite cipher

PostPosted: Fri Aug 07, 2020 1:09 am
by MarioG
cnavarro wrote:Carlos, para empezar, necesitas la libreria sqlcipher.lib, que has de construirla a partir de su DLL sqlcipher.DLL para el compilador que vayas a utilizar
A partir de ahí, ( te lo resumo )
local dbName := ...
local lCreate := .F.
local cKey := "mykey"

oDB := sqlite3_open( dbname, lCreate )
sqlite3_exec( oDB, 'pragma key = ' + cKey )

if sqlite3_errcode(dbo1) > 0 // error
.../...


Buenas noches.
No puedo importar la .dll a .lib
De donde la baje dice que es versión 3.15.2.0
Al hacer implib me devuelve lo siguiente
D:\Bat>implib

Embarcadero Implib Version 3.3.0 Copyright (c) 1991-2014 Embarcadero Technologies, Inc.

Syntax: IMPLIB [options] libname[.lib] [@respfile | srcname] [srcname ...]
Options:
-a Add '_' alias for MS flavor cdecl functions
-aa Force the alias even if the function already starts with '_'
-c Case sensitive symbols
-f Force imports by name (with hints)
-s Don't convert stdcall names from Microsoft mangling
-w No Warnings

Respfile may contain a list of source files to process.
Wildcards are ok for .DLL and .DEF file names.

D:\Bat>implib sqlcipher.dll sqlcipher.lib

Embarcadero Implib Version 3.3.0 Copyright (c) 1991-2014 Embarcadero Technologies, Inc.
Error : unable to open file

Re: Sqlite cipher

PostPosted: Sat Aug 08, 2020 12:17 am
by gabo
Crea la libreria dlel Clone de la version harbour 3.4 de Viktor Szakats, Te puede funcionar

Re: Sqlite cipher

PostPosted: Sat Aug 08, 2020 12:40 pm
by MarioG
gracias Gabo!