Why does a commit trigger an access violation? - sql

The following code using the Community version of Delphi and Interbase 2020 running on a Windows 10 machine results in the following error when the Commit is executed. What am I doing wrong?
Project FamilyTree.exe raised exception class $C0000005 with message ‘access violation at 0x00481a6f: read of address 0x8b57569f’.
procedure writepreamble;
var
sqltext : string;
basicaction : tbasicaction;
begin
sqltext := 'insert into preambleds(preambleseq,text) values(';
sqltext := sqltext+inttostr(preambleseq)+','+quotedstr(cardimage)+');';
datamodule1.IBSQL1.SQL.text := sqltext;
datamodule1.geddb.DefaultTransaction := datamodule1.IBTransaction1;
datamodule1.IBsql1.Transaction := datamodule1.IBTransaction1;
datamodule1.IBsql1.Transaction.StartTransaction;
datamodule1.ibsql1.ExecuteAction(basicaction) ;
datamodule1.IBsql1.Transaction.Commit;
sqlcount := sqlcount+1;
end;

why you are using `ExecuteAction` ?
use `ExecQuery` to execute insert :
IBSQL1.ExecQuery();
best regards
Moskw#

Related

Is possible to set filter option in SMDBGrid to CaseInsensitive?

I have SMDBGrid component with show filter bar option set to true, but filter just working in case-sensitive mode
1.Try with lower case
2.Try with upper case
I have tried to insert the code in SMDBgrid.pas like this
procedure TSMDBGrid.ApplyFilter;
var
TempCol: Integer;
begin
if (eoFilterAutoApply in ExOptions) then
begin
TempCol := LeftCol;
BeginUpdate;
try
if DataLink.Active then
begin
DataLink.DataSet.CheckBrowseMode;
DataLink.DataSet.Filtered := False;
DataLink.DataSet.OnFilterRecord := nil;
DataLink.DataSet.OnFilterRecord := OnFilterRecord;
DataLink.DataSet.FilterOptions := [foCaseInsensitive]; <-- this the inserted code
DataLink.DataSet.Filtered := not FilterIsEmpty();//True;
end;
finally
LeftCol := TempCol;
EndUpdate;
end;
end;
if Assigned(OnFilterChanged) then
OnFilterChanged(Self);
end;
But no luck, Is posible filter the record ignoring the case?
PS:
I use Delphi 2009
You may use the OnAccentStringConvert event to transform the value for filter in column before compare:
begin
Result := UpperCase(S)
end;
Looks like I cope with this problem too. Trying to find any solution for Delphi XE 10.3 community edition and wrote to author of SMDBGrid and he found workaround.
Please use SQL ADOQuery as follows.
SELECT UPPER(field) FROM your_table
then use event OnAccentStringConvert and uppercase S String as follows:
function TYourFormName.DBGrridNameAccentStringConvert(Sender: TObject; const S: string): string;
begin
Result := UpperCase(S)
end;
This works very ugly, but at least works. Or you may just create filter by yourself for every table.

Commit vs CommitRetaining

I have made a Delphi application, which inserts a row into Firebird database.
There was a problem with a query which a solved via CommitRetaining, but I read that it is not right thing to use, because it may affect the server being more slow. Strange thing happens when I use Commit only, query runs ok, but when I want to see if the row is inserted, Retainingit isn't. It only gets inserted on application terminate. But when using CommitRetaining, the row is inserted instantly.
What may cause the problem?
EDIT: Code using CommitRetaining
adqPom := TADQuery.Create(nil);
adqPom.Connection := form1.ADOConnection1;
adTransakcija := TADTransaction.Create(nil);
adTransakcija.Connection:=form1.ADOConnection1;
adqPom.Transaction:=adTransakcija;
adTransakcija.StartTransaction;
try
with adqPom do
begin
close;
sql.Clear;
sql.Add('insert into uplate(sifra,b_prijema,magacin,datum,iznos,b_uplate,b_izvoda,banka,godina,tr_rac,datum_dokument)');
sql.Add('values(:S,:BP,:M,:D,:I,:BU,:BI,:B,:G,:TR,:DD)');
ParamByName('S').Value := strtoint(edit1.Text);
if Form15.adoqDostavn.FieldValues['A1'] = 3 then
edit3.Text := '99999';
ParamByName('BP').Value := edit3.Text;
ParamByName('M').Value := edit2.Text;
ParamByName('D').Value := strtodate(edit4.Text);
ParamByName('I').Value := StrToFloat(edit5.Text);
ParamByName('BU').Value := Br_Uplate+1;
ParamByName('BI').Value := strtoint(Edit6.Text);
ParamByName('B').Value := Edit8.Text;
ParamByName('G').Value := 2006;
if Form15.adoqDostavn.FieldValues['A1'] = 3 then
ParamByName('TR').Value:= form15.adoqDostavn.FieldValues['B_PRIJEMA']
else
ParamByName('TR').Value:= Form15.adoqDostavn.FieldValues['B_DOST'];
ParamByName('DD').Value:=StrToDate(edit9.Text);
ExecSQL;
end;
adTransakcija.CommitRetaining;
except
adTransakcija.RollbackRetaining;
raise;
end;
FreeAndNil(adTransakcija);
FreeAndNil(adqPom);
EDIT: Code using Commit (actually property of a query is set to autocommit)
adqPom := TADQuery.Create(nil);
adqPom.Connection := form1.ADOConnection1;
with adqPom do
begin
close;
sql.Clear;
sql.Add('insert into uplate(sifra,b_prijema,magacin,datum,iznos,b_uplate,b_izvoda,banka,godina,tr_rac,datum_dokument)');
sql.Add('values(:S,:BP,:M,:D,:I,:BU,:BI,:B,:G,:TR,:DD)');
ParamByName('S').Value := strtoint(edit1.Text);
if Form15.adoqDostavn.FieldValues['A1'] = 3 then
edit3.Text := '99999';
ParamByName('BP').Value := edit3.Text;
ParamByName('M').Value := edit2.Text;
ParamByName('D').Value := strtodate(edit4.Text);
ParamByName('I').Value := StrToFloat(edit5.Text);
ParamByName('BU').Value := Br_Uplate+1;
ParamByName('BI').Value := strtoint(Edit6.Text);
ParamByName('B').Value := Edit8.Text;
ParamByName('G').Value := 2006;
if Form15.adoqDostavn.FieldValues['A1'] = 3 then
ParamByName('TR').Value:= form15.adoqDostavn.FieldValues['B_PRIJEMA']
else
ParamByName('TR').Value:= Form15.adoqDostavn.FieldValues['B_DOST'];
ParamByName('DD').Value:=StrToDate(edit9.Text);
ExecSQL;
end;
FreeAndNil(adqPom);
Commit free the transaction environment and CommitRetaining is a Commit that not free the transaction environment (cursors still open). You can use CommitRetaining in a process but at the end you must use Commit to release the memory.
Usually CommitRetainning is used to optimize a process (that include a big number of Begin/Commit), but at the end of you must this process use Commit to clear memory.

delphi - Save multiple records at once

Dont know how to formulate this exactly so bear with me please... I am saving text from a memo to a database with date selected in the PlannerCalendar1. Since I can select multiple dates in the PlannerCalendar1, how can I post the value of the memo to all dates selected in the PlannerCalendar1?So when I click 'save' the contents of the memo gets saved to all selected dates.Database is SQLite. The table also has an ID field which is autoinc (primary).PlannerCalendar is from the set of TMS components.
procedure TForm1.cxButton1Click(Sender: TObject);
var i:integer;
begin
with UniQuery1 do
begin
UniQuery1.SQL.Text:='INSERT INTO LOG (DATE,PERSON,DONE,TIME) VALUES (:a1,:a2,:a3,:a4)';
UniQuery1.PARAMS.ParamByName('A1').VALUE := PlannerCalendar1.Date;
UniQuery1.PARAMS.ParamByName('A2').VALUE := cxmemo1.Lines.text ;
UniQuery1.PARAMS.ParamByName('A3').VALUE := (0);
UniQuery1.PARAMS.ParamByName('A4').Value := AdvOfficeStatusBar1.Panels[0].Text;
UniQuery1.ExecSQL;
cxmemo1.Clear;
UniTable1.Refresh;
Tried this at the end but it wont work :
with plannercalendar1.Dates do
begin
for i := 0 to -1 do
begin
UniQuery1.PARAMS.ParamByName('A1').VALUE :=plannercalendar1.dates.Add + i ;
UniQuery1.ExecSQL;
end;
I have no idea what a PlannerCalendar is, but presumably there's some way to get at the list of dates that are selected. You want to do something like this:
UniQuery1.SQL.Text:='INSERT INTO LOG (DATE,PERSON,DONE,TIME) VALUES (:a1,:a2,:a3,:a4)';
UniQuery1.PARAMS.ParamByName('A2').VALUE := cxmemo1.Lines.text ;
UniQuery1.PARAMS.ParamByName('A3').VALUE := (0);
UniQuery1.PARAMS.ParamByName('A4').Value := AdvOfficeStatusBar1.Panels[0].Text;
for i := 0 to PlannerCalendar1.NumberOfDatesSelected-1 do begin
UniQuery1.PARAMS.ParamByName('A1').VALUE := PlannerCalendar1.SelectedDate[i];
UniQuery1.ExecSQL;
end;
Of course, NumberOfDatesSelected and SelectedDate are wild guesses. You'll need to find out what they're really called.
You need to use the Planner's SelectionToAbsTime method :-
Var
lStart, lEnd : TDateTime;
Begin
Planner1.SelectionToAbsTime(lStart, lEnd);
For I := Trunc(lStart) To Trunc(lEnd) Do
SaveMemosForDate(I);
End;

Delphi, error :"Access violation at address xxxxxxxx. Read of address yyyyyyyy", at AdoQuery.SQL.Text:='''

"Access violation at address xxxxxxxx. Read of address yyyyyyyy"
On this project i have more than 200 procedures like that, but only this procedure gets error (only windows xp)..
Procedure than have error :
Procedure TfMain.CreateNewDocument(p_money_direction,p_status,p_base:integer);
begin
With fMain.ADOTemp do
Begin
SQL.Clear;
SQL.Text:='INSERT INTO documents '+
'(document_date,fk_id_status,money_direction,'+
'paid,addition,saving,fk_id_base,fk_id_user)'+
'VALUES '+
'(CONVERT(DATE,:pdocument_date,103),:pfk_id_status,'+
':pmoney_direction,0,0,0,:pfk_id_base,:pfk_id_user)';
Parameters.ParamByName('pdocument_date').Value:=Date;
Parameters.ParamByName('pfk_id_status').Value:=p_status;
Parameters.ParamByName('pmoney_direction').Value:=p_money_direction;
Parameters.ParamByName('pfk_id_base').Value:=p_base;
Parameters.ParamByName('pfk_id_user').Value:=fMain.ApplicationVariablers.user_id;
ExecSQL;
End;
end;
Error at :
SQL.Text:='INSERT INTO documents '+
'(document_date,fk_id_status,money_direction,'+
'paid,addition,saving,fk_id_base,fk_id_user)'+
'VALUES '+
'(CONVERT(DATE,:pdocument_date,103),:pfk_id_status,'+
':pmoney_direction,0,0,0,:pfk_id_base,:pfk_id_user)';
Debuingging process, error at line 1907 :
I fix the problem like that
Procedure TfMain.CreateNewDocument(p_money_direction,p_id_status,p_id_base:integer);
Var
ADOTemp:TAdoQuery;
begin
ADOTemp:=TADOQuery.Create(nil);
ADOTemp.Connection:=fMain.ADOConnection;
With ADOTemp do
Begin
SQL.Clear;
SQL.Add('INSERT INTO documents');
SQL.Add('(document_date,fk_id_status,money_direction,');
SQL.Add('paid,addition,saving,fk_id_base,fk_id_user)');
SQL.Add('VALUES ');
SQL.Add('(CONVERT(DATE,GETDATE(),103),:pfk_id_status,:pmoney_direction,');
SQL.Add('0,0,0,'+IntToStr(p_id_base)+',:pfk_id_user)');
Parameters.ParamByName('pfk_id_status').Value:=p_id_status;
Parameters.ParamByName('pmoney_direction').Value:=p_money_direction;
//Parameters.ParamByName('p').Value:=p_id_base;
Parameters.ParamByName('pfk_id_user').Value:=fMain.ApplicationVariablers.user_id;
ExecSQL;
End;
end;
I removed Parameters.ParamByName('p').Value:=p_id_base; and it worked
http://oi43.tinypic.com/dvotxx.jpg
But it is not good solition
As first, change assigment for text property using "add" insthead .text.
Using text is not good. some year ago I've issue uisng text property for a complex sql statement.
Also, I suggest you:
FORGET WITH STATEMENT.
Each time I works on code using "with" I spend (waste) more more time to understand code.
With fMain.ADOTemp do
Begin
AdoTemp.SQL.Clear;
AdoTemp.SQL.Add( ' INSERT INTO documents ');
AdoTemp.SQL.Add( ' (document_date,fk_id_status,money_direction, ');
....

Delphi ADO SQL Syntax Error

I am getting an Syntax Error when processing the following lines of code. Especially on the AQ_Query.Open;
procedure THauptfenster.Button1Click(Sender: TObject);
var
option: TZahlerArray;
begin
option := werZahlte;
AQ_Query.Close;
AQ_Query.SQL.Clear;
AQ_Query.SQL.Add('USE wgwgwg;');
AQ_Query.SQL.Add('INSERT INTO abrechnung ');
AQ_Query.SQL.Add('(`datum`, `titel`, `betrag`, `waldemar`, `jonas`, `ali`, `ben`)');
AQ_Query.SQL.Add(' VALUES ');
AQ_Query.SQL.Add('(:datum, :essen, :betrag, :waldemar, :jonas, :ali, :ben);');
AQ_Query.Parameters.ParamByName('datum').Value := DateToStr(mcDatum.Date);
AQ_Query.Parameters.ParamByName('essen').Value := ledTitel.Text;
AQ_Query.Parameters.ParamByName('betrag').Value := ledPreis.Text;
AQ_Query.Parameters.ParamByName('waldemar').Value := option[0];
AQ_Query.Parameters.ParamByName('jonas').Value := option[1];
AQ_Query.Parameters.ParamByName('ali').Value := option[2];
AQ_Query.Parameters.ParamByName('ben').Value := option[3];
AQ_Query.Open;
end;
The error:
I am using MySQL Delphi 2010.
USE and INSERT are two different SQL
commands.
MySQL does not support so
called "Batches".
=> you have to call these commands one-by-one
Looks to me like you're using backticks on the third AQ_Query.SQL.Add line, when you need to use normal single quotes.
Use the database in the sql script.
AQ_Query.SQL.Add('INSERT INTO wgwgwg.dbo.abrechnung ');
AQ_Query.SQL.Add('(`wgwgwg.dbo.abrechnung.datum`, `wgwgwg.dbo.abrechnungtitel`, `wgwgwg.dbo.abrechnungbetrag`, `wgwgwg.dbo.abrechnungwaldemar`, `wgwgwg.dbo.abrechnungjonas`, `wgwgwg.dbo.abrechnungali`, `wgwgwg.dbo.abrechnungben`)');
AQ_Query.SQL.Add(' VALUES ');
AQ_Query.SQL.Add('(:datum, :essen, :betrag, :waldemar, :jonas, :ali, :ben);');
AQ_Query.Parameters.ParamByName('datum').Value := DateToStr(mcDatum.Date);
AQ_Query.Parameters.ParamByName('essen').Value := ledTitel.Text;
AQ_Query.Parameters.ParamByName('betrag').Value := ledPreis.Text;
AQ_Query.Parameters.ParamByName('waldemar').Value := option[0];
AQ_Query.Parameters.ParamByName('jonas').Value := option[1];
AQ_Query.Parameters.ParamByName('ali').Value := option[2];
AQ_Query.Parameters.ParamByName('ben').Value := option[3];
You can also make a new connection to wgwgwg and refer your AQ_Query to the new connection