TDBF works like a charm
Took two minutes to check out TDBF, the free open-source embedded database for Delphi (out of many). Discovered that it actually ships with Lazarus. I did a clean install of Lazarus/FPC on one of my mac’s at work (old macbook) just for fun – and you will find it pre-installed in the database tab.
Right. To get it working all you need to do is the following:
procedure TForm1.Button1Click(Sender: TObject); var x: Integer; mstart: TDateTime; mField: TField; begin dbx.FilePath:=getMyDocsPath; dbx.CreateTable; dbx.Open; mStart:=now; mField:=dbx.fieldByname('name'); for x:=1 to 100000 do begin dbx.Append; mField.AsString:='Name #' + IntToStr(x); dbx.post; end; caption:='Time:' + TimeToStr(now-mStart); dbx.First; listbox1.items.BeginUpdate; repeat listbox1.items.Add(dbx.fieldByName('name').asString); dbx.Next; if listbox1.items.count>100 then break; until dbx.EOF; listbox1.items.EndUpdate; end;
That code will create a new database (remember to add ID and Name fields to the field-def collection property of the component).
The function getMyDocsPath looks like this:
function getMyDocsPath:String; Begin result:='/Users/' + getCurrentUser + '/Documents/'; end;
Note: The above code is for Unix (OS X) only, but should be trivial to figure out for Windows or Linux.
Speed
Well I only tested it on my ancient mac, but I was able to stuff 100.000 records into the flat-file in 16 seconds. This includes (as you can see above) a post for each insert. The time should be a lot better by caching, say, 1000 records in memory before commiting the data to disk.
Either way — TDBF is a great little database if you have a small project, or even if you just want to store options and/or user-data like they do in dot net.
Check out the code from sourceforge here: http://tdbf.sourceforge.net/