?
Solved

CREATE TQUERY AND TUPDATESQL IN RUN TIME AND INSERTING ROW IN MY DATABASE

Posted on 2006-07-15
4
Medium Priority
?
353 Views
Last Modified: 2010-04-05
I WOULD LIKE TO CREATE ONE OBJECT TQUERY AND ONE TUPDATESQL TO INSERT ONE ROW IN MY DATABASE.

HERE IS THE CODE THAT I USE FOR DO THAT, BUT LIKE I SAD I WANT CREATE THIS OBJECTS (TQUERY AND TUPDATESQL) IN RUNTIME:
SQLERRO IS TUPDATESQL AND QUERY_ERRO IS TQUERY

   SQLErro.InsertSQL.Clear;
   SQLErro.InsertSQL.Add('insert into TB_ERRO');
   SQLErro.InsertSQL.Add('(DATA, MENSAGEM, FORMNAME, FORMCAPTION, OBJETOATIVO)');
   SQLErro.InsertSQL.Add(' values');
   SQLErro.InsertSQL.Add('(''' + DateTimeToStr(now()) + ''',');
   SQLErro.InsertSQL.Add('''' + E.Message + ''',');
   SQLErro.InsertSQL.Add('''' + screen.ActiveForm.Name + ''',');
   SQLErro.InsertSQL.Add('''' + Screen.ActiveForm.Caption + ''',');
   SQLErro.InsertSQL.Add('''' + screen.ActiveControl.Name + ''')');
   Query_Erro.Insert;
   Query_Erro.ApplyUpdates;
   SQLErro.InsertSQL.Clear;
0
Comment
Question by:rafaelrgl
  • 3
4 Comments
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 2000 total points
ID: 17115834
the simple way to do this is to select the query on the form
then do a ctrl-c (copy)
next, find a nice spot in your unit and paste

you should get something like

object Query1: TQuery
  DatabaseName = 'DBDEMOS'
  SQL.Strings = (
    'select * from biolife')
  Left = 424
  Top = 296
end

object UpdateSQL1: TUpdateSQL
  Left = 432
  Top = 304
end

now simply replace the bits you need with delkphi code
e.g.
Query_Erro :=  TQuery.create(nil);
Query_Erro.DatabaseName := 'DBDEMOS';
Query_Erro.SQL.Text := 'select * from biolife';

SQLErro := TUpdateSQL.create(nil);

0
 
LVL 1

Author Comment

by:rafaelrgl
ID: 17116001
[Error] FrmPrinc.pas(445): Undeclared identifier: 'Query_Erro'
[Error] FrmPrinc.pas(445): Undeclared identifier: 'TQuery'
[Error] FrmPrinc.pas(446): Undeclared identifier: 'SQLErro'
[Error] FrmPrinc.pas(446): Undeclared identifier: 'TUpdateSQL'
[Error] FrmPrinc.pas(446): Missing operator or semicolon
[Error] FrmPrinc.pas(447): Missing operator or semicolon
[Hint] FrmPrinc.pas(233): Private symbol 'UniqueUser' declared but never used
[Fatal Error] Nucleo_Server.dpr(31): Could not compile used unit 'FrmPrinc.pas'
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 17116871
Sorry, i thought you knew that bit.
If you wish to create it dynamically you will need to make a variable for it somewhere

eg. if you wish to create it for your form you'd have

private // at the top of your unit you will seee this section
    Query_erro: TQuery;
    SQLErro: TUpdateSQL;
end;

but if you wish to create it in an object, eg. for yout TIdTCPServer, then you'd put it in the data object you create for that
eg.
type
    TClientSpecificData = class(TObject)
  private
    Query_erro: TQuery;
    SQLErro: TUpdateSQL;
...
etc...
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 17116889
you will also need to add DB, and DBTables to your USES clause at the top of your unit
eg.
uses windows..(etc)...,DBTables, DB;
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Screencast - Getting to Know the Pipeline
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

839 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