Recent

Author Topic: Easy to use memory dataset  (Read 27230 times)

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Easy to use memory dataset
« on: February 26, 2016, 10:01:32 am »
Hi,

I want a memory dataset like  VirtualTable and really I cant understand what are these bufdataset and memdataset  and wiki page as I cant make bufdataset works without a file.
I want an easy to sue memory dataset without any file usage.

Thaddy

  • Hero Member
  • *****
  • Posts: 18305
  • Here stood a man who saw the Elbe and jumped it.
Re: Easy to use memory dataset
« Reply #1 on: February 26, 2016, 10:36:56 am »
You don't have to save to file or create a file as long as the database is open.
Here's a simple example:
Code: Pascal  [Select][+][-]
  1. program memdbtest;
  2. {$apptype console}{$mode objfpc}
  3. uses
  4.   db,memds;
  5. var
  6.    MemDb:TMemDataset;
  7. begin
  8.  MemDb:=TMemDataset.Create(nil);
  9.  try
  10.    MemDb.FieldDefs.Add('NAME',ftString,20);
  11.    MemDb.CreateTable;
  12.    MemDb.Open;
  13.    MemDb.Append;
  14.    MemDb.FieldByName('NAME').Value:='Free';
  15.    MemDb.Post;
  16.    MemDb.Append;
  17.    MemDb.FieldByName('NAME').Value:='Pascal';
  18.    MemDb.Post;
  19.    MemDb.SaveToFile('MemDb.txt');  // as long you do not close, you don't have to save
  20.  finally
  21.    MemDb.Close;
  22.    MemDb.Free;
  23.  end;
  24. end.

Note: example modified from the mailing list. http://lists.lazarus.freepascal.org/pipermail/lazarus/2009-January/040684.html

You can of course also SaveToStream / LoadFromStream;
« Last Edit: February 26, 2016, 10:43:28 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Re: Easy to use memory dataset
« Reply #2 on: February 26, 2016, 10:50:41 am »
Thanks Thaddy,
Another question:
As in wiki is said BufDataset is a better choice but is it really and is it a choice for using as a memory dataset?

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Re: Easy to use memory dataset
« Reply #3 on: February 26, 2016, 11:01:53 am »
I checked your code and it works but Codetools has problem with memds unit and also it will replace it with MemDataSet unit that has a different TMemDataset without CreateTable option.

Thaddy

  • Hero Member
  • *****
  • Posts: 18305
  • Here stood a man who saw the Elbe and jumped it.
Re: Easy to use memory dataset
« Reply #4 on: February 26, 2016, 11:12:29 am »
Dunno about how codetools would screw that up. It should not. Are you using the dreaded CodeTyphon?

BufDataSet has a the wiki explains some more possibiliities like custom filters, autoincrement data type and the Locate method. If you need those (at the cost of a little more memory pressure, but not much) by all means use BufDataSet.

Here's a BufDataset example. Basically, you would call CreateDataSet instead of CreateTable.

Code: Pascal  [Select][+][-]
  1. program bufdbtest;
  2. {$apptype console}{$mode objfpc}
  3. uses
  4.   db,BufDataset;
  5. var
  6.   BufDb:TBufDataset;
  7. begin
  8.  BufDb:=TBufDataset.Create(nil);
  9.  try
  10.    BufDb.FieldDefs.Add('NAME',ftString,20);
  11.    BufDb.CreateDataSet;
  12.    BufDb.Open;
  13.    BufDb.Append;
  14.    BufDb.FieldByName('NAME').Value:='Free';
  15.    BufDb.Post;
  16.    BufDb.Append;
  17.    BufDb.FieldByName('NAME').Value:='Pascal';
  18.    BufDb.Post;
  19.    BufDb.SaveToFile('BufDb.txt');
  20.  finally
  21.    BufDb.Close;
  22.    BufDb.Free;
  23.  end;
  24. end.
« Last Edit: February 26, 2016, 11:20:53 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Re: Easy to use memory dataset
« Reply #5 on: February 26, 2016, 11:22:17 am »
Its interesting and your works good but my tests with a component made in IDE not in code and playing with options lead to this error :
Quote
Missing (compatible) underlying dataset, can not open
I dont know the reason but making a new component even in IDE without changing any property and do the stuff in the code make it work.
Thanks.

Thaddy

  • Hero Member
  • *****
  • Posts: 18305
  • Here stood a man who saw the Elbe and jumped it.
Re: Easy to use memory dataset
« Reply #6 on: February 26, 2016, 11:38:49 am »
Here's simple code to get you started.
You wil have to add the db unit to the uses clause.:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5.   Classes, SysUtils, BufDataset, Db, FileUtil, Forms, Controls, Graphics,
  6.   Dialogs, DbCtrls, DBGrids;
  7.   // I had to manually add the db unit to the uses clause... tdk.
  8. type
  9.  
  10.   { TForm1 }
  11.  
  12.   TForm1 = class(TForm)
  13.     BufDataset1: TBufDataset;
  14.     DataSource1: TDataSource;
  15.     DBEdit1: TDBEdit;
  16.     DBGrid1: TDBGrid;
  17.     DBNavigator1: TDBNavigator;
  18.     procedure FormCreate(Sender: TObject);
  19.     procedure FormDestroy(Sender: TObject);
  20.   private
  21.     { private declarations }
  22.   public
  23.     { public declarations }
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. { TForm1 }
  34.  
  35. procedure TForm1.FormCreate(Sender: TObject);
  36. begin
  37.   BufDataSet1.FieldDefs.Add('NAME',ftString,20);
  38.   BufDataSet1.CreateDataset;
  39.   BufDataSet1.Open;
  40. end;
  41.  
  42. procedure TForm1.FormDestroy(Sender: TObject);
  43. begin
  44.   BufDataSet1.Close;
  45. end;
  46.  
  47. end.
  48.  
  49.  

Put a DataSource, a DbNavigator, a DbEdit and a BufDatasource on the form and set the datasource and dataset properties. That's all.
« Last Edit: February 26, 2016, 11:46:53 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Re: Easy to use memory dataset
« Reply #7 on: February 26, 2016, 11:42:06 am »
Thanks I did the same and it works but my problem was a TBufDataset that I made changes in propeties and it makes it crash but without any changes as you showed it works.
Thank you Thaddy.

Thaddy

  • Hero Member
  • *****
  • Posts: 18305
  • Here stood a man who saw the Elbe and jumped it.
Re: Easy to use memory dataset
« Reply #8 on: February 26, 2016, 11:47:35 am »
Thanks I did the same and it works but my problem was a TBufDataset that I made changes in propeties and it makes it crash but without any changes as you showed it works.
Thank you Thaddy.

Have a look at my by now modified example. It works. Don't activate the dataset through the designer first time. Wait. I created the fielddefs in code.
« Last Edit: February 26, 2016, 11:56:21 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Easy to use memory dataset
« Reply #9 on: February 26, 2016, 11:56:13 am »
With Virtualtable you can assign from another dataset.  Bufdataset can't (even tried it with loadfromstream). Still strange, because TSQLQuery is an inheritence of TBufDataset.
Memdataset can do this, but what Thaddy says, it's less powerfull than Bufdataset.

Hopefully in the future bufdataset will be equivalent with virtualtable
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

Thaddy

  • Hero Member
  • *****
  • Posts: 18305
  • Here stood a man who saw the Elbe and jumped it.
Re: Easy to use memory dataset
« Reply #10 on: February 29, 2016, 06:32:22 am »
This may also be of interest for submitter:
https://www.sqlite.org/inmemorydb.html

It works with Lazarus!
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Re: Easy to use memory dataset
« Reply #11 on: February 29, 2016, 06:58:09 am »
Good point Thaddy, its useful but I should investigate more about how assign from another dataset with Bufdataset because it is moreeasy to use.

cdbc

  • Hero Member
  • *****
  • Posts: 2462
    • http://www.cdbc.dk
Re: Easy to use memory dataset
« Reply #12 on: February 29, 2016, 10:35:56 am »
Hi
Why don't you implement the "CopyFromDataset" method yourself, in TBufDataset from TMemDataset and then send the patch to the community, it doesn't look too hard  ;)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6 -> FPC 3.2.2 -> Lazarus 4.0 up until Jan 2025 from then on it's both above &: KDE6/QT6 -> FPC 3.3.1 -> Lazarus 4.99

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Re: Easy to use memory dataset
« Reply #13 on: February 29, 2016, 10:59:05 am »
Yes you are right and as I said I will investigate more about it.

Thaddy

  • Hero Member
  • *****
  • Posts: 18305
  • Here stood a man who saw the Elbe and jumped it.
Re: Easy to use memory dataset
« Reply #14 on: February 29, 2016, 11:09:22 am »
Well. I am looking into the fact that the memdataset can do things that the bufdataset can't do.
There is an obvious design flaw considering the inheritance. It may be the case that we have to move some code from one to the other. Mmmm, where's BigChimp when you need him ;) O:-)
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

 

TinyPortal © 2005-2018