Delphi designs several methods for database query

Source: Internet
Author: User

Delphi designs several methods for database query

Delphi is really easy to use in designing database queries. In actual use, I have summarized the following points and shared them with you.
(1) Filtering
(1) General Filtering
Delphi is the simplest and most common method used to design database queries. In table attributes (such as Table1), the filter attribute is set to the condition to be queried. The static setting method is to directly enter the condition after the attribute value, for example: XB = 'male' and NL <35, and then set the value of filtered in another attribute of the table to true. After table1.active is set to true, in this way, you can see the qualified records in DBGrid. The dynamic setting method is to set and change the conditions according to application requirements during the running process of the program. Then, when entering the conditions, you can use:
With Table1 do
Begin
Filter: = 'xb = '+ ''' + 'male' + ''' +' and NL <35 ';
Filtered: = true;
End;
(2) query records starting with a word
To query records with the surname Li, enter XM = 'Li * 'in the filter *';
(3) query remarks or graphic Fields
The above query field is a common field. If you want to query fields such as memo and picture, for example, if you want to query a resume that is not empty, enter the following in the filter attribute value: not (Jl is null ).
(2) Use the SELECT statement
(1) The general conditions are dynamically set as follows (for example, the database name is Da. DB ):
With query1 do
Begin
Close;
If prepared then unprepare;
SQL. Clear;
SQL. Add ('select * from Da Where XM =: XM ');
Params [0]. asstring: = 'Liu qing ';
Prepare;
Open;
End;
Static settings, as long as you enter a condition in the SQL attribute value of query1, if there is a parameter in the condition (the condition value is
: Variable name), enter the condition value in Params value settings. If it is a letter, it must be case sensitive. Set query1 to active to true.
(2) Change the field name displayed in the query results:
For example, change the original field name XM displayed in the query result to the name:
Select XM as name, XB, NL from DA
This method can display the same field for different databases in DBGrid.
Display a field for the calculation result: (for example, query a person's annual salary, XM-name, BM-department, ygz-monthly salary)
Select XM, BM, ygz * 12 as annual salary from DA
Two calculated fields show a result field: (for example, querying the service fee, SL-number of parts used, DJ: unit price of parts, djbh-document number)
Select djbh, sl * DJ as service fee from wxk
(3) fuzzy search:
Use the like statement. For example, to query records of persons in the archive named 'qiang:
Select * from Da Where XM like '% strong %'
Dynamic settings:
Select * from Da Where XM like: XM
Then set the parameter Params [0]. asstring: = '% ';
If you want to query memo fields such as the condition "instructor" contained in your resume:
Select * from Da Where JL like '% instructor %'

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.