Solved

What is the best way to show records in dbgrid, do sql query and modify the records?

Posted on 2001-07-20
9
244 Views
Last Modified: 2010-04-06
Hi all,
I have an event database.
When user open the mainform, I want all the records show in a "dbgrid". Then the user can select some conditions to do a sql query, the query result show in the "dbgrid", and they can modify some fields.

What is the best way to do that? What is the best component to do that? Please give me some
advice.  Thanks.

(I found a component, it name is xRxDBGrid. xSortRxDBGrid is an RxDBGrid (from RxLib) with automatically data sorting when user clicks on field's title button. Support for TRxQuery and
TTable-based grids.   http://deks.webjump.com/)

Regards, supwang
0
Comment
Question by:SupWang
9 Comments
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
hi supwang,

use the TUpdateSQL-Component as Update-Object of your TQuery (property UpdateObject)
fill out the the Update, Insert, Delete Statements in TUpdateSQL
(there is an expert, if you double click on it)
set the cachedupdates property to true of the TQuery
don't forget to call applyupdates, maybe in the afterpost and afterdelete-event of the TQuery

meikl ;-)
0
 
LVL 3

Expert Comment

by:ahll
Comment Utility
Hi,

You can use a TTable component and the filter property, if you filter the table the grid relationed to its datasource will show only the records that applies this filter.

please excuse my english.

bye  
0
 
LVL 2

Author Comment

by:SupWang
Comment Utility
Hi meikl,
I am not go to here for a long time. Your points stride over rwilson now. :-)
I will view the help of TUpdateSQL-Component. Please give me more advice.  

Hi ahll,
I want to use sql. Because the query will be very complicated, and may have many records.

Regards, supwang
0
 
LVL 9

Accepted Solution

by:
ITugay earned 100 total points
Comment Utility
Hi SupWang,

meikl :)

easy way to modify SQL statement is:

procedure TForm1.Query1BeforeOpen(DataSet: TDataSet);
var
  S: string;
begin
  S := 'select * from groups where group_name like "E1%" and user_name like "E2%"';
  S :=  StringReplace(S,   'E1',  Edit1.Text, [rfReplaceAll]);
  S :=  StringReplace(S,   'E2',  Edit2.Text, [rfReplaceAll]);
  Query1.SQL.Text := S;
end;

if user type nothing in TEdit1 then all recods will be shown. If user type some text then after reopenning Query1, dbgrid will keep only records where field "group_name" starts from typed text.

------
Igor
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 3

Expert Comment

by:rondi
Comment Utility
You can use a regular TQuery and TDBGrid/TRxDBGrid.
The TQuery's RequestLive property should be set to True to let
users modify the data.
Then use queries with parameters
 eg.
const
  AT_LEAST_QUERY = "SELECT * FROM MyTable WHERE MyAmount >= :pAmount"

Then when the user exits the TEdit or clicks a "Filter"
button:

with Query1 do    //where Grid's datasource is Query1
begin
  DisableControls;
  try
    if Active then Close;  
    SQL.Clear;
    SQL.Add(AT_LEAST_QUERY);
    ParamByName("pAmount").AsInteger := StrToInt(Edit1.Text);
    Open;
  finally
    EnableControls;
  end;
end;

//-------------------------------------------------

This approach works for me. I just declare all this query
constants and fill the parameters later with whatever I want. The queries can get as complex as you want. I've
had a few with multiple nested SELECT's
eg. SELECT ... WHERE ... IN (SELECT ... WHERE ... IN (SELECT ... WHERE ... = ...))

All you really need to be wary of is the parameter types
so you use the right As... property, esp. with dates.

HTH,
rondi
0
 
LVL 2

Author Comment

by:SupWang
Comment Utility
Hi meikl,
About the TUpdateSQL, it seems that it just be use when cached updates are enabled. But I don?t need to set the cached updates to true. How about I just set the TQuery's RequestLive property to True?

Hi Igor,
I like your comment. :-)  


Please give more advice.

Thanks, supwang
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
yup, TUpdateSQL do only work,
if the TQuery cached updates are set,
but this may not cause a problem

the requestlive Porperty may not work under some conditions,
which depends on the database, which is used

meikl ;-)
0
 
LVL 2

Author Comment

by:SupWang
Comment Utility
Hi,
Sorry, very busy in this time.
Thanks all.
0
 
LVL 2

Author Comment

by:SupWang
Comment Utility
Hi meikl,
What time will need the TUpdateSQL?
If I just use some simple Query and the requestlive property can be true then I don't need the TUpdateSQL. Is it?
And could you give me a very simple example about the TUpdateSQL? (Please don't let me to view the delphi demo)

Regards, supwang
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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

18 Experts available now in Live!

Get 1:1 Help Now