PDA

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&#40;const id_report&#58; string&#41;;
var
sSql,s, sCampi&#58; String;
F1&#58; Textfile;
begin




try
// - STEP 010
sSql &#58;='SELECT ......something ';


setQry&#40;sSql,qry_dyn&#41;;
qry_dyn.Active &#58;= True;
qry_dyn.First;

Savedialog1.DefaultExt&#58;='*.csv';
SaveDialog1.Filter &#58;= 'Comma separeted files &#40;*.csv&#41;|*.CSV|Text files &#40;*.txt&#41;|*.TXT';

if SaveDialog1.Execute then
AssignFile&#40;F1, SaveDialog1.Filename&#41;
else Exit;

Rewrite&#40;F1&#41;;


while not qry_dyn.Eof do
begin
writeln&#40;F1,
qry_dyn.FieldByName&#40;'a'&#41;.AsString + '|' +
qry_dyn.FieldByName&#40;'b'&#41;.AsString + '|' +
//.....
qry_dyn.FieldByName&#40;'z'&#41;.AsString&#41;;

qry_dyn.Next;
end; //while
if &#40;Debug_enable > 1&#41; then caricaLogProcedure&#40;'TForm1.SaveReport;','010','OK' ,''&#41;;
except
try
Closefile&#40;F1&#41;;
except
//errore nella chiususa file
ShowMessage&#40;'Errore duranta la scrittura del tipo1, impossibile chiudere il file'&#41;;
end;
ShowMessage&#40;'Bugggone!'&#41;;
if &#40;Debug_enable > 0&#41; then caricaLogProcedure&#40;'TForm1.SaveReport;','010','KO' ,sSql&#41;;
end;

Closefile&#40;F1&#41;;


end;