How to save a query result (live data)?

How to save a query result (live data)?

Post by Ninghua Pu (Albe » Thu, 09 Nov 1995 04:00:00



Hi,

I got delphi several days ago and am trying to save the live data into a
.dbf file. Sorry if this question has been asked.

In SQL, you can do:
        INSERT INTO newtable
                SELECT * FROM oldtable
Here newtable and oldtable have the same structure.

Delphi BDE does not support this. What I want to know is how I can save a
TQuery result into a .dbf file instead of live data in TDBGrid?


Albert Pu

 
 
 

How to save a query result (live data)?

Post by Brian C. Wrig » Sat, 11 Nov 1995 04:00:00



Quote:>I got delphi several days ago and am trying to save the live data into a
>.dbf file. Sorry if this question has been asked.

>In SQL, you can do:
>        INSERT INTO newtable
>                SELECT * FROM oldtable
>Here newtable and oldtable have the same structure.

>Delphi BDE does not support this. What I want to know is how I can save a
>TQuery result into a .dbf file instead of live data in TDBGrid?

I am trying to do this as well.  I am writing a Delphi CGI program
that will transfer dbase files across the network that match criteria
entered on a webpage.

The TQuery is nice, because I can dynamically specify the database at
runtime, and so the same program can access any of a number of databases.

What I started working on last night was to use a ttable object to
create a new database (using TTable.CreateTable) with the same structure
as the database I am querying.  I would then use batch move to copy
the records.

The code goes something like this: (Source is a TQuery, NewTable a TTable)

Source.Open;
With NewTable do
  begin
  Active := False;
  DataBaseName := 'c:\tempfile';
  TableName := 'temp.dbf';
  TableType := ttDefault;
  FieldDefs := Source.FieldDefs;
  IndexDefs.Clear;
  CreateTable;
  BatchMove(Source, batCopy)
  Close;
  end;
end;

The problem seems to be in the FieldDefs line.  When I run this program
I (not surprisingly) get an exception saying "bad field def" at the
CreateTable line.

I am pretty sure that I need to copy the FieldDefs one by one to the
NewTable object.  Unfortunately, it looks to me like I will have to
decompose the fielddefs from the TQuery since there is no Add(TFieldDef)
method for FieldDefs. That's my project for this afternoon.

I'll let you know if I have any luck.

Of course, any advice from someone who has done this before would be
appreciated!

Thanks,
Brian

 
 
 

How to save a query result (live data)?

Post by Brian C. Wrig » Sat, 11 Nov 1995 04:00:00



Quote:>What I want to know is how I can save a
>TQuery result into a .dbf file instead of live data in TDBGrid?

Here is the code that I came up with (Thanks to Steve Koterski for
his pointer to the TBatchMove Component).

aSource is a TQuery component which was dropped on the main form at
design time.  I entered the information for it in the Object Inspector,
but no reason why you couldn't instantiate it at run time.

This will create (or overwrite) the dbase file:
'd:\database\programs\copy\temp.dbf' using the results of the TQuery.

{-- QueryRunClick ----------------------------------------------------}
procedure TForm1.QueryRunClick(Sender: TObject);
Var
  NewTable: TTable;
  CreateNew: TBatchMove;
begin
aSource.Open;
NewTable := TTable.Create(Self);
With NewTable do
  begin
  DatabaseName := 'd:\database\programs\copy';
  TableName := 'temp.dbf';
  TableType := ttDBase;
  end;
CreateNew := TBatchMove.Create(Self);
With CreateNew do
  begin
  Source := aSource;
  Destination := NewTable;
  Mode := batCopy;
  Execute;
  end;
CreateNew.Free;
NewTable.Free;
aSource.Close;
end;

 
 
 

1. Query returning live-result...

Hi,

(Using Delphi 2 and Sybase SQL Anywhere 5.02)

I learned that in order to be able to modify the query results
from within a DBGrid, the query must return a live set.

I also learned that a live set is obtained by :
 1 - using the open method of the TQuery
 2 - having a query refering to only one table

This is what I am doing... However, I have in my DBGrid a lookup
field which is working fine into the DBGrid but does not post
the changes to the database...

Am I missing something here? Any help appreciated.

Regards,
Sylvain.

2. Paradox 3.5 with Windows 95

3. Live Query Result Set where PK also FK

4. Save to adPersistXML returns junk

5. Automatically saving query results

6. Paradox 7 -retreiving data from prior records

7. How to I save results from a Local Query

8. T E L E W O R K - Oracle DBA - Work Anywhere

9. how to save the result SQL Server Query in a excel file

10. Save results of a query to a file

11. Save the results of a FOR XML query

12. SQL query to save result table

13. Save result automatically when using SQL Query tool