Yes, you can use such key for an attached database, but with some conditions:
1) Such a key should be stored in a TBytes array
2) The key must be mapped to Hex, being previously converted from ANSI to UTF8
3) The key must be passed to the ATTACH command as a binary string
4) Before executing ATTACH, an encryption algorithm must be set for the attached database using the command: PRAGMA encryption=ХХ
A sample below demonstrates the behavior you need
Code: Select all
program Project4;
{$APPTYPE CONSOLE}
{$R *.res}
uses
SysUtils,
Uni,
SQLiteUniProvider,
LiteError;
const
KeyBytes: TBytes = [10, 101, 12, 103, 145, 150, 110, 109,
188, 129, 67, 152, 100, 141, 187, 135,
01, 11, 12, 31, 10, 17, 62, 78,
11, 9, 02, 240, 230, 170, 206, 172,
188, 127, 207, 113, 44, 77, 11, 15,
182, 193, 102, 109, 170, 127, 188, 195];
var
UniConnection: TUniConnection;
i, ii: integer;
str_key: AnsiString;
hex_key: string;
NewBytes: TBytes;
begin
try
str_key := TEncoding.ANSI.GetString(KeyBytes);
hex_key := '';
NewBytes := TEncoding.Convert(TEncoding.ANSI, TEncoding.UTF8, KeyBytes);
for i := 0 to High(NewBytes) do
hex_key := hex_key + IntToHex(NewBytes[i], 2);
if FileExists('d:\main.db3') then
DeleteFile('d:\main.db3');
if FileExists('d:\attached.db3') then
DeleteFile('d:\attached.db3');
UniConnection := TUniConnection.Create(nil);
try
UniConnection.ProviderName := 'SQLite';
UniConnection.Database := 'd:\main.db3';
UniConnection.SpecificOptions.Values['Direct'] := 'True';
UniConnection.SpecificOptions.Values['ForceCreateDatabase'] := 'true';
UniConnection.Connect;
UniConnection.ExecSQL('create table table1(id integer primary key, txt varchar(100))');
UniConnection.ExecSQL('insert into table1(id, txt) values(1, ''test'')');
UniConnection.Disconnect;
UniConnection.Database := 'd:\attached.db3';
UniConnection.SpecificOptions.Values['EncryptionKey'] := str_key;
UniConnection.SpecificOptions.Values['EncryptionAlgorithm'] := 'leAES128';
UniConnection.Connect;
UniConnection.ExecSQL('create table table2(id integer primary key, txt varchar(100))');
UniConnection.Disconnect;
UniConnection.Database := 'd:\main.db3';
UniConnection.SpecificOptions.Values['EncryptionKey'] := '';
UniConnection.SpecificOptions.Values['EncryptionAlgorithm'] := 'leDefault';
UniConnection.Connect;
try
UniConnection.ExecSQL('PRAGMA encryption=AES128');
UniConnection.ExecSQL('ATTACH DATABASE ''d:\attached.db3'' AS two KEY X''' + hex_key + '''');
except
on E: ESQLiteError do
Writeln(e.Message + ' ' + E.ErrorCode.ToString);
end;
UniConnection.ExecSQL('insert into two.table2 select * from main.table1');
UniConnection.ExecSQL('select * from two.table2');
finally
UniConnection.Free;
end;
except
on E: Exception do
writeln(e.Message);
end;
readln;
end.