四、数据库操作
1. 嵌入式SQL语句
PB脚本中可以直接使用嵌入式SQL与DM通信,PB中的嵌入式SQL语句分为静态语句和动态语句,一般情况下可使用静态的sql,动态的sql一般在静态的sql无法满足的情况下或需要更多的灵活性的情况,考虑使用,如数据定义,表名作为变量传递时,或者条件列名不确定等情况。
例如程序开启阶段添加代码,需要完成如果不存在pbdm这张表即建立该表,可用以下动态SQL实现:
int ls_tag,i;
select count(*) into :ls_tag from sysdba.systables where name = 'pbdm';
if ls_tag = 0 then
execute immediate 'create table pbdm (a bigint,b binary,c bit,d blob,e char,f clob,g date,h decimal,i double,j float,k integer,l smallint,m time,n timestamp,o tinyint,p varbinary,q varchar)';
end if
select count(*) into :ls_tag from sysdba.systables where name = 'pbdm';
if ls_tag = 0 then
execute immediate 'create table pbdm (a bigint,b binary,c bit,d blob,e char,f clob,g date,h decimal,i double,j float,k integer,l smallint,m time,n timestamp,o tinyint,p varbinary,q varchar)';
end if
需要给该表添加100条记录,使用静态sql,例如:
int temp
select max(a) into :temp from pbdm;
if isnull(temp) then temp=0;
temp=temp+1;
long i
for i = temp to temp+100
insert into pbdm values (:i,'a',0,'sgsdg','w','sgsdg',sysdate,34634,34634,34634,34634,3464,curtime,curtime,34,'a','2');
st_2.text=string(i)
next
commit;
select max(a) into :temp from pbdm;
if isnull(temp) then temp=0;
temp=temp+1;
long i
for i = temp to temp+100
insert into pbdm values (:i,'a',0,'sgsdg','w','sgsdg',sysdate,34634,34634,34634,34634,3464,curtime,curtime,34,'a','2');
st_2.text=string(i)
next
commit;
2.数据窗口和数据存储
数据窗口(DataWindow)是PowerBuilder提供给开发人员快速建立基于数据库应用程序的最强有力的工具,也是PowerBuilder与其他面向对象的数据库应用前端开发工具的最主要的区别。它的本质是以自动化的用户/数据库接口为开发人员最大限度地节省时间和精力,并且这种自动化特性并不限制开发人员的主观能动性,开发者能以他所钟爱的方式来应用数据窗口,开发出高质量的应用程序。精通PB者就必定精通数据窗口技术。
数据存储(DataStore)可以看作数据窗口的不可视版,除了不可见,其他功能和数据窗口完全一样,为提高效率使用。
因DM程序员手册已有相关简单介绍,且数据窗口内容相当琐碎,这里不再作更深入研究。
3.PB中的SQL提交方式
在pb中,可以使用sqlca.autocommit = true将自动提交开启,或sqlca.autocommit = False关闭自动提交,需要注意的是,DDL语句为隐性自动提交,不可rollback。