?? sql.txt
字號:
● 一個(gè)TDatasource部件
名字為OrderSource,其DataSet屬性被設(shè)置為Orders。
● 兩個(gè)TDBGrid部件
它們分別連接CustSource和OrderSource。
TQuery部件Orders中的動(dòng)態(tài)SQL語句中的參數(shù):CustNo在程序設(shè)計(jì)過程中沒有給它賦值,當(dāng)該應(yīng)用程序運(yùn)行時(shí)Delphi會自動(dòng)地到其Datasource屬性中說明的數(shù)據(jù)源CustSource中查找與參數(shù):CustNo匹配的字段,而CustSource中正好有一個(gè)名字為 CustNo 的字段與參數(shù):CustNo匹配,這樣Customer表中的CustNo字段值被賦給了參數(shù) : CustNo , 而當(dāng)每移動(dòng)Customer表中的記錄指針,參數(shù):CustNo的值會隨之改變,而參數(shù):CustNo的值發(fā)生改變時(shí),Orders中的動(dòng)態(tài)SQL語句會根據(jù)新的參數(shù)值重新查詢,從數(shù)據(jù)庫表中獲取相應(yīng)的訂單數(shù)據(jù),這樣也變實(shí)現(xiàn)了類似于主要--明細(xì)型應(yīng)用。即連接查詢?!?
4.4 Prepare方法的使用
在使用動(dòng)態(tài)SQL語句編程時(shí),常常用到一個(gè)很重要的方法prepare,調(diào)用prepare 方法之后,Delphi會將帶參數(shù)的SQL語句傳送給與其對應(yīng)的數(shù)據(jù)庫引擎,對動(dòng)態(tài)SQL語句進(jìn)行語法分析和優(yōu)化。雖然在用動(dòng)態(tài)SQL語句編程時(shí),調(diào)用prepare方法并不是必須的,但是這里我們要極力推薦調(diào)用prepare方法,因?yàn)檎{(diào)用prepare方法后,會大大提高動(dòng)態(tài)SQL 語句的執(zhí)行性能,特別是當(dāng)要反復(fù)多次執(zhí)行同一條動(dòng)態(tài)SQL語句時(shí),其優(yōu)越性會更加明顯。如果在應(yīng)用程序中執(zhí)行一條SQL語句之前并沒有顯式地調(diào)用prepare方法,每次在執(zhí)行SQL 語句時(shí),Delphi會隱含地調(diào)用propare方法以準(zhǔn)備這個(gè)查詢。
TQuery部件還有一個(gè)prepare屬性,這是一個(gè)布爾型屬性,當(dāng)其屬性值為True時(shí),表明該查詢已被準(zhǔn)備好了( SQL 語句已被傳送到數(shù)據(jù)庫引擎中 ) ,當(dāng)我們使用參數(shù)編輯器Parameters Editor來為動(dòng)態(tài)SQL語句中的參數(shù)賦值時(shí),當(dāng)設(shè)置完相應(yīng)的參數(shù)值并退出參數(shù)編輯器時(shí),Delphi會隱含地調(diào)用prepare方法以準(zhǔn)備好查詢。
當(dāng)SQL語句執(zhí)行完之后,要想準(zhǔn)備下一個(gè)查詢,首先必須調(diào)用close方法,然后才能調(diào)用prepare方法準(zhǔn)備下一個(gè)查詢。一般來說,在一個(gè)應(yīng)用程序中應(yīng)該調(diào)用一次prepare方法,常常在窗體的OnCreate事件處理過程中調(diào)用prepare方法,然后用上述介紹的方法為參數(shù)賦值,最后調(diào)用Open方法或ExecSQL方法執(zhí)行SQL語句,以完成查詢。
當(dāng)然在調(diào)用prepare方法準(zhǔn)備好一個(gè)查詢時(shí),會消耗一些數(shù)據(jù)庫資源,因而每當(dāng)一個(gè)查詢執(zhí)行完畢之后,要養(yǎng)成調(diào)用Unprepare方法以撤消查詢的好習(xí)慣。在運(yùn)行程序過程中,通過程序改變TQuery部件的SQL屬性值時(shí),Delphi會自動(dòng)地調(diào)用Close方法和Unprepare 方法,以撤消查詢。
5 SQL編程實(shí)例
我們在學(xué)習(xí)了SQL程序的編寫方法之后,我們便可以著手創(chuàng)建自己的應(yīng)用程序了,通過創(chuàng)建應(yīng)用程序我們對Delphi的強(qiáng)大功能就會有更深刻的印象,同時(shí)會進(jìn)一步全面掌握有關(guān)SQL編程的知識,在本節(jié)中我們主要介紹兩個(gè)例子,前一個(gè)例子主要是用靜態(tài)的SQL語句編程,后一個(gè)例子是用動(dòng)態(tài)SQL語句編程?!?
5.1 設(shè)計(jì)簡單的SQL程序編輯器
例1:在這個(gè)例子中,我們設(shè)計(jì)一個(gè)交互式的SQL程序編輯器,在這個(gè)編輯器中,我們可以根據(jù)SQL語言的語法規(guī)則,編寫常用的SQL命令,并通過單擊編輯器中的有關(guān)的按鈕,直接執(zhí)行編寫好的SQL命令,SQL命令的執(zhí)行結(jié)果也會及時(shí)地通過一個(gè)TDBGrid 部件顯示出來?!?
表3 SQL編輯器中個(gè)主要部件的屬性
━━━━━━━━━━━━━━━━━━━━
部 件 屬 性 值
────────────────────
Form1 Caption=SQL程序編輯器
DBGrid1 DataSource=DataSource1
Button1 Caption=執(zhí)行(&E)
Button2 Caption=清除(&C)
Button3 Caption=退出(&X)
Button3 kind=bkClose
Memo1
DataSource1 DataSet=Query1
Query1 DatabaseName=DEMOS
━━━━━━━━━━━━━━━━━━━━
因?yàn)槲覀冊谠O(shè)置Query1的DatabaseName屬性時(shí)將其設(shè)置為DEMOS,所以我們設(shè)計(jì)的這個(gè)SQL程序編輯器只能對DEOMS中的數(shù)據(jù)庫表進(jìn)行操作。
單擊按鈕Button1的事件處理過程代碼為:
程序清單17.1
procedure TForm1.Button1Click(Sender:TObject);
begin
Query1.close;
Query1.SQL.clear;
Query1.SQL.Add(Memo1.text);
Query1.Open;
end;
單擊按鈕Button2的事件處理過程為:
程序清單17.2
procedure TForm1.Button2Click(Sender:TObject);
begin
Query1.close;
Query1.SQL.clear;
Query1.ExceSQL;
end;
下面我們對程序清單17.1和程序清單17.2中的程序代碼進(jìn)行簡要的分析:
程序清單17.1中的程序代碼是用來執(zhí)行查詢的?!?
Query1.close;
這一行程序是用來關(guān)閉Query1的,我們在前面的章節(jié)中介紹過,只有在調(diào)用close 方法將TQuery部件關(guān)閉之后,才能修改其SQL屬性值,執(zhí)行close命令關(guān)閉查詢是很安全的,如果查詢已經(jīng)被關(guān)閉了,調(diào)用該方法不會產(chǎn)生任何影響。
Query1.SQL.clear;
因?yàn)門Query部件的SQL屬性只能包含一條SQL語句,調(diào)用Clear 方法的目的是為了清除SQL屬性原來的屬性值即原來的SQL命令語句,如果不調(diào)用clear方法清除原來的SQL命令語句,當(dāng)在后面的程序中調(diào)用Add方法為SQL屬性設(shè)置新的SQL命令語句時(shí),Delphi 會將新的SQL命令語句加在原來的SQL命令語句,這樣使得SQL屬性中包含兩條獨(dú)立的SQL語句,這是不允許的。
Query1.SQL.Add(Memo.text);
該條命令是將SQL編輯器的編輯區(qū)內(nèi)的內(nèi)容(TMemo部件Memo1)設(shè)置成Query1的SQL屬性值。
Query1.open;
該語句用來執(zhí)行Query1中的SQL命令語句,如果執(zhí)行查詢從數(shù)據(jù)庫中獲得查詢結(jié)果,查詢結(jié)果會在數(shù)據(jù)網(wǎng)格DBGrid1中顯示出來。
程序清單2是用來清除查詢的,其前兩行語句跟程序清單1中的代碼是一樣的。Query1.ExecSQL有一些特別,調(diào)用ExecSQL方法也是打開Query1,ExecSQL方法與open方法不一樣的,請參看前面的章節(jié),當(dāng)Query1中SQL屬性值為空時(shí),即沒有SQL語句時(shí),只能調(diào)用ExecSQL方法來打開Query1,如果調(diào)用 open 方法會返回一個(gè)錯(cuò)誤。 在執(zhí)行完 Query1.ExecSQL語句之后,應(yīng)用程序?qū)宄龜?shù)據(jù)網(wǎng)格DBGrid1中的所有內(nèi)容。
5.2 設(shè)計(jì)一個(gè)數(shù)據(jù)庫查詢器
例17.2:在數(shù)據(jù)庫查詢器中,用戶可以選擇要查詢的數(shù)據(jù)庫,查詢數(shù)據(jù)庫中的那一個(gè)表、根據(jù)數(shù)據(jù)庫表中那一個(gè)字段進(jìn)行查詢,并且可以方便地指定查詢條件,指定查詢條件主要包括指定邏輯運(yùn)算符(=、>、<、<=、>=、like、in、NOT like、NOT in)和字段值。
例子全部的程序清單如下:
unit main;
interface
uses
SysUtils, Windows, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls, DB, DBTables, Buttons, ComCtrls, Tabnotbk;
type
TQueryForm = class(TForm)
BitBtn1: TBitBtn;
DataSource1: TDataSource;
Table1: TTable;
GroupBox1: TGroupBox;
CheckBox1: TCheckBox;
CheckBox2: TCheckBox;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
Label5: TLabel;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
ListBox1: TListBox;
ListBox2: TListBox;
ListBox3: TListBox;
Edit1: TEdit;
ComboBox1: TComboBox;
BitBtn2: TBitBtn;
TabSheet2: TTabSheet;
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
procedure ListBox2Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
var
QueryForm: TQueryForm;
implementation
{$R *.DFM}
uses RSLTFORM;
procedure TQueryForm.FormCreate(Sender: TObject);
begin
Screen.Cursor := crHourglass;
{ Populate the alias list }
with ListBox1 do
begin
Items.Clear;
Session.GetAliasNames(Items);
end;
{ Make sure there are aliases defined }
Screen.Cursor := crDefault;
if ListBox1.Items.Count < 1 then
MessageDlg( 'There are no database aliases currently defined. You need at least one alias to use this demonstration.',
mtError, [mbOK], 0 );
{ Default the drop-down list to the first value in the list }
ComboBox1.ItemIndex := 0;
end;
procedure TQueryForm.ListBox1Click(Sender: TObject);
var
strValue: string; { Holds the alias selected by the user }
bIsLocal: Boolean; { Indicates whether or not an alias is local }
slParams: TStringList; { Holds the parameters of the selected alias }
iCounter: Integer; { An integer counter variable for loops}
begin
{ Determine the alias name selected by the user }
with ListBox1 do
strValue := Items.Strings[ItemIndex];
{ Get the names of the tables in the alias and put them in the appropriate list box, making sure the user's choices are reflected
in the list. }
ListBox2.Items.Clear;
Session.GetTableNames(strValue, { alias to enumerate }
'', { pattern to match }
CheckBox1.Checked, { show extensions flag }
CheckBox2.Checked, { show system tables flag }
ListBox2.Items); { target for table list }
{ Make sure there are tables defined in the alias. If not, show an
error; otherwise, clear the list box. }
Screen.Cursor := crDefault;
if ListBox2.Items.Count < 1 then
MessageDlg('There are no tables in the alias you selected. Please
choose another', mtError, [mbOK], 0 );
ListBox3.Items.Clear;
end;
procedure TQueryForm.ListBox2Click(Sender: TObject);
begin
Screen.Cursor := crHourglass;
try
{ First, disable the TTable object. }
if Table1.Active then
Table1.Close;
{ Open the selected table }
with ListBox1 do
Table1.DatabaseName := Items.Strings[ItemIndex];
with ListBox2 do
Table1.TableName := Items.Strings[ItemIndex];
{ Open the table and put a list of the field names in the Fieldslist box. }
Table1.Open;
if Table1.Active then
Table1.GetFieldNames(ListBox3.Items);
finally
Screen.Cursor := crDefault;
end;
end;
procedure TQueryForm.BitBtn2Click(Sender: TObject);
var
strAlias, { Alias name selected by the user }
strTable, { Table name selected by the user }
strField, { Field name selected by the user }
strValue, { Field Value entered by the user }
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -