Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 06-11-2003, 06:08 AM
Aris Aris is offline
Junior Member
 
Join Date: Jun 2003
Posts: 4
Default Load from file into table

In informix sql i use :load from file "file_name" delimiter "|" insert into table

How can i do this with delphi ?
I have a delimited text file .
Reply With Quote
  #2  
Old 06-11-2003, 07:54 AM
simes simes is offline
Senior Member
 
Join Date: Jan 2002
Posts: 2,327
Default RE: Load from file into table

You don't do it with Delphi, but you may be able do it with your database. What database are you using?

To do it with Delphi, you'd have to load the file, then parse it and write the individual fields to the DB.

Please click accept if this helps

Simon
SadMan Software
http://www.simes.clara.co.uk
(No emails please unless explicitly requested!)
Reply With Quote
  #3  
Old 06-11-2003, 08:48 AM
w0rd-driven w0rd-driven is offline
Member
 
Join Date: Jun 2001
Posts: 70
Default RE: Load from file into table

They use informix apparently. They already do it in their database, they WANT to do it from delphi. Doing it the long and hard way I might add.

There's a couple of approaches. You can hard code the delimited file to always use the same result, or use an alternative method I like.

The first line will be the header line. It will give you every field in the table as a header. Then in delphi you simply read the 1st line and know the exact fieldnames you wish to create in the database. A program I use does this and it increases the ability to modularize the contents, rather than use flat files and recode delphi if something changes. I'm sure informix probably does this as well but I thought I would make it clear.

Then comes the fun part. You have to make a connection to your database and then create the table and each field in it. The best thing I would think is to use a stringlist to gather up all the headers, then do a for loop with the stringlist and get everything line by line, adding things one at a time.

Nothing in delphi does this automatically to my knowledge. Someone may have programmed a component for your database type (informix, or if you use something else) that will do this for you but they most likely took the same approach I outlined.

So in conclusion what you are asking is possible, just won't be too easy unless you find a component to do it for you.

Jeremy
Reply With Quote
  #4  
Old 06-11-2003, 08:52 AM
jamiguel77 jamiguel77 is offline
Senior Member
 
Join Date: Jan 2002
Posts: 1,155
Send a message via MSN to jamiguel77 Send a message via Yahoo to jamiguel77
Default RE: Load from file into table

u can with delphi:

Fisrt u need create a ODBC dsn the with a component Tdatabase (2 click in the component, choose your Odbc DSn and setup the parameters, username, PAssword), u drop too a Tquery component and Setup to databasename,

in Sql property of Tquery write:

load from file "file_name" delimiter "|" insert into table


in the form too drop a button, 2 clicks in the button and type:

query1.execsql;

work fine... but........

1) u only can add the files that are in your informix (if the informix are in Unix), u need transfer the file via FTP or any other way, if u want open with delphi the file and add to table u need follow these way:
var
f:Textfile;
l:string;
begin
Assignfile(f,c:\x.txt);
reset(f);
while not eof(f) do
begin
readln(f,l);
wfield1:=copy(l,1,pos('|',l)-1);
delete(l,1,pos('|',l));
wfield2:=copy(l,1,pos('|',l)-1);
delete(l,1,pos('|',l));
wfield3:=copy(l,1,pos('|',l)-1);
delete(l,1,pos('|',l));
wfield4:=copy(l,1,pos('|',l)-1);
delete(l,1,pos('|',l));
query1.sql.text:='Insert into mytable (field1,field2,field3,field4) values ('''+wfield1+''','''+wfield2+''','''+wfield3+''',' ''+wfield4+''')';
query1.execsql;
end;
closefile(f);

in the sentence:
all fields for me are of type string
if by sample wfiel3 is integer then u need these statment:
query1.sql.text:='Insert into mytable (field1,field2,field3,field4) values ('''+wfield1+''','''+wfield2+''','+wfield3+','''+w field4+''')';


thanks
accept the Answer if help you thanks
Reply With Quote
  #5  
Old 06-12-2003, 01:00 AM
simes simes is offline
Senior Member
 
Join Date: Jan 2002
Posts: 2,327
Default RE: Load from file into table

Ah right. I misunderstood. I thought that since he already had a method of loading the data with Informix, he wanted to load it into a different database.

Please click accept if this helps

Simon
SadMan Software
http://www.simes.clara.co.uk
(No emails please unless explicitly requested!)
Reply With Quote
  #6  
Old 06-12-2003, 02:28 AM
mshkolnik mshkolnik is offline
Senior Member
 
Join Date: Jul 2001
Posts: 4,195
Default RE: Load from file into table

You may use SMImport suite: http://www.scalabium.com/smi

Allow to load data into dataset from TXT/CSV, MS Excel, MS Word, MS Access, HTML, XML and other sources

With best regards, Mike Shkolnik
http://www.scalabium.com
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 10:39 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2014, vBulletin Solutions, Inc.