最近準備使用lazrus開發SQLite小應用,發現在linux aarch64下沒找到適合的libsqlite3.so加密版本,需然網上有wxsqlite等開源版本,但編譯不成功,最終發現開源的SQLite3 Multiple Ciphers能滿足使用,最主要是編譯so很簡單,適合新手根據不同平臺自行編譯加密的so文件。
SQLite3MultipleCiphers下載網址:
https://github.com/utelle/SQLite3MultipleCiphers/
下載解壓后,在終端執行以下2行命令就能生成libsqlite3.so,建議將libsqlite3.so拷貝到工程的文件夾。
gcc -O2 -s -shared -fPIC -c sqlite3mc.c
gcc -O2 -s -shared -fPIC -o libsqlite3.so sqlite3mc.o
發現linux x86_64下不能按以述方法編譯,經測試使用以下方法就可以編譯:
linux x86_64編譯so步驟: 1、安裝sudo apt install -y autoconf 如已安裝請跳過 2、autoreconf 3、配置:./configure --prefix =/home/sqlite3/ (注:=后面是sqlite的安裝路徑,可以自己新建一個文件夾存放) 4、編譯,安裝 命令:make clean;make;make install (注:make clean是為了清除以前的編譯文件,make是在編譯,make install是安裝) 安裝完成后,在安裝目錄下可以看到生成了lib,include
使用Zeos控件時要注意指定libsqlite3.so和位置(必須使用絕對路徑),可以參考以下代碼:
ZConnection1.LibraryLocation:=ExtractFilePath(Application.ExeName)+'libsqlite3.so';
如需加密SQLite庫,只需在生成時ZConnection1.Password加上密碼就可以。
ZConnection1.DisConnect; ZConnection1.Protocol:='sqlite-3'; ZConnection1.LibraryLocation:=ExtractFilePath(Application.ExeName)+'libsqlite3.so'; ZConnection1.Properties.Add('encrypted=yes'); ZConnection1.Database:='demo.db3'; ZConnection1.Password:='123asd'; ZConnection1.Connect; ZQuery1.SQL.Text := 'CREATE TABLE hardware (id INTEGER PRIMARY KEY, compname VARCHAR(30), username VARCHAR(30), model VARCHAR(30))'; ZQuery1.ExecSQL; ZQuery1.SQL.Text := 'CREATE INDEX sHardware ON hardware(compname)'; ZQuery1.ExecSQL; ZQuery1.SQL.Text := 'INSERT INTO hardware(id, compname, username, model) VALUES (1, "AMD8537", "OMonge", "Gigabyte");'; ZQuery1.ExecSQL; ZConnection1.Disconnect;
打開SQLite數據庫,如果數據庫沒設置密碼,則為數據庫設置密碼:
procedure RekeyDB(conn: TZConnection; pwd: string); var db: Pointer; i: integer; begin if not conn.Connected Then conn.Connect; db := (conn.DbcConnection as IZSQLiteConnection).GetConnectionHandle; i := (conn.DbcConnection as IZSQLiteConnection).GetPlainDriver.ReKey (db, PChar(pwd), Length(pwd)); If (i <> 0) then // 函數正常執行返回0,否則 begin // xxxxxxx end; end; function IsSQLite3File(const FileName: TFileName): boolean; var F: THandle; Header:array [0..15] of char ; begin F := FileOpen(FileName,fmOpenRead or fmShareDenyNone); if F= THandle(-1) then result := false else
begin FileRead(F,Header,15); if Header='SQLite format 3' then result:=true
else result:=false;
FileClose(F);
end;
end;
procedure TForm1.Button2Click(Sender: TObject); begin ZConnection1.Disconnect; ZConnection1.Protocol:='sqlite-3'; ZConnection1.LibraryLocation:=ExtractFilePath(Application.ExeName)+'libsqlite3.so'; ZConnection1.Properties.Clear; ZConnection1.Properties.Add('encrypted=yes'); ZConnection1.Properties.Add('controls_cp=CP_UTF8'); ZConnection1.Properties.Add('AutoEncodeStrings=True'); ZConnection1.Database:='demo.db3'; if IsSQLite3File('demo.db3') then RekeyDB(ZConnection1,'123asd') //未加密,則設置數據庫密碼 else begin ZConnection1.Password:='123asd';//已加密 ZConnection1.connect; end; end;
執行上面的代碼后就能生成加密的demo.db3。
設置或取消數據庫密碼:
uses ZDbcSqLite---要加上這個單元
procedure RekeyDB(conn: TZConnection; pwd: string); var db: Pointer; i: integer; begin db := (conn.DbcConnection as IZSQLiteConnection).GetConnectionHandle; i := (conn.DbcConnection as IZSQLiteConnection).GetPlainDriver.ReKey (db, PChar(pwd), Length(pwd)); If (i <> 0) then // 函數正常執行返回0,否則 begin // xxxxxxx end; end;
使用方法:
procedure TForm1.Button2Click(Sender: TObject); begin ZConnection1.Disconnect; ZConnection1.Protocol:='sqlite-3'; ZConnection1.LibraryLocation:=ExtractFilePath(Application.ExeName)+'libsqlite3.so'; ZConnection1.Properties.Clear; ZConnection1.Properties.Add('encrypted=yes'); ZConnection1.Properties.Add('controls_cp=CP_UTF8'); ZConnection1.Properties.Add('AutoEncodeStrings=True'); ZConnection1.Database:='demo.db3'; ZConnection1.Password:='123asd'; ZConnection1.Connect; RekeyDB(ZConnection1,'');//取消密碼 end;
linux aarch64編譯SQLite3命令行:
gcc shell.c sqlite3mc.c -lpthread -ldl -o sqlite3
linux x86_64 SQLite3命令行的編譯:
autoreconf mkdir build-gtk [or any other suitable name] cd build-gtk ../configure make
使用SQLite3命令行數據密碼設置和取消的方法:
sqlite3 demo.db3 # 創建一個新的數據庫 sqlite> PRAGMA key='123asd'; # 設置加密數據庫的密碼 ok # 顯示 ok 說明設置密碼成功 sqlite> create table help (id int, name text); # 創建一些數據 sqlite> .q # 退出數據庫 sqlite3 demo.db3 # 再次進入數據庫,相當于進入了一個已經加密的數據庫 sqlite> .tab # 在不輸入密碼的情況下查看當前的表 Error: file is not a database # 不輸入密碼的情況下,解析數據庫失敗 sqlite> PRAGMA key = '123asd'; # 使用密碼進行認證 ok # 輸出 ok,說明認證成功 sqlite> .tab # 查看數據庫中的表 help # 查看表成功,目前數據庫中只有表 help
PRAGMA key = '123asd'--設置密碼 PRAGMA rekey = ''--取消密碼

浙公網安備 33010602011771號