1.建立临时表
数据输入是开发数据库程序的必然环节。在client/server结构中,客户端可能要输入一批数据后,再向服务器的后台数据库提交,这就需要在本地(客户端)建立临时数据表来存储用户输入的数据,待提交后,清除本地表数据。 这种方法的好处是:提高输入效率,减小网络负担。
由于用户一次输入的数据量一般情况下较小(不会超过几百条记录),所以临时表可以建立在内存中,这样处理速度较快。
方法1:使用查询控件(tquery)
第1步:在窗体上放上查询控件(tquery),设置好所连接的数据表。
第2步:使tquery. cachedupdates=true;
tquery. requestlive=true
第3步:在原有的sql语句后加入一条where子语句,要求加入这条where子语句后sql查询结果为空。
例如:
select biolife.″species no″, category, common_name, biolife.″species name″, biolife.″length (cm)″, length_in, notes, graphic
from ″biolife.db″ biolife
where biolife.category=′a′ and biolife.category=′b′
这样临时表就建立完成了。
方法2:使用代码创建临时表
代码如下:
function createtableinmemory(const afielddefs:tfielddefs):tdataset;
var
temptable:tclientdataset;
begin
temptable:=nil;
result:=nil;
if afielddefs$#@60;$#@62;nil then
begin
try
temptable:=tclientdataset.create(application);
temptable.fielddefs.assign(afielddefs);
temptable.createdataset;
result:=(temptable as tdataset);
except
if temptable$#@60;$#@62;nil then
temptable.free;
result:=nil;
raise;
end
end
end;
在程序中按如下方法使用:
procedure tform1.button1click(sender: tobject);
var
adataset:tdataset;
begin
adataset:=tdataset.create(self);
with adataset.fielddefs do
begin
add(′name′,ftstring,30,false);
add(′ ue′,ftinteger,0,false);
end;
with datasource1 do
begin
dataset:=createtableinmemory(adataset.fielddefs);
dataset.open;
end;
adataset.free;
end;
临时表创建完成。
方法1使用简单,但由于利用查询控件,清空数据时需要查询服务器后台数据库,所以速度稍慢,而且不适用于临时表中各个字段由数个数据表的字段拼凑而成的情况。方法2适用范围广、速度快,但需要编写代码。(代码中tfielddefs的使用方法十分简单,见delphi的联机帮助)。
2.配置数据引擎(bde、sql link)
有关数据库程序分发时,需要携带数据引擎(bde、sql link),并且在客户端安装完程序后还需要配置数据引擎,如用户名(username)、密码(password)等等。如果手工配置的话,工作量比较大(根据客户机数量而定)。而installshield for delphi又好像没有这方面的选项,其实installshield for delphi可以做到,在生成安装程序的目录里有一个*.iwz的文本文件,只要在[idapi alias]片段中手工加入即可。 例如:
[idapi alias]
usesname=sysdba
password=masterkey
安装程序后数据引擎自动配置完毕。
3.在interbase数据库中使用函数
程序员可能在用interbase作为后台数据库时,会为其提供的函数过少而感到不方便(只有四个),无法方便地编写出复杂的存储过程。interbase本身无法编写函数,但它可以使用外部函数(调用dll中的函数)。下例中说明如何在interbase 中声明substr函数。
declare external function substr
cstring(80), smallint, smallint
returns cstring(80)
entry_point ″ib_udf_substr″ module_name ″ib_udf″
其中:module_name为dll的名称,entry_point为函数名。
声明后便可以使用,例如:
select substr(country)
from country
本例使用的是delphi安装时自带的iblocal数据库。用户也可以自己编写函数来扩充interbase。
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器