Sqlite cipher

Sqlite cipher

Postby carlos vargas » Fri Mar 08, 2019 9:58 pm

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*/
/*-------------------------------------------------------------------------------------------*/

 
Salu2
Carlos Vargas
Desde Managua, Nicaragua (CA)
User avatar
carlos vargas
 
Posts: 1683
Joined: Tue Oct 11, 2005 5:01 pm
Location: Nicaragua

Re: Sqlite cipher

Postby Lailton » Sat Mar 09, 2019 1:55 am

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!
Regards,
Lailton Fernando Mariano
User avatar
Lailton
 
Posts: 125
Joined: Fri Jul 20, 2012 1:49 am
Location: Brazil

Re: Sqlite cipher

Postby cnavarro » Sat Mar 09, 2019 2:59 am

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
.../...
Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
User avatar
cnavarro
 
Posts: 6500
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

Re: Sqlite cipher

Postby MarioG » Fri Aug 07, 2020 1:09 am

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
Resistencia - "Ciudad de las Esculturas"
Chaco - Argentina
User avatar
MarioG
 
Posts: 1380
Joined: Fri Oct 14, 2005 1:28 pm
Location: Resistencia - Chaco - AR

Re: Sqlite cipher

Postby gabo » Sat Aug 08, 2020 12:17 am

Crea la libreria dlel Clone de la version harbour 3.4 de Viktor Szakats, Te puede funcionar
gabo
 
Posts: 126
Joined: Tue Jan 03, 2006 8:31 pm

Re: Sqlite cipher

Postby MarioG » Sat Aug 08, 2020 12:40 pm

gracias Gabo!
Resistencia - "Ciudad de las Esculturas"
Chaco - Argentina
User avatar
MarioG
 
Posts: 1380
Joined: Fri Oct 14, 2005 1:28 pm
Location: Resistencia - Chaco - AR


Return to FiveWin para Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 91 guests