Recent

Author Topic: Record Locked  (Read 3926 times)

Petrus Vorster

  • Full Member
  • ***
  • Posts: 241
Record Locked
« on: July 14, 2025, 03:43:15 pm »
Greeting all

Thank you for all the help. The searched, entries, updates and so forth work perfectly now.
Now i need to Delete some records in a normal DBF file searching TWO strings.
This is what I did :
(DM1 is DATAMODULE]

Code: Pascal  [Select][+][-]
  1. dbf:= dm1.Company;
  2.       Found := False;
  3.       DBF.First;
  4.       dbf.Open;
  5.  
  6.       begin
  7.       while not DBF.Eof do
  8.       begin
  9.       if (dbf.FieldByName('Boxnum').AsString= EDIT1.TEXT) AND
  10.          (dbf.fieldbyname('Branch').AsString=EDIT2.text) then
  11.        begin
  12.         dbf.Edit;
  13.         found:=true;
  14.  
  15.         dbf.Delete;
  16.        end;
  17.        dbf.Next;
  18.        end;
  19.                                      
It finds the records perfectly, but I get the "RECORD LOCKED" error in each instance.

I can append, Read, write and modify to that database with no trouble, its just the delete that gives the error.

As usual, I must be something small I am missing.

Much thanks for your patience.

-Peter

BrunoK

  • Hero Member
  • *****
  • Posts: 766
  • Retired programmer
Re: Record Locked
« Reply #1 on: July 14, 2025, 04:42:19 pm »

Code: Pascal  [Select][+][-]
  1.        begin
  2.         dbf.Edit;
  3.         found:=true;
  4.  
  5.         dbf.Delete;
  6.        end;
  7.  
It finds the records perfectly, but I get the "RECORD LOCKED" error in each instance.

Probably dbf.Edit locks the record thus preventing dbf.Delete.
Try putting a dbf.Cancel before the dbf.Delete.

Sieben

  • Sr. Member
  • ****
  • Posts: 387
Re: Record Locked
« Reply #2 on: July 14, 2025, 05:08:35 pm »

Probably dbf.Edit locks the record thus preventing dbf.Delete.
Try putting a dbf.Cancel before the dbf.Delete.

Why enter Edit-mode at all...?
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

CharlyTango

  • Full Member
  • ***
  • Posts: 178
Re: Record Locked
« Reply #3 on: July 14, 2025, 09:13:45 pm »
It finds the records perfectly, but I get the "RECORD LOCKED" error in each instance.

You mentioned "in each instance". Is there more than one instance of the datamodule which is currently working? Maybe they lock each other.

As i remember old DBF days it was not a good idea to do delete series topdown.
I'd prefer bottom-up

Code: Pascal  [Select][+][-]
  1. procedure TForm1.deletecompany;
  2. begin
  3.  
  4. dbf.Open;
  5. dbf.Last;
  6. begin
  7. while not DBF.BOF do
  8. begin
  9.   if (dbf.FieldByName('Boxnum').AsString= EDIT1.TEXT) AND
  10.      (dbf.fieldbyname('Branch').AsString=EDIT2.text) then
  11.    dbf.Delete;
  12.   end;
  13.  
  14.   dbf.Prior;
  15. end;
  16.  
  17. end;

and this strategy can only be used when there are a few records in a table.
I'd solve it with an indexed search previous to the while loop


Lazarus stable, Win32/64

Petrus Vorster

  • Full Member
  • ***
  • Posts: 241
Re: Record Locked
« Reply #4 on: July 15, 2025, 08:31:45 am »
What would I have done without you all?

I had the weirdest results on my own attempts.  :D

Thanks a million.

Peter

Petrus Vorster

  • Full Member
  • ***
  • Posts: 241
Re: Record Locked
« Reply #5 on: July 15, 2025, 08:45:34 am »
Ok, that last example does work and deletes everything, but it also hangs the application.

I have a Tdbf control and a Tdbgrid connected to that database.

Man, I wish i did these things years ago and not stayed with primitive flat files in Basic.
I did a little Sqlite with Basic, but I used a great tool that i had to do nothing, and also learn nothing.
I feel completely lost with starting Sqlite in LPC, hence me toying With DBF.

-Peter

Thaddy

  • Hero Member
  • *****
  • Posts: 19017
  • Glad to be alive.
Re: Record Locked
« Reply #6 on: July 15, 2025, 09:05:13 am »
@CharlyTango
In dBase, on deletion of records only the deleted flag is set, nothing is really removed, so updown or downup deletes makes no difference.
This is only relevant when you want to pack the database and really remove the records. In that case work from last to first.
That is, if TDbf follows the dBase standards.
« Last Edit: July 15, 2025, 09:07:50 am by Thaddy »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

Petrus Vorster

  • Full Member
  • ***
  • Posts: 241
Re: Record Locked
« Reply #7 on: July 15, 2025, 10:42:43 am »
That does make sense.

I have made some changes to how I "Delete" or mark records to be deleted and then just overwrite that record with the next insert into that DBF
Since I do not connect a DBgridview to that database the marked records wont bother anyone.

For now, this is resolved.

Thanks a million.

-Peter

Thaddy

  • Hero Member
  • *****
  • Posts: 19017
  • Glad to be alive.
Re: Record Locked
« Reply #8 on: July 15, 2025, 02:46:03 pm »
That does make sense.
Yes it does, it is the dBase format specification..... <sigh>
The first byte in a record is set to * to mark it is deleted otherwise it is empty. You can only see that in RAW format.
The dBase PACK command subsequently removes the records marked for deletion for real.
(I am an expert on the format, I used to work for PerfectView, which is a dBase derivative)

https://www.dbase.com/Knowledgebase/INT/db7_file_fmt.htm
Just if you want to write your own too.... 8) :D >:D
« Last Edit: July 15, 2025, 03:29:03 pm by Thaddy »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

CharlyTango

  • Full Member
  • ***
  • Posts: 178
Re: Record Locked
« Reply #9 on: July 15, 2025, 06:27:04 pm »
Man, I wish i did these things years ago and not stayed with primitive flat files in Basic.
I did a little Sqlite with Basic, but I used a great tool that i had to do nothing, and also learn nothing.
I feel completely lost with starting Sqlite in LPC, hence me toying With DBF.
In my opinion, you should rather build up knowledge about SQLite instead of relying on an old technology.
The same effort of solved problems will bring you more in the medium term. With Lazarus you have a very powerful tool to use SQL databases and SQLite is (IMHO) one of the simpler ones if you follow some basic rules.

The rules for table design and field design are quite similar to dBase. And you don't even have to worry about indexes in the frontend

With an SQL connection (assuming SQLQuery1 has a correct database connection) your task (without exception handling) would look like this

Code: Pascal  [Select][+][-]
  1.   with SQLQuery1 do begin
  2.     Close;
  3.     SQL.Text:='DELETE FROM Company WHERE Boxnum = :boxn AND Branch = :bra';
  4.     ParamByName('boxn').AsString:=Edit1.Text;
  5.     ParamByName('bra').AsString:=Edit2.Text;
  6.   end;
  7.   SQLQuery1.ExecSQL;
  8.                      
Lazarus stable, Win32/64

Thaddy

  • Hero Member
  • *****
  • Posts: 19017
  • Glad to be alive.
Re: Record Locked
« Reply #10 on: July 15, 2025, 07:28:38 pm »
Yes, you are correct, and if he switches to SQLite that will make his life a lot easier.
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

 

TinyPortal © 2005-2018