📄 c++ builder 程序设计实用教程 - 7_4 sql在数据库中的使用 - 张晋 - 17xie.htm
字号:
style="FONT-FAMILY: 楷体_GB2312; mso-ascii-font-family: 'Times New Roman'">事件</SPAN></SPAN></H4>
<P class=MsoNormalIndent><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">本程序(也是本节)的重点在于向读者说明在</SPAN><SPAN
lang=EN-US>CBuilder 5</SPAN></SPAN><SPAN
style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中</SPAN><SPAN
lang=EN-US>SQL</SPAN></SPAN><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">语句的用法,所以在窗体上放置了一个</SPAN><SPAN
lang=EN-US>Memo</SPAN></SPAN><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">控件,用于接收用户的输入,而具体的执行动作是由按钮“执行</SPAN><SPAN
lang=EN-US>SQL</SPAN></SPAN><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">”来完成的。</SPAN></SPAN></P>
<P class=MsoNormalIndent><SPAN style="mso-bookmark: _Toc392384325"><SPAN
lang=EN-US><o:p> </o:p></SPAN></SPAN></P>
<P class=MsoNormalIndent><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在程序的设计阶段,用鼠标的左键双击窗体上的“执行</SPAN><SPAN
lang=EN-US>SQL</SPAN></SPAN><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">”按钮,在屏幕上就会弹出一个代码窗口,把光标移动到相应的事件处理过程中,并且添加如下代码:</SPAN></SPAN></P>
<P class=a1><SPAN style="mso-bookmark: _Toc392384325"><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">void __fastcall
TForm1::Button1Click(TObject *Sender)<o:p></o:p></SPAN></SPAN></P>
<P class=a1><SPAN style="mso-bookmark: _Toc392384325"><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">{<o:p></o:p></SPAN></SPAN></P>
<P class=a1><SPAN style="mso-bookmark: _Toc392384325"><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">Form1->Query1->Close();<o:p></o:p></SPAN></SPAN></P>
<P class=a1><SPAN style="mso-bookmark: _Toc392384325"><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">//</SPAN></SPAN><SPAN
style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">关闭数据库</SPAN></SPAN><SPAN
style="mso-bookmark: _Toc392384325"><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'"><o:p></o:p></SPAN></SPAN></P>
<P class=a1><SPAN style="mso-bookmark: _Toc392384325"><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">Form1->Query1->SQL->Clear();<o:p></o:p></SPAN></SPAN></P>
<P class=a1><SPAN style="mso-bookmark: _Toc392384325"><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">//</SPAN></SPAN><SPAN
style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">清除</SPAN></SPAN><SPAN
style="mso-bookmark: _Toc392384325"><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">SQL</SPAN></SPAN><SPAN
style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">语句</SPAN></SPAN><SPAN
style="mso-bookmark: _Toc392384325"><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'"><o:p></o:p></SPAN></SPAN></P>
<P class=a1><SPAN style="mso-bookmark: _Toc392384325"><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">Form1->Query1->SQL->Add(Form1->Memo1->Text);<o:p></o:p></SPAN></SPAN></P>
<P class=a1><SPAN style="mso-bookmark: _Toc392384325"><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">//</SPAN></SPAN><SPAN
style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">读入新的</SPAN></SPAN><SPAN
style="mso-bookmark: _Toc392384325"><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">SQL</SPAN></SPAN><SPAN
style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">语句</SPAN></SPAN><SPAN
style="mso-bookmark: _Toc392384325"><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'"><o:p></o:p></SPAN></SPAN></P>
<P class=a1><SPAN style="mso-bookmark: _Toc392384325"><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">Form1->Query1->Open();<o:p></o:p></SPAN></SPAN></P>
<P class=a1><SPAN style="mso-bookmark: _Toc392384325"><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">//</SPAN></SPAN><SPAN
style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">打开数据库</SPAN></SPAN><SPAN
style="mso-bookmark: _Toc392384325"><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'"><o:p></o:p></SPAN></SPAN></P>
<P class=a1><SPAN style="mso-bookmark: _Toc392384325"><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">}<o:p></o:p></SPAN></SPAN></P>
<P class=a1><SPAN style="mso-bookmark: _Toc392384325"><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">//-----------------------------------------------------------------------<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormalIndent><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">程序说明:</SPAN></SPAN></P>
<P class=MsoNormalIndent><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这样,在程序的运行过程中,当用户在文本框中输入</SPAN><SPAN
lang=EN-US>SQL</SPAN></SPAN><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">语句并且单击按钮“执行</SPAN><SPAN
lang=EN-US>SQL</SPAN></SPAN><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">”时,程序就会激活控件的</SPAN><SPAN
lang=EN-US>Button1Click()</SPAN></SPAN><SPAN
style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">事件,首先通过控件</SPAN><SPAN
lang=EN-US>Query</SPAN></SPAN><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的</SPAN><SPAN
lang=EN-US>Close</SPAN></SPAN><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">方法把当前打开的数据库关闭,然后清除执行完的</SPAN><SPAN
lang=EN-US>SQL</SPAN></SPAN><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">语句,再通过语句</SPAN><SPAN
lang=EN-US>Form1->Query1->SQL->
Add(Form1->Memo1->Text);</SPAN></SPAN><SPAN
style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">从文本框中读取新的</SPAN><SPAN
lang=EN-US>SQL</SPAN></SPAN><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">语句,最后通过控件</SPAN><SPAN
lang=EN-US>Query</SPAN></SPAN><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的</SPAN><SPAN
lang=EN-US>Open</SPAN></SPAN><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">方法来重新打开数据库,即执行新的</SPAN><SPAN
lang=EN-US>SQL</SPAN></SPAN><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">语句。</SPAN></SPAN></P>
<H4><SPAN style="mso-bookmark: _Toc392384325"><SPAN
lang=EN-US>3</SPAN></SPAN><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 楷体_GB2312; mso-ascii-font-family: 'Times New Roman'">.响应数据浏览操作</SPAN></SPAN></H4>
<P class=MsoNormalIndent><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在窗体上有</SPAN><SPAN
lang=EN-US>8</SPAN></SPAN><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">个用于进行数据浏览和编辑操作的按钮——</SPAN><SPAN
lang=EN-US>Insert</SPAN></SPAN><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">、</SPAN><SPAN
lang=EN-US>Delete</SPAN></SPAN><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">、</SPAN><SPAN
lang=EN-US>Last</SPAN></SPAN><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">、</SPAN><SPAN
lang=EN-US>Next</SPAN></SPAN><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">、</SPAN><SPAN
lang=EN-US>Prior</SPAN></SPAN><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">、</SPAN><SPAN
lang=EN-US>First</SPAN></SPAN><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">、</SPAN><SPAN
lang=EN-US>Set Mark</SPAN></SPAN><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN
lang=EN-US>To Mark</SPAN></SPAN><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">按钮,它们的功能如下所示:</SPAN></SPAN></P>
<P class=MsoNormalIndent
style="MARGIN-LEFT: 42.5pt; TEXT-INDENT: -21.25pt; mso-list: l0 level1 lfo2; tab-stops: list 21.25pt"><SPAN
style="mso-bookmark: _Toc392384325"><SPAN lang=EN-US
style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN
style="mso-list: Ignore">n<SPAN
style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">控件</SPAN><SPAN
lang=EN-US>First</SPAN></SPAN><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">:将数据库的指针移动到第一条记录处;</SPAN></SPAN></P>
<P class=MsoNormalIndent
style="MARGIN-LEFT: 42.5pt; TEXT-INDENT: -21.25pt; mso-list: l0 level1 lfo2; tab-stops: list 21.25pt"><SPAN
style="mso-bookmark: _Toc392384325"><SPAN lang=EN-US
style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN
style="mso-list: Ignore">n<SPAN
style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">控件</SPAN><SPAN
lang=EN-US>Prior</SPAN></SPAN><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">:将数据库的指针移动到前一条记录处;</SPAN></SPAN></P>
<P class=MsoNormalIndent
style="MARGIN-LEFT: 42.5pt; TEXT-INDENT: -21.25pt; mso-list: l0 level1 lfo2; tab-stops: list 21.25pt"><SPAN
style="mso-bookmark: _Toc392384325"><SPAN lang=EN-US
style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN
style="mso-list: Ignore">n<SPAN
style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">控件</SPAN><SPAN
lang=EN-US>Next</SPAN></SPAN><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">:将数据库的指针移动到下一条记录处;</SPAN></SPAN></P>
<P class=MsoNormalIndent
style="MARGIN-LEFT: 42.5pt; TEXT-INDENT: -21.25pt; mso-list: l0 level1 lfo2; tab-stops: list 21.25pt"><SPAN
style="mso-bookmark: _Toc392384325"><SPAN lang=EN-US
style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN
style="mso-list: Ignore">n<SPAN
style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">控件</SPAN><SPAN
lang=EN-US>Last</SPAN></SPAN><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">:将数据库的指针移动到最后一条记录处;</SPAN></SPAN></P>
<P class=MsoNormalIndent
style="MARGIN-LEFT: 42.5pt; TEXT-INDENT: -21.25pt; mso-list: l0 level1 lfo2; tab-stops: list 21.25pt"><SPAN
style="mso-bookmark: _Toc392384325"><SPAN lang=EN-US
style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN
style="mso-list: Ignore">n<SPAN
style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">控件</SPAN><SPAN
lang=EN-US>Insert</SPAN></SPAN><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">:在当前位置插入一条数据;</SPAN></SPAN></P>
<P class=MsoNormalIndent
style="MARGIN-LEFT: 42.5pt; TEXT-INDENT: -21.25pt; mso-list: l0 level1 lfo2; tab-stops: list 21.25pt"><SPAN
style="mso-bookmark: _Toc392384325"><SPAN lang=EN-US
style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN
style="mso-list: Ignore">n<SPAN
style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">控件</SPAN><SPAN
lang=EN-US>Delete</SPAN></SPAN><SPAN style="mso-bookmark: _Toc392384325"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">:将当前位置处的数据删除;</SPAN></SPAN></P>
<P class=MsoNormalIndent
style="MARGIN-LEFT: 42.5pt; TEXT-INDENT: -21.25pt; mso-list: l0 level1 lfo2; tab-stops: list 21.25pt"><SPAN
style="m
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -