DBD::SQLcipher prepare 和 PRAGMA 都不工作

DBD::SQLcipher prepare nor PRAGMA is working

我无法使用 DBD::SQLcipher 从 SQLcipher 数据库 select。

我尝试了各种方法,它仍然认为我提供的文件不是 sqlcipher db(我确信它是)

第一个函数尝试连接常规 SQLite3 数据库,并在失败时连接到 SQLcipher(这确实会发生)。

这是我的代码:

sub connectSQLite() {
    my $DBH = undef;
    eval {
        local $SIG{__WARN__} = sub {};
        my $dsn = "DBI:SQLite:dbname=dbfile";
        my $userid = "";
        my $password = "";
        $DBH = DBI->connect($dsn, $userid, $password, { RaiseError => 1 });
        $DBH->do('select * from properties;');
    };

    if ($@) {
        eval {
            my $dsn = "DBI:SQLcipher:dbname=dbfile";
            my $userid = "";
            my $password = "";
            my $dbKey = "123";
            $DBH = DBI->connect($dsn, $userid, $dbKey, { RaiseError => 1});
        };
    }

    return $DBH;
}

sub getCurrentSensorsList() {
    my $dbHandler = connectSQLite() || return 0;

    $dbHandler->do("PRAGMA key=\"123\";");
    my $sqlStatment = "SELECT id FROM sensors;";
    my $statementHandler = $dbHandler->prepare($sqlStatment); #### this is line 46

    print Dumper $dbHandler;
    print Dumper $statementHandler;

    return $statementHandler->fetchall_arrayref();
}

my $sensorsData = getCurrentSensorsList();
print Dumper $sensorsData;

这是输出(我用第 46 行标记了代码):

DBD::SQLcipher::db prepare failed: file is encrypted or is not a database at /talm/Repos/devops/dependencies/agent/linux//Linux/UpgradeHandler.pm line 46.
DBD::SQLcipher::db prepare failed: file is encrypted or is not a database at /talm/Repos/devops/dependencies/agent/linux//Linux/UpgradeHandler.pm line 46.

也尝试过:

my $sqlStatment = "PRAGMA key=\"123\"; SELECT " . join(", ", @$columns) . " FROM sensors;";

它的输出是:

$VAR1 = bless( {}, 'DBI::db' );
$VAR1 = bless( {}, 'DBI::st' );
$VAR1 = [];

这意味着它可以工作 - 但准备可能无法从 select 语句中检索数据,因为那里有 2 个命令。

找到答案了!

步骤:

  1. 编译openssl(我用的是1.1.1)-保持libcrypto.so
  2. 使用您刚刚创建的 libcrypto 编译 sqlcipher(我使用 4.3.0)。
  3. 下载 DBD-SQLite(目前最新版本为 1.66)并解压
  4. 复制 sqlite3.c sqlite3.h sqlite3ext.h 从 sqlcipher 文件夹到 DBD-SQLite
  5. 复制libcrypto.so到DBD-SQLite
  6. 编辑 LDFLAGS、LDDLFLAGS、CFLAGS 在所有定义后添加:
push(@CCFLAGS, "-I/usr/include/"); ## or the include folder of openssl
push(@CCFLAGS, "-L.");
push(@CCFLAGS, "-ldl");
push(@CCFLAGS, "-lpthread");
push(@CCFLAGS, "-lcrypto");

push(@LDFLAGS, "-L.");
push(@LDFLAGS, "-ldl");
push(@LDFLAGS, "-lpthread");
push(@LDFLAGS, "-lcrypto");

push(@LDDLFLAGS, "-L.");
push(@LDDLFLAGS, "-ldl");
push(@LDDLFLAGS, "-lpthread");
push(@LDDLFLAGS, "-lcrypto");

然后:

perl Makeperl.PL
make
sudo make install

而且我的代码有效!