Skip to content

Error: SQL logic error or missing database #47

Closed

Description

@Type1J

Screenshot
On c19c72f I cannot perform any operation on encrypted databases. As soon as PRAGMA key = 'some_key'; is executed, then next SQL operation fails with "Error: SQL logic error or missing database". This error also occurs after performing an ATTACH with a KEY specified.

I am on OSX 10.8.2. I used Xcode 4.6 (with the repo provided project) to generate an amalgamation. I then used the CMakeLists.txt file below to compile shell.c and sqlite3.c, and linked it with the OS provide libcrypto from OpenSSL.

Here's the "CMakeLists.txt" file that I used:

cmake_minimum_required(VERSION 2.8.8)
project(SQLite3)

set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/CMake ${CMAKE_MODULE_PATH})

find_package(OpenSSL)
if(NOT OPENSSL_FOUND)
message(FATAL_ERROR "OpenSSL is required for SQLCipher")
endif()

set(SQLITE3_FOUND TRUE CACHE INTERNAL "SQLite3 Found")
set(SQLITE3_LIBRARIES SQLite3 ${OPENSSL_LIBRARIES} CACHE INTERNAL "SQLite3 Libraries")
set(SQLITE3_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/include CACHE INTERNAL "SQLite3 Include Directories")
set(SQLITE3_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/include CACHE INTERNAL "SQLite3 Include Directories")

add_definitions(
-D_LARGE_FILE=1
-D_FILE_OFFSET_BITS=64
-D_LARGEFILE_SOURCE=1
-DSQLITE_ENABLE_RTREE=1
-DSQLITE_HAS_CODEC
-DSQLITE_TEMP_STORE=2
-DSQLITE_THREADSAFE
)

include_directories(${SQLITE3_INCLUDE_DIRS} ${OPENSSL_INCLUDE_DIRS})

add_library(SQLite3 STATIC
${CMAKE_CURRENT_LIST_DIR}/sqlite3.c
${CMAKE_CURRENT_LIST_DIR}/include/sqlite3.h
${CMAKE_CURRENT_LIST_DIR}/include/sqlite3ext.h
)

option(BUILD_SQLITE3SHELL "Build the SQLite3 shell (sqlite3 executable)" ON)

if(BUILD_SQLITE3SHELL)
add_executable(SQLite3_Shell ${CMAKE_CURRENT_LIST_DIR}/shell.c)
target_link_libraries(SQLite3_Shell ${SQLITE3_LIBRARIES})
set_target_properties(SQLite3_Shell PROPERTIES OUTPUT_NAME sqlite3)
endif()

Activity

  1. sjlombardo commented on Mar 13, 2013

    @sjlombardo
    Member

    I've built SQLCipher using CMake exactly as you've described. I've verified both the standard operations on a single encrypted database and the use of an attached database with sqlcipher_export(). Everything works without issues.

    That said, we've seen similar errors to what you described in the past due to mismatched OpenSSL headers. For instance, if you include OpenSSL headers for one version but then another version is dynamically loaded at runtime you can see this behavior. Can you please make sure that you don't have any OpenSSL headers in the include path other than the ones in /usr/include/openssl?

  2. Type1J commented on Mar 13, 2013

    @Type1J
    Author

    Thanks! That was the problem (MacPorts headers with system .dylib).

    One more question. I'm going to have some people using the database files through the shell, and this came up:
    Is this the correct behavior?
    $ ./sqlite3 encrypted.db
    SQLite version 3.7.13 2012-06-11 02:05:22
    Enter ".help" for instructions
    Enter SQL statements terminated with a ";"
    sqlite> select * from blah;
    Error: file is encrypted or is not a database
    sqlite> PRAGMA key = 'testkey';
    sqlite> select * from blah;
    Error: file is encrypted or is not a database

    It works correctly if "PRAGMA key ..." is first, but if not, then it doesn't attempt to try to use the database again after a correct "PRAGMA key ..." is given. Is it a requirement that the database be detached and reattached if the user forgot to give the key at first, or is this a bug?

    By the way, thanks for making SQLCipher!

  3. sjlombardo commented on Mar 14, 2013

    @sjlombardo
    Member

    Yes, it is a requirement to provide the key (and any other sqlcipher pragmas) as the first operation. Automatically re-initializing the database after it is used, e.g. supplying a key after an operation has already failed, is fairly complex, and not possible at this time. We may try to improve this at some point in the future, but for now please provide the key first. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions