View Full Version : save the result of a query on a text file
Paizo
29-03-2007, 03:46 PM
hi people,
I'm noob on that :oops:
i have to save the result of a query in a text file, which is the easiest way for do that?
I don't want to install new components i think delphi standard is enought (i'm using delphi 5)
ty for help.
jdarling
29-03-2007, 08:51 PM
Can you provide a bit more information on what exactly you are wanting to do? Saving the results of a query to a text file is a bit vague. Are you looking to do something like in SQL Server Management Console where you can copy the results and paste them into a text file, and if so what Database are you working out of? Are you using a generic provider like ADO and want a generic storage hander? Do you want cross platform or just windows? I could go on, but I think you get the idea.
Paizo
30-03-2007, 08:12 AM
I connect through ODBC to a mysql server;
the server is running winxp and the client too, i'm using Tquery component for execute query.
i just wanna save the result of a select query on a file with column separetor, ex:
name|surname|age|etc..
AthenaOfDelphi
30-03-2007, 09:46 AM
This code snippet assumes that the query is already open. It puts " around strings and uses a comma for the separator, and dumps each record as a new line in a TMemo component.
var
fieldLoop : integer;
temp : string;
begin
while (not qry.eof) do
begin
temp:='';
for fieldLoop:=0 to qry.fieldCount-1 do
begin
if (temp<>'') then
temp:=temp+',';
if (qry.Fields[fieldLoop] is TStringField) then
temp:=temp+'"'+qry.fields[fieldLoop].asString+'"'
else
temp:=temp+qry.fields[fieldLoop].asString;
end;
memo1.lines.add(temp);
qry.next;
end;
end;
Its probably not perfect in that there are possibly other field types that should be wrapper in quotes, but its a start.
Paizo
30-03-2007, 10:18 AM
ty i made something like this and work good:
procedure TForm1.SaveReport(const id_report: string);
var
sSql,s, sCampi: String;
F1: Textfile;
begin
try
// - STEP 010
sSql :='SELECT ......something ';
setQry(sSql,qry_dyn);
qry_dyn.Active := True;
qry_dyn.First;
Savedialog1.DefaultExt:='*.csv';
SaveDialog1.Filter := 'Comma separeted files (*.csv)|*.CSV|Text files (*.txt)|*.TXT';
if SaveDialog1.Execute then
AssignFile(F1, SaveDialog1.Filename)
else Exit;
Rewrite(F1);
while not qry_dyn.Eof do
begin
writeln(F1,
qry_dyn.FieldByName('a').AsString + '|' +
qry_dyn.FieldByName('b').AsString + '|' +
//.....
qry_dyn.FieldByName('z').AsString);
qry_dyn.Next;
end; //while
if (Debug_enable > 1) then caricaLogProcedure('TForm1.SaveReport;','010','OK' ,'');
except
try
Closefile(F1);
except
//errore nella chiususa file
ShowMessage('Errore duranta la scrittura del tipo1, impossibile chiudere il file');
end;
ShowMessage('Bugggone!');
if (Debug_enable > 0) then caricaLogProcedure('TForm1.SaveReport;','010','KO' ,sSql);
end;
Closefile(F1);
end;
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.