Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Delphi - DataSet and Text files

Posted on 2007-04-02
28
3,275 Views
Last Modified: 2013-11-23
Hi

How can i load table fields / data from text file . and how can i save it to the file
thanks
0
Comment
Question by:ibrobar
  • 13
  • 13
28 Comments
 
LVL 21

Expert Comment

by:ziolko
ID: 18841825
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
 

Author Comment

by:ibrobar
ID: 18855539
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
 
LVL 21

Expert Comment

by:ziolko
ID: 18856220
>>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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:ibrobar
ID: 18856303
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
 
LVL 21

Expert Comment

by:ziolko
ID: 18856654
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
 

Author Comment

by:ibrobar
ID: 18862549
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
 
LVL 21

Expert Comment

by:ziolko
ID: 18867228
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
 

Author Comment

by:ibrobar
ID: 18868792
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
 
LVL 21

Expert Comment

by:ziolko
ID: 18877201
sure it can be done.
just one more question what data should be loaded into tree,
except tree structure and captions?

ziolko
0
 

Author Comment

by:ibrobar
ID: 18897852
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
 
LVL 21

Expert Comment

by:ziolko
ID: 18901521
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
 

Author Comment

by:ibrobar
ID: 18903472
Hi Zoilko

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

regards
0
 

Author Comment

by:ibrobar
ID: 18916477
Hi zoliko I am stil waiting for your code.

thanks a lot
0
 
LVL 21

Accepted Solution

by:
ziolko earned 125 total points
ID: 18930376
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
 

Author Comment

by:ibrobar
ID: 18938434
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
 

Author Comment

by:ibrobar
ID: 18939107
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
 
LVL 21

Expert Comment

by:ziolko
ID: 18939763
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
 

Author Comment

by:ibrobar
ID: 18939831
Hi ziolko

So i'd rather use the treeview.save and load from file cause it's shorter , right :)
Anyway thanks a lot
0
 
LVL 21

Expert Comment

by:ziolko
ID: 18944764
sure, no problem:)
and about that combobox i'll see what i can do:)
ziolko.
0
 
LVL 21

Expert Comment

by:ziolko
ID: 18964403
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
 

Author Comment

by:ibrobar
ID: 18982513
Hi ziolko

I will check and let you know

thanks
0
 

Author Comment

by:ibrobar
ID: 18996633
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
 
LVL 21

Expert Comment

by:ziolko
ID: 18996704
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
 

Author Comment

by:ibrobar
ID: 18996887
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
 
LVL 21

Expert Comment

by:ziolko
ID: 18997185
yup, you have to change height in on measure item and both InflateRect() and OffestRect()

ziolko.
0
 
LVL 21

Expert Comment

by:ziolko
ID: 19653183
is this Q still open?

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

ziolko.
0
 
LVL 1

Expert Comment

by:Computer101
ID: 21156537
Forced accept.

Computer101
EE Admin
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question