Solved

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

Posted on 2006-07-15
4
320 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 500 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now