Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3499
  • Last Modified:

Delphi - DataSet and Text files

Hi

How can i load table fields / data from text file . and how can i save it to the file
thanks
0
ibrobar
Asked:
ibrobar
  • 13
  • 13
1 Solution
 
ziolkoCommented:
if you use ADO the easiest way is use built in functionality for XML:

ADOQuery1.SaveToFile('c:\mydata.xml', pfXML);
ADOQuery1.LoadFromFile('c:\mydata.xml');

but if you prefer other DB controls or dont want xml then
loop thru all fields and all records and save to .csv file


procedure TForm1.Button1Click(Sender: TObject);
var i: Integer;
begin
  while not datasource.EOF do begin
    for i := 0 to datasource.fieldscount - 1 do
      //
    datasource.next;
  end;
end;

ziolko.
0
 
ibrobarAuthor Commented:
Hi zioloko

Thanks for your help but could please write me the code . and i don't know about Ado
Cann't we use like an excel file or simple text file

thanks , regads
0
 
ziolkoCommented:
>>Cann't we use like an excel file or simple text file
it's impossible to write one piece of code to read from any file
there are completely different methods to read excel files, different for csv files...
so if you want some working piece of code tell me what is file's format

ziolko.

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ibrobarAuthor Commented:
Dear Ziolko

I am using simple text file to get TreeView nodes but afcourse when i am closing the application I am losing any changes i am doing to the tree that's why i want to save my text file to a database.
I know that i can load the tree nodes from a database directly without using file, but i want to give the user of the application the ability to open a file and to creat his own tree simply .
so how can i do this

thanks
0
 
ziolkoCommented:
ahhh now it's a lot easier to help you:)
in your Q you wrote about fields and data so I thought
you want to save some database table to file:)

ok, if you want to store structure of TreeView in textfile first thing you
need to do is decide what format use, for example:
node_id parentnode_id node_caption
1            -1                     root1
2            -1                     root2
3            1                      1st child of root1
4            1                      2nd child of root1  
5            2                      1st child of root2
.
.
.

is this way is good for you I can paste here some working code,
just let me know

ziolko.
0
 
ibrobarAuthor Commented:
Hi Ziolko

let's say i am using as simple as notepad, and i am filling the text file with my tree nodes (it's easy to do).
Up to now everything is good , but my problem is to save this file to a database so i won't lose the changes i am doing to the tree during the runtime.
For example if i remove a node or i add a new one . if i logged out my program and then logged in again
I will lose any changes and i will get the same tree again. so what i want to do is when i do changes either i want to save it to database and then load it again from the database or if it is possible to save the tree after the modification to the file it self.

regards
0
 
ziolkoCommented:
i'm confused... correct me if i'm wrong.
1. you have treeview
2. you make changes to treeview (add/remove/move treenodes)
3. you save state of treeview to file
4. you want save file (created in point 3) to database
and then when you start your app next time
1. read file from database
2. read treeview state from file
3. restore treeview state to what you read form file

is that right?

ziolko.
0
 
ibrobarAuthor Commented:
Hi ziolko

Let me explain to you :  I have a treeView
1. First I want to load it's nodes from a file.
2. Second I am doing changes to the nodes like add / reomve and edit
3. before i leave my application i want to store these changes to the same file. means i want to save treeview to a file.
Is it possible ............ ??

P.S : in my application i am giving the users the possibilty to load a default tree which i loaded from a database.
And the other option is to creat his own tree from a text file cause it looks easy like using word and typing so he open a text file write his chosen tree and then loaded to the tree which i can do it .
up to here it's good but now when any changes i want it to restore it back to the file . afcourse i can save it to a database and load again from it but here i will get back to the first solution which i don't want unless my demand cann't be done.

thanks a lot
0
 
ziolkoCommented:
sure it can be done.
just one more question what data should be loaded into tree,
except tree structure and captions?

ziolko
0
 
ibrobarAuthor Commented:
Hi zoliko

sorry for the delay , i was in a holiday.
What Data I am loading to the tree is just the years and the months
my tree takes the following shape :
Library
           2000
                   jan
                  feb
                   ..........
                 dec
           2001
           2002
and so on.
that's it
0
 
ziolkoCommented:
ok, i got it
i'll post working code.. maybe not tomorrow but on weekend for sure

so how was your holiday? are you ready for new lines of code?:)

ziolko.
0
 
ibrobarAuthor Commented:
Hi Zoilko

Thanks for your help , I enjoyed the holiday afcourse , although i was sitting on my pc for long times

regards
0
 
ibrobarAuthor Commented:
Hi zoliko I am stil waiting for your code.

thanks a lot
0
 
ziolkoCommented:
TreeView.txt:
2005
      January
      February
      March
      April
      May
      June
      July
      August
      September
      October
      November
      December
2006
      January
      February
      March
      April
      May
      June
      July
      August
      September
      October
      November
      December
2007
      January
      February
      March
      April
      May
      June
      July
      August
      September
      October
      November
      December


important thing is that before month name there is TAB character (Chr(9)) NOT
series of space characters

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

type

  TForm1 = class(TForm)
    Button1: TButton;
    TreeView1: TTreeView;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    procedure LoadFromFile;
    procedure SaveToFile;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  LoadFromFile;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  SaveToFile;
end;

procedure TForm1.LoadFromFile;
var infile: TStringList;
    cnt: Integer;
    line: string;
    root: TTreeNode;
begin
  root := nil;
  infile := TStringList.Create;
  try
    infile.LoadFromFile('TreeView.txt');
    for cnt := 0 to infile.Count - 1 do begin
      line := infile[cnt];
      if Length(line) <> 0 then begin
        if line[1] = #9 then begin
          TreeView1.Items.AddChild(root, Copy(line, 2, Length(line)))
        end else begin
          root := TreeView1.Items.AddChild(nil, line);
        end;
      end;
    end;
  finally
    infile.Free;
  end;
end;

procedure TForm1.SaveToFile;
var outfile: TStringList;
    cnt: Integer;
    line: string;
begin
  outfile := TStringList.Create;
  try
    for cnt := 0 to TreeView1.Items.Count - 1 do begin
      if TreeView1.Items[cnt].Parent = nil then
        line := TreeView1.Items[cnt].Text
      else
        line := #9 + TreeView1.Items[cnt].Text;
      outfile.Add(line);
    end;
    outfile.SaveToFile('TreeView.txt');
  finally
    outfile.Free;
  end;
end;

end.

ziolko.
0
 
ibrobarAuthor Commented:
hI ZIOLKO

Thanks for your help . i will try it today.
can you help with this , I am asking everywhere but no body is answering me , why I don't know ? is it that difficult . help me with it and  i will double the point.
I need to add icons to combobox to the left of the text. I know that i have to use the ondrawitem and canvas but it's more complicated afcourse.
thanks a lot
0
 
ibrobarAuthor Commented:
Hi ziolko

I have a comment on your solution .
Cann't we simply use these two commands :

TreeView.LoadFromFile('FileName');  And
TreeView.SaveFromFile('FileName')

thanks

0
 
ziolkoCommented:
heh funny:)
TreeView.LoadFromFile and TreeView.SaveToFile  
look exactly as i did it:)

yup, you need set ComboBox.Style to csOwnerDrawFixed or csOwnerDrawVariable
and then use OnDrawItem()

i never did it with TComboBox but i shouldn't be as complicated:)

ziolko.
0
 
ibrobarAuthor Commented:
Hi ziolko

So i'd rather use the treeview.save and load from file cause it's shorter , right :)
Anyway thanks a lot
0
 
ziolkoCommented:
sure, no problem:)
and about that combobox i'll see what i can do:)
ziolko.
0
 
ziolkoCommented:
here's some code:

procedure TForm1.Button1Click(Sender: TObject);
begin
  ComboBox1.Items.Add('ala');
  ComboBox1.Items.Add('ola');
  ComboBox1.Items.Add('ela');
end;

procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer; Rect:
    TRect; State: TOwnerDrawState);
var AText: string;
    pic: TGraphic;
begin
  pic := Image1.Picture.Icon;
  AText := ComboBox1.Items[Index];
  ComboBox1.Canvas.Draw(Rect.Left, Rect.Top, pic);
  InflateRect(Rect, -8, 0);
  OffsetRect(Rect, 8, 0);
  ComboBox1.Canvas.FillRect(Rect);
  DrawTextEx(ComboBox1.Canvas.Handle, PChar(AText), Length(AText), Rect,
              DT_LEFT or DT_VCENTER or DT_SINGLELINE or DT_NOCLIP, nil );
end;

procedure TForm1.ComboBox1MeasureItem(Control: TWinControl; Index: Integer; var
    Height: Integer);
begin
  Height := 16;
end;

Image1 contains 16X16 icon

ziolko.
0
 
ibrobarAuthor Commented:
Hi ziolko

I will check and let you know

thanks
0
 
ibrobarAuthor Commented:
Hi ziolko

I tried the code and it does draw an icon to the left of the text in a CombBox but it's not so accurate.
It's coming over the text and its big the 16*16.
what can i do in this case.
thanks
0
 
ziolkoCommented:
just change this values:

  InflateRect(Rect, -8, 0);
  OffsetRect(Rect, 8, 0);

procedure TForm1.ComboBox1MeasureItem(Control: TWinControl; Index: Integer; var
    Height: Integer);
begin
  Height := 16;
end;

untill you get desired effect

ziolko.
0
 
ibrobarAuthor Commented:
Hi

I tried this yesterday , but it didn't work like i want, maybe the reason is the icon i am using.
Can we use other than 16*16 icon.
What if i have smaller or bigger should i change the height in measureItem procedure.

regards
0
 
ziolkoCommented:
yup, you have to change height in on measure item and both InflateRect() and OffestRect()

ziolko.
0
 
ziolkoCommented:
is this Q still open?

please see here:
http://www.experts-exchange.com/help.jsp#hs5

ziolko.
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 13
  • 13
Tackle projects and never again get stuck behind a technical roadblock.
Join Now