📄 042.htm
字号:
<HTML><HEAD><meta http-equiv="Content-Type" content="text/html; charset=GB2312"><TITLE>—DELPHI基础教程—第十七章 SQL编程(二)</TITLE><META NAME="keywords" CONTENT=" DELPHI基础教程 第十七章 SQL编程(二)"><META NAME="description" CONTENT=" - DELPHI基础教程 - 第十七章 SQL编程(二)"><style><!--#page {position:absolute; z-index:0; left:0px; top:0px}.tt3 {font: 9pt/12pt "宋体"}.tt2 {font: 12pt/15pt "宋体"}a {text-decoration:none}a:hover {color: blue;text-decoration:underline}--></style></HEAD><body text="#000000" bgcolor="#FFFFFF" leftmargin="3" topmargin="3" marginheight="3" marginwidth="3"><TABLE WIDTH="100%" CELLPADDING=10 CELLSPACING=0 BORDER=0><TR><TD CLASS="tt3" VALIGN="top" width="8%" ><strong><A HREF="043.htm"><FONT style="FONT-SIZE: 9pt">后一页</font></A><BR><A HREF="041.htm"><FONT style="FONT-SIZE: 9pt">前一页</font></A><BR><A HREF="index.html"><FONT style="FONT-SIZE: 9pt">回目录</font></A><BR></strong></TD><TD class="tt2" width="84%"><center><B><FONT style="FONT-SIZE: 16.5pt" COLOR="#FF6666" FACE="隶书">第十七章 SQL编程(二)</FONT></B></center><hr width="94%"><p>17.5 SQL编程实例 </p><p>我们在学习了SQL程序的编写方法之后,我们便可以着手创建自己的应用程序了,通过创建应用程序我们对Delphi的强大功能就会有更深刻的印象,同时会进一步全面掌握有关SQL编程的知识,在本节中我们主要介绍两个例子,前一个例子主要是用静态的SQL语句编程,后一个例子是用动态SQL语句编程。 </p><p>17.5.1 设计简单的SQL程序编辑器 </p><p>例17.1:在这个例子中,我们设计一个交互式的SQL程序编辑器,在这个编辑器中,我们可以根据SQL语言的语法规则,编写常用的SQL命令,并通过单击编辑器中的有关的按钮,直接执行编写好的SQL命令,SQL命令的执行结果也会及时地通过一个TDBGrid部件显示出来。 </p><p>表17.3 SQL编辑器中个主要部件的属性</p><p>━━━━━━━━━━━━━━━━━━━━</p><p>部 件 属 性 值</p><p>────────────────────</p><p>Form1 Caption=SQL程序编辑器</p><p>DBGrid1 DataSource=DataSource1</p><p>Button1 Caption=执行(&E)</p><p>Button2 Caption=清除(&C)</p><p>Button3 Caption=退出(&X)</p><p>Button3 kind=bkClose</p><p>Memo1 </p><p>DataSource1 DataSet=Query1</p><p>Query1 DatabaseName=DEMOS</p><p>━━━━━━━━━━━━━━━━━━━━</p><p> </p><p>因为我们在设置Query1的DatabaseName属性时将其设置为DEMOS,所以我们设计的这个SQL程序编辑器只能对DEOMS中的数据库表进行操作。</p><p>单击按钮Button1的事件处理过程代码为:</p><p> </p><p>程序清单17.1</p><p> </p><p>procedure TForm1.Button1Click(Sender:TObject);</p><p>begin</p><p>Query1.close;</p><p>Query1.SQL.clear;</p><p>Query1.SQL.Add(Memo1.text);</p><p>Query1.Open;</p><p>end;</p><p> </p><p>单击按钮Button2的事件处理过程为:</p><p> </p><p>程序清单17.2</p><p> </p><p>procedure TForm1.Button2Click(Sender:TObject);</p><p>begin</p><p>Query1.close;</p><p>Query1.SQL.clear;</p><p>Query1.ExceSQL;</p><p>end;</p><p> </p><p>下面我们对程序清单17.1和程序清单17.2中的程序代码进行简要的分析:</p><p>程序清单17.1中的程序代码是用来执行查询的。</p><p> </p><p>Query1.close;</p><p> </p><p>这一行程序是用来关闭Query1的,我们在前面的章节中介绍过,只有在调用close方法将TQuery部件关闭之后,才能修改其SQL属性值,执行close命令关闭查询是很安全的,如果查询已经被关闭了,调用该方法不会产生任何影响。</p><p> </p><p>Query1.SQL.clear;</p><p> </p><p>因为TQuery部件的SQL属性只能包含一条SQL语句,调用Clear方法的目的是为了清除SQL属性原来的属性值即原来的SQL命令语句,如果不调用clear方法清除原来的SQL命令语句,当在后面的程序中调用Add方法为SQL属性设置新的SQL命令语句时,Delphi会将新的SQL命令语句加在原来的SQL命令语句,这样使得SQL属性中包含两条独立的SQL语句,这是不允许的。</p><p> </p><p>Query1.SQL.Add(Memo.text);</p><p> </p><p>该条命令是将SQL编辑器的编辑区内的内容(TMemo部件Memo1)设置成Query1的SQL属性值。</p><p> </p><p>Query1.open;</p><p> </p><p>该语句用来执行Query1中的SQL命令语句,如果执行查询从数据库中获得查询结果,查询结果会在数据网格DBGrid1中显示出来。</p><p>程序清单2是用来清除查询的,其前两行语句跟程序清单1中的代码是一样的。Query1.ExecSQL有一些特别,调用ExecSQL方法也是打开Query1,ExecSQL方法与open方法不一样的,请参看前面的章节,当Query1中SQL属性值为空时,即没有SQL语句时,只能调用ExecSQL方法来打开Query1,如果调用open 方法会返回一个错误。 在执行完 Query1.ExecSQL语句之后,应用程序将会清除数据网格DBGrid1中的所有内容。 </p><p>17.5.2 设计一个数据库查询器 </p><p>例17.2:在数据库查询器中,用户可以选择要查询的数据库,查询数据库中的那一个表、根据数据库表中那一个字段进行查询,并且可以方便地指定查询条件,指定查询条件主要包括指定逻辑运算符(=、>、<、<=、>=、like、in、NOTlike、NOT in)和字段值。</p><p>例子全部的程序清单如下:</p><p>unit main;</p><p> </p><p>interface</p><p> </p><p>uses</p><p>SysUtils, Windows, Messages, Classes, Graphics, Controls,</p><p>Forms, Dialogs, StdCtrls, DB, DBTables, Buttons, ComCtrls, Tabnotbk;</p><p> </p><p>type</p><p>TQueryForm = class(TForm)</p><p>BitBtn1: TBitBtn;</p><p>DataSource1: TDataSource;</p><p>Table1: TTable;</p><p>GroupBox1: TGroupBox;</p><p>CheckBox1: TCheckBox;</p><p>CheckBox2: TCheckBox;</p><p>PageControl1: TPageControl;</p><p>TabSheet1: TTabSheet;</p><p>Label5: TLabel;</p><p>Label1: TLabel;</p><p>Label2: TLabel;</p><p>Label3: TLabel;</p><p>Label4: TLabel;</p><p>ListBox1: TListBox;</p><p>ListBox2: TListBox;</p><p>ListBox3: TListBox;</p><p>Edit1: TEdit;</p><p>ComboBox1: TComboBox;</p><p>BitBtn2: TBitBtn;</p><p>TabSheet2: TTabSheet;</p><p>Memo1: TMemo;</p><p>procedure FormCreate(Sender: TObject);</p><p>procedure ListBox1Click(Sender: TObject);</p><p>procedure ListBox2Click(Sender: TObject);</p><p>procedure BitBtn2Click(Sender: TObject);</p><p>end;</p><p> </p><p>var</p><p>QueryForm: TQueryForm;</p><p> </p><p>implementation</p><p> </p><p>{$R *.DFM}</p><p> </p><p>uses RSLTFORM;</p><p> </p><p>procedure TQueryForm.FormCreate(Sender: TObject);</p><p>begin</p><p>Screen.Cursor := crHourglass;</p><p> </p><p>{ Populate the alias list }</p><p> </p><p>with ListBox1 do</p><p>begin</p><p>Items.Clear;</p><p>Session.GetAliasNames(Items);</p><p>end;</p><p> </p><p>{ Make sure there are aliases defined }</p><p> </p><p>Screen.Cursor := crDefault;</p><p>if ListBox1.Items.Count < 1 then</p><p>MessageDlg( 'There are no database aliases currently defined. You</p><p>need at least one alias to use this demonstration.',</p><p>mtError, [mbOK], 0 );</p><p> </p><p>{ Default the drop-down list to the first value in the list }</p><p>ComboBox1.ItemIndex := 0;</p><p>end;</p><p> </p><p>procedure TQueryForm.ListBox1Click(Sender: TObject);</p><p>var</p><p>strValue: string; { Holds the alias selected by the user }</p><p>bIsLocal: Boolean; { Indicates whether or not an alias is local }</p><p>slParams: TStringList; { Holds the parameters of the selected alias }</p><p>iCounter: Integer; { An integer counter variable for loops}</p><p>begin</p><p> </p><p>{ Determine the alias name selected by the user }</p><p> </p><p>with ListBox1 do</p><p>strValue := Items.Strings[ItemIndex];</p><p> </p><p>{ Get the names of the tables in the alias and put them in the</p><p>appropriate list box, making sure the user's choices are reflected</p><p>in the list. }</p><p> </p><p>ListBox2.Items.Clear;</p><p>Session.GetTableNames(strValue, { alias to enumerate }</p><p>'', { pattern to match }</p><p>CheckBox1.Checked, { show extensions flag }</p><p>CheckBox2.Checked, { show system tables flag }</p><p>ListBox2.Items); { target for table list }</p><p> </p><p>{ Make sure there are tables defined in the alias. If not, show an</p><p>error; otherwise, clear the list box. }</p><p> </p><p>Screen.Cursor := crDefault;</p><p>if ListBox2.Items.Count < 1 then</p><p>MessageDlg('There are no tables in the alias you selected. Please</p><p>choose another', mtError, [mbOK], 0 );</p><p> </p><p>ListBox3.Items.Clear;</p><p>end;</p><p> </p><p>procedure TQueryForm.ListBox2Click(Sender: TObject);</p><p>begin</p><p>Screen.Cursor := crHourglass;</p><p>try</p><p>{ First, disable the TTable object. }</p><p>if Table1.Active then</p><p>Table1.Close;</p><p> </p><p>{ Open the selected table }</p><p> </p><p>with ListBox1 do</p><p>Table1.DatabaseName := Items.Strings[ItemIndex];</p><p> </p><p>with ListBox2 do</p><p>Table1.TableName := Items.Strings[ItemIndex];</p><p> </p><p>{ Open the table and put a list of the field names in the Fields</p><p>list box. }</p><p> </p><p>Table1.Open;</p><p>if Table1.Active then</p><p>Table1.GetFieldNames(ListBox3.Items);</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -