Cipher: failed to reserve an envelope space - sqlite

I am attempting to encrypt/decrypt a SQLite database via FireDAC in a Delphi XE7 application running on Windows 7 (64 bit).
The code looks like this:
Procedure TMain.ActionBtnClick(Sender: TObject);
Begin
If ActionBtn.Caption = 'Encrypt' Then
Begin
SetPassword;
FDSQLiteSecurity.SetPassword;
End
Else
FDSQLiteSecurity.RemovePassword;
SetStatus;
End;
Procedure TMain.DBNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
Begin
If OpenDialog.Execute Then
Begin
DBName.Text := OpenDialog.FileName;
SetStatus;
End;
End;
Procedure TMain.FormClose(Sender: TObject; Var Action: TCloseAction);
Var
Reg: TRegistry;
Begin
Reg := TRegistry.Create;
Try
Reg.OpenKey('\SQLiteSecurity', True);
Reg.WriteString('Database', DBName.Text);
Finally
Reg.CloseKey;
Reg.Free;
End;
End;
Procedure TMain.FormShow(Sender: TObject);
Var
Reg: TRegistry;
Begin
DBStatus.Caption := '';
Reg := TRegistry.Create;
Try
Reg.OpenKey('\SQLiteSecurity', True);
If Reg.ValueExists('Database') Then
Begin
DBName.Text := Reg.ReadString('Database');
SetStatus;
End;
Finally
Reg.CloseKey;
Reg.Free;
End;
End;
Procedure TMain.SetPassword;
Var
s: String;
Begin
FDSQLiteSecurity.Database := DBName.Text;
BEK(s);
FDSQLiteSecurity.Password := s;
End;
Procedure TMain.SetStatus;
Begin
DBStatus.Caption := FDSQLiteSecurity.CheckEncryption;
If DBStatus.Caption = '<unencrypted>' Then
ActionBtn.Caption := 'Encrypt'
Else
ActionBtn.Caption := 'Decrypt';
End;
When trying to encrypt, at the line that reads "FDSQLiteSecurity.SetPassword;", I get the following error message:
[FireDAC][Phys][SQLite] ERROR: Cipher: failed to reserve an envelope space.
I have tried to find the meaning of this error message without success. Does anyone know what the error message from SQLite is trying to tell me?

TFDSQLiteSecurityOptions FireDAC.Phys.SQLite.TFDSQLiteSecurity.Options
Have you set option soSetLargeCache ?
Use the Options property to specify the database encryption options.
Due to current SQLite encryption limitation the SetPassword / ChangePassword / RemovePassword calls will fail, if the database has blob fields with a value size greater than 1 DB page, and the database does not fit into the SQLite cache.
If soSetLargeCache is set, then SetPassword / ChangePassword / RemovePassword automatically set the cache size greater than the DB size, to fit the database into the memory in full.
If the DB size is greater than the accessible system memory, then the corresponding call fails.

Related

The difference between the user's old and new password isn't checked in Oracle APEX

I’m working with an Oracle APEX application and today I have discovered that the password verify function, which is set up for several profiles, doesn’t work properly, because the parameter, which stands for the old password, turned out to be null.
My password verify function F_VERIFY_PASSWORD is based on the example from the Oracle admin file utlpwdmg.sql: the code changes were made only for the parts which are not related to the comparison of the old password and the new one. The function code is presented below (excluding the parts which make no problems):
create or replace function F_VERIFY_PASSWORD
(
USERNAME varchar2
,PASSWORD varchar2
,OLD_PASSWORD varchar2
) return boolean is
…
DIFFER integer;
MINDIFFER number := 3;
…
begin
…
-- Check that the new password differs from the previous one no less than in MINDIFFER positions.
if OLD_PASSWORD is not null
then
DIFFER := LENGTH(OLD_PASSWORD) - LENGTH(PASSWORD);
DIFFER := ABS(DIFFER);
if DIFFER < MINDIFFER
then
if LENGTH(PASSWORD) < LENGTH(OLD_PASSWORD)
then
M := LENGTH(PASSWORD);
else
M := LENGTH(OLD_PASSWORD);
end if;
for I in 1 .. M
loop
if SUBSTR(PASSWORD,I,1) != SUBSTR(OLD_PASSWORD,I,1)
then
DIFFER := DIFFER + 1;
end if;
end loop;
if DIFFER < MINDIFFER
then
RAISE_APPLICATION_ERROR(-20011,'The new password must differ from the old one no less than in 3 positions!');
end if;
end if;
end if;
-- Return TRUE, if all is correct.
return(true);
end;
The changing password itself is made by the means of the procedure P_CHANGE_PASS, which is executed in Oracle APEX application. The code of it is presented below (excluding the parts which make no problems).
create or replace procedure P_CHANGE_PASS
(
NEWPASS varchar2
,NEWPASS2 varchar2
,USR varchar2
) as
…
begin
…
--changing the password
begin
execute immediate 'alter user ' || USR || ' identified by ' || NEWPASS;
exception
when others then
…
end;
end;
I’ve tried the following steps to make the verify function include the check of differences between the old and new passwords:
revoked the privilege ALTER USER from the user APEX_PUBLIC_USER;
added the statement AUTHID CURRENT_USER to the code of the
“client” procedure P_CHANGE_PASS;
added REPLACE <old password>
to the ALTER USER command (it’s awful thing, but I made it just
for debugging).
None of these actions helped me to solve my problem. So, what should I do to make the parameter OLD_PASSWORD not null in the function F_VERIFY_FUNCTION?
I use Oracle 11g (version 11.2.0.3.0), Oracle APEX 4.2.6.00.03.

Table locks in SQLite accessed by fireDAC

I'm working on porting a set of paradox tables to SQLite. In order to do so, I created a test application that simulates (somewhat) the current usage scenario: multiple users accessing the same DB file and performing simultaneous read and writes.
The application is very simple: it will start several threads that each create a connection, opens a table and will randomly read, update or insert inside the table.
Almost immediately, The application encounters a "database table locked" error. I have tried several things to attempt to work around it but nothing seems to work. What am I doing wrong ?
Here is the code internal to the threads:
procedure testDB(TargetFolder: string);
var
Conn: TFDConnection;
Table: TFDTable;
i: Integer;
begin
randomize;
Conn := TFDConnection.Create(nil);
try
Conn.DriverName := 'SQLite';
Conn.LoginPrompt := false;
Conn.Params.clear;
Conn.Params.Database := TPath.Combine(TargetFolder, 'testDB.sdb');
Conn.Params.Add('DriverID=SQLite');
// all this is the result of several attemp to fix the table locking error. none worked
Conn.Params.Add('LockingMode=Normal');
Conn.Params.Add('Synchronous=Normal');
Conn.UpdateOptions.UpdateMode := TUpdateMode.upWhereAll;
Conn.UpdateOptions.LockWait := True;
Conn.UpdateOptions.LockMode := TFDLockMode.lmPessimistic;
Conn.UpdateOptions.LockPoint := TFDLockPoint.lpImmediate;
Conn.UpdateOptions.AssignedValues := [uvLockMode,uvLockPoint,uvLockWait];
Conn.Open();
Conn.ExecSQL('CREATE TABLE IF NOT EXISTS ''test'' (''ID'' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,''data1'' TEXT NOT NULL,''data2'' INTEGER NOT NULL)');
Table := TFDTable.Create(nil);
try
table.Connection := Conn;
while True do
begin
case Trunc(Random(10)) of
0..3:
begin
table.Open('test');
try
if table.Locate('data1', 'name'+intToStr(Trunc(Random(10))),[TLocateOption.loCaseInsensitive]) then
begin
table.Edit;
table.FieldByName('data2').AsInteger := table.FieldByName('data2').AsInteger + 1;
table.Post;
end;
finally
table.close;
end;
end;
4..8:
begin
table.Open('test');
try
i := Trunc(Random(10));
if not table.Locate('data1', 'name'+ i.ToString,[TLocateOption.loCaseInsensitive]) then
begin
table.AppendRecord([null, 'name'+ i.ToString, 0]);
end;
finally
table.close;
end;
end
else
break;
end;
end;
finally
FreeAndNil(Table);
end;
finally
FreeAndNil(Conn);
end;
end;
Thanks to Victoria, I managed to find the right parameters.
Conn := TFDConnection.Create(nil);
try
Conn.DriverName := 'SQLite';
Conn.LoginPrompt := false;
Conn.Params.clear;
Conn.Params.Database := TPath.Combine(TargetFolder, 'testDB.sdb');
Conn.Params.Add('DriverID=SQLite');
Conn.Params.Add('SharedCache=False');
Conn.Params.Add('LockingMode=Normal');
Conn.Params.Add('Synchronous=Normal');
Conn.UpdateOptions.LockWait := True;
Conn.Open();
Thanks again

Error using SQLite ATTACH - DETACH in Delphi

I experience a, for me unsolvable, problem with ATTACH and DETACH in SQLite, using Delphi (Firedac).
I have one database file connected and attach a second one with:
FDConnection1.ExecSQL('ATTACH DATABASE "' + Import_DB_filename + '" AS IMPORTDB;');
Therein, the variable 'Import_DB_filename' contains the full path and filename of the database file.
This works OK and I can access both databases within the connection through the FireDac queries, and can do my coding without problems.
However, things go wrong upon detaching:
FDConnection1.ExecSQL('DETACH DATABASE IMPORTDB;');
In debugging mode, I always get the error:
Debugger Exception Notification
E Project My_Program.EXE raised exception class $C0000005 with message 'access violation at 0x00405d7b: read of address 0x00000000'.
Apparently something goes wrong with the memory assignments, since the debugger stops in a (assembly) function SysFreeMem(P:Pointer): Integer; in GETMEM.INC.
Whatever I try, the error persists and associates a memory leak that eventually leads to a crash of the compiler (Delphi Seattle Enterprise).
Even attaching and subsequent detaching of the database without passing any code results in the same error.
(FDconnection: locking mode = lmNormal;
JournalMode = jmOff or jmWALL or jmdelete)
I do hope that you can help me out on this lasting problem.
If you run the project below, you should find that:
You can access two Sqlite databases quite happily using separate FDConnections and FDTables.
You can move data from a table in one db to a table of the same name in the other using a FireDAC FDDataMove component.
Code:
unit BatchMoveu;
interface
[...]
type
TForm3 = class(TForm)
FDConnection1: TFDConnection;
DBGrid1: TDBGrid;
DataSource1: TDataSource;
DBNavigator1: TDBNavigator;
FDGUIxWaitCursor1: TFDGUIxWaitCursor;
FDPhysSQLiteDriverLink1: TFDPhysSQLiteDriverLink;
Button1: TButton;
FDTable1: TFDTable;
FDConnection2: TFDConnection;
DataSource2: TDataSource;
DBGrid2: TDBGrid;
btnBatchMove: TButton;
FDDataMove1: TFDDataMove;
FDTable2: TFDTable;
procedure btnBatchMoveClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
procedure PopulateTable1;
procedure TestDataMove;
public
procedure CreateDatabase(DBName : String; FDConnection : TFDConnection;
FDTable : TFDTable);
end;
var
Form3: TForm3;
implementation
{$R *.dfm}
const
DBName1 = 'd:\delphi\code\sqlite\db1.sqlite';
DBName2 = 'd:\delphi\code\sqlite\db2.sqlite';
procedure TForm3.Button1Click(Sender: TObject);
begin
CreateDatabase(DBName1, FDConnection1, FDTable1);
CreateDatabase(DBName2, FDConnection2, FDTable2);
PopulateTable1;
FDTable2.Open;
end;
procedure TForm3.CreateDatabase(DBName : String; FDConnection : TFDConnection;
FDTable : TFDTable);
var
AField : TField;
i : Integer;
begin
if FileExists(DBName) then
DeleteFile(DBName);
AField := TLargeIntField.Create(Self);
AField.FieldName := 'ID';
AField.DataSet := FDTable;
AField.Name := AField.DataSet.Name + 'IDField';
AField := TWideStringField.Create(Self);
AField.Size := 80;
AField.FieldName := 'Name';
AField.DataSet := FDTable;
AField.Name := AField.DataSet.Name + 'NameField';
FDConnection.Params.Values['database'] := DBName;
FDConnection.Connected:= True;
FDTable.CreateTable(False, [tpTable]);
end;
procedure TForm3.PopulateTable1;
var
i : Integer;
begin
FDTable1.Open;
for i:= 1 to 1000 do begin
FDTable1.InsertRecord([i, 'Row ' + IntToStr(i)]);
end;
FDTable1.Close;
//FDConnection1.Commit;
FDTable1.Open;
end;
procedure TForm3.TestDataMove;
var
Item : TFdMappingItem;
begin
Item := FDDataMove1.Mappings.Add;
Item.SourceFieldName := 'ID';
Item.DestinationFieldName := 'ID';
Item := FDDataMove1.Mappings.Add;
Item.SourceFieldName := 'Name';
Item.DestinationFieldName := 'Name';
FDDataMove1.Source := FDTable1;
FDDataMove1.Destination := FDTable2;
FDDataMove1.Options := FDDataMove1.Options - [poOptimiseSrc];
FDDataMove1.Execute;
FDConnection2.Connected := False;
FDTable2.Open;
end;
procedure TForm3.btnBatchMoveClick(Sender: TObject);
begin
TestDataMove;
end;
procedure TForm3.FormDestroy(Sender: TObject);
begin
FDConnection1.Close;
end;
end.

Error reading a file using UTL_FILE

I am facing some issue in reading a file using utl_file.
The same code is working fine in some of the instances and failing in one instance.
Below is the issue:
Code:
procedure del_mul_tags(p_request_id in number)
is
fileID UTL_FILE.FILE_TYPE;
fileID1 UTL_FILE.FILE_TYPE;
line varchar2(32000);
l_dir varchar2(1000);
l_file varchar2(20);
l_file1 varchar2(20);
l_request_id number;
l_count number := 0;
l_count_t number :=0;
l_write boolean := TRUE;
cursor c_dir is
select substr(OUTFILE_NAME,1,instr(OUTFILE_NAME,'/',-1,1)-1),substr(OUTFILE_NAME,instr(OUTFILE_NAME,'/',-1,1)+1)
from fnd_concurrent_requests
where request_id = p_request_id;
begin
open c_dir;
fetch c_dir into l_dir,l_file;
close c_dir;
execute immediate 'create or replace directory W2_OUT_DIR as ''' || l_dir || '''';
begin
fileID1 := UTL_FILE.FOPEN ('W2_OUT_DIR', l_file, 'R'); -----Getting the error right here!!!-----------
loop
begin
UTL_FILE.GET_LINE (fileID1, line);
--Some Logic
EXCEPTION WHEN NO_DATA_FOUND THEN
UTL_FILE.FCLOSE(fileID1);
EXIT;
end;
end loop;
fileID := UTL_FILE.FOPEN ('W2_OUT_DIR', l_file, 'R');
loop
begin
UTL_FILE.GET_LINE (fileID, line);
--Some Logic
EXCEPTION WHEN NO_DATA_FOUND THEN
UTL_FILE.FCLOSE(fileID);
EXIT;
end;
end loop;
end;
EXCEPTION WHEN OTHERS THEN
fnd_file.put_line(fnd_file.log,'Error while deleting the xml tags: '||SQLERRM);
end;
................
In this instance file, l_file 'ACHAKR01.23067873' was created through another concurrent process. Its OS user is 'appsofde'.
And the unix permissions are as below:
-rw-r--r-- 1 appsofde dba 192092429 jan 27 05:00 ACHAKR01.23067873
The directory W2_OUT_DIR is created and the oracle user apps has EXECUTE,READ,WRITE privileges.
The file exists and the W2_OUT_DIR dir patch is correct.
Exact error coming is:
Error while deleting the xml tags: ORA-29283: invalid file operation
ORA-06512: at "SYS.UTL_FILE", line 536
ORA-29283: invalid file operation
Any thoughts here?
You will want to make sure the filesystem is presented to your Oracle DB host (not just your application server). I'm assuming it is otherwise you would get an invalid directory path error instead. I would suggest you double check however and also ensure the oracle account on the DB host has access to read the file in question. The account appsofde sounds as if its likely the application account.

SQLite insert on android works only once

I'm with a problem on Delphi, i create a simple app to test the mobile power of RadStudio, I created a simple app that put some data into some inputs and then add it to database when button is clicked. I followed this Embarcadero tutorial as starting point
The problem is that I only get one entry added, then no more entries are added or the list is not refreshed. Below some code:
Table creation:
procedure TTabbedForm.logAfterConnect(Sender: TObject);
begin
log.ExecuteDirect('CREATE TABLE IF NOT EXISTS lista (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,idCons INTEGER,nome TEXT,kms INTEGER,kmsAlarme INTEGER,quantidade INTEGER,quantidadeAlarme INTEGER,data INTEGER,dataAlarme INTEGER,alarmeMsg TEXT)');
end;
Add button code:
procedure TTabbedForm.btnGravarClick(Sender: TObject);
begin
try
SQLQueryInsert.ParamByName('idCons').AsInteger := PopupBoxTipo.ItemIndex;
SQLQueryInsert.ParamByName('nome').AsString := PopupBoxTipo.Text;
SQLQueryInsert.ParamByName('kms').AsInteger := StrToInt(kmsEdit.Text);
SQLQueryInsert.ParamByName('quantidade').AsInteger := StrToInt(qtdEdit.Text);
SQLQueryInsert.ParamByName('data').AsInteger := DateTimeToUnix(dtaEvento.Date);
SQLQueryInsert.ExecSQL();
lista.Refresh;
LinkFillControlToField1.BindList.FillList;
except
on e: Exception do
begin
ShowMessage(e.Message);
end;
end;
end;
If you need some more code snippet, please ask!
Thanks in advance for any reply!
Try this method it works for me
procedure TData.InsertItem(someObject: TSomeObjectClass);
var
qry: TFDQuery;
begin
qry := CreateQry( 'insert into SomeObject(id, description, something)'+
' values (:id, :description, :something);', false);
qry.Params.ParamByName('id').AsInteger := someObject.id;
qry.Params.ParamByName('description').asstring := someObject.description;
qry.Params.ParamByName('something').asstring := someObject.something;
qry.Prepare;
qry.execsql;
qry.Free;
end;
I've put and object in the parameter but you can also put the data you want to insert seperatly

Resources