-------创建数据库应用
时间过的真快,距离上次写Delphi+Apache 开发动态网站已经快一年了,Borland 的Delphi 7都发布快半年了,一直没有时间静下写文章,今天就写一下怎么创建数据库应用,其实网站的数据库和普通的客户端应用差不多,唯一的不同就是那些可视化控件不能再用了,虽然delphi 6 和7里面提供的websnap 功能比较强大,但是在实际应用中定制性太差,可控制的东西复杂,因此本文还是以web broker 为例。
一般的网页都是以上、中、下来设计的,即上面是基本固定的LOGO 和联接部分,中间是内容部分,下面是版权等基本信息,因此我们可以把网页分成三部分,即上、中、下部分,由于上、下部分相对固定,我们就建立两个PageProducer1 元件与之相对应,中间的部分再根据情况动态设定。
我们以delphi 7 和 apache 2.0.43 和 Interbase为例,delphi7 不支持 Apache 2.0.43 ,具体解决办法请看我的文章。首先建立web server 应用程序。如图一,
这样就建立起一个Apache 2.x 的 DSO 模块,放置dbexpress 数据库联接和访问控件和PageProducer 元件,如图二,
并保证 Dbexpress 控件可以正常访问数据库,具体访问方式超出本文章范围,这里不再详细讨论。在Apache 2 的主目录(一般为htdocs目录)建立一下两个文件,title.html,foot.html.
title.html:
<#getalluser>
<BODY topmargin="0" leftmargin="0" >
<table align="center" border="0" width="90%" cellSpacing="1" cellPadding="1" >
<td align="left" width="20%" >
<a href="http://www.51delphi.com"><img src="/image/logo.jpg" border=0></a>
</td>
<td align="left">
<table width="100%" height=20 align="left">
<form method="POST" action="/delphi/login<#wz>" >
<td bgColor="#FEFDED" align="center">
<FONT class="pt9">用户名<input text name="user" class="edit" size=12>
密码<input type=password name="pass" class="edit" size=12 >
<input type =submit value="登陆" class="bt" ></font></td>
<td bgColor="#FEFDED" align="center">
<FONT class="pt9"><a href="/delphi/userreg" target="_blank"> 注册新用户</font>
</td>
<td bgColor="#FEFDED" align="center">
<FONT class="pt9">
<#username>
</font>
</td>
</form>
</table>
</td>
</table>
<table align="center" border="0" width="90%" cellspacing="0" cellpadding="0">
<td align="left" bgcolor="white"><a href="/delphi/fwtj"> <#alluser></a></td>
<td align="center" ><#date></td>
<td align="right" >当前人数:<#curuser></td>
</table>
</td>
</table>
<table align="center" border="0" width="95%" height=24 bgcolor="#FFCF60" cellSpacing="0" cellPadding="0" >
<td width="7" valign="left" bgcolor="#FFCF60"><img src="/topleft.gif" width="7" height="7"></td>
<td valign="bottom" bgcolor="#FFCF60" align="center" class="L17">
<a href="/delphi/" class="title"> 首页 </a><span class="L">|</span>
<a href="/delphi/soft" class="title"> 软件下载 </a><span class="L">|</span>
<a href="/delphi/book" class="title"> 技术资料 </a><font color="#FFFFFF" class="L">|</font>
<a href="/delphi/jswz" class="title"> 技术文章 </a><span class="L">|</span>
<a href="/delphi/link" class="title"> 网络资源 </a><span class="L">|</span>
<a href="/delphi/jstl" class="title"> 技术论坛 </a><span class="L">|</span>
<a href="/delphi/ly" class="title"> 给我留言 </a>
</td>
<td valign="top" align="right" bgcolor="#FFCF60" width="7"><img src="/topright.gif" width="7" height="7"></td>
</table>
foot.html:
<br>
<TABLE align="center" cellSpacing=1 cellPadding=1 width="95%" border=0>
<TBODY>
<TR>
<TD align=center bgcolor="#FFFFFF" colspan="3"><FONT class=pt9 color="#800000">谢谢光临</FONT></TD>
</TR>
<TR bgColor="#FFFFCC">
<TD align=middle ><A
href="/delphi/ly"><font class="pt9" color="#0000FF">给我留言</font></A></TD>
<TD align=middle ><A href="/"><font class="pt9" color="#0000FF">返回主页</font></A></TD>
<TD align=middle ><A href="mailto:xalion@51delphi.com"><font class="pt9" color="#0000FF">给我写信</font></A></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=1 cellPadding=1 width="100%" border=0 height="20">
<TBODY>
<TR>
<TD align=middle height="1"><FONT class="pt9">1999-2001 delphi 窑洞
copyright</FONT></TD></TR>
<TR>
<TD align=middle height="14">窑洞洞主:xalion <FONT class="pt9">合作伙伴:<A
href="mailto:richard@51delphi.com">阿土</A></FONT></TD></TR>
</TBODY></TABLE>
</BODY></HTML>
好,开始我们的程序之旅:
系统常量:
const
chweek:array [1..7]of string[2]=('日','一','二','三','四','五','六');
defpathname:string='d:/Apache Group/Apache2/htdocs';
defsoftpath:string='ftp://202.117.213.5/file/';
defbookpath:string='ftp://202.117.213.5/book/';
gridhead:string=' <table width="100%" border="0" cellspacing="1" cellpadding="1" bgColor="#609000">';
gridheadline:string=' <tr bgcolor="#ffffcc">';
gridline:string=' <tr bgcolor="#fefded">';
gridtdhead:string=' <td ><font class="pt9">';
gridtdheadnowrap:string=' <td nowrap><font class=pt9>';
gridtdend:string=' </font></td>';
webModule 的建立事件:
procedure Twm.WebModuleCreate(Sender: TObject);
var
fconfig:Tinifile;
bbsopen:string;
fname:pchar;
path:string;
begin
getmem(fname,256);
GetModuleFileName( 0,fname,256);
path:=ExtractFilePath(fname);
Freemem(fname);
看是否有设置文件
fconfig:=Tinifile.create(path+'51delphi.ini'); //in folder
pathname:=fconfig.readstring('PATH','HTMLROOT',defpathname);
softpath:=fconfig.readstring('PATH','SOFTPATH',defsoftpath);
bookpath:=fconfig.readstring('PATH','BOOKPATH',defbookpath);
// defpathname);
fconfig.Free;
title.HTMLFile:=pathname+'/title.html';
foot.HTMLFile:=pathname+'/foot.html';
center.HTMLFile:=pathname+'/center.html';
bookpage.HTMLFile:=pathname+'/book.html';
end;
在title 的onhtmltag 事件写:
var
yy,mm,dd:word;
s,img,rc:string;
i:integer;
sip:string;
allnum:integer;
begin
// 加入IP 计数
if tagstring='getalluser' then
begin
sip:=request.RemoteAddr; //浏览器地址
with cx do
begin
sql.Clear;
SQL.Add('select max(id)+1 AS NUM from ip '); //取最大IP 值
OPen;
allnum:=fields[0].asinteger;
sql.Clear;
SQL.Add('select max(iptime) as iptime from ip where ip='''+sip+''''); // 是否访问过本站
OPen;
if Isempty then
//没有访问过本站
begin
SQL.Clear;
SQL.Add('INSERT INTO IP (ID,IP ,IPTIME ,IPTYPE ) VALUES (:1,:2, CURRENT_TIMESTAMP, :3)');
Params[0].AsInteger:=allnum;
Params[1].AsString:=sip;
Params[2].asstring:=request.UserAgent;
try
ExecSQL; //写入访问时间
except
exit;
end;
end
else
begin
//访问过本站
if (now-fields[0].asdatetime)>(1/12) then
//是否两小时内访问过
begin
SQL.Clear;
SQL.Add('INSERT INTO IP (ID,IP ,IPTIME ,IPTYPE ) VALUES (:1,:2,CURRENT_TIMESTAMP, :3)');
Params[0].AsInteger:=allnum;
Params[1].asstring:=sip;
Params[2].asstring:=request.UserAgent;
try
ExecSQL;
except
exit;
end;
end;
end;
end;
ReplaceText:=''; //仅仅是计数,不返回结果
end;
if tagstring='curuser' then //显示当前五分钟内在线用户
begin
with cx do
begin
SQL.Clear;
SQL.Add(' select count(*) as num from IP');
SQL.Add(' where (CURRENT_TIMESTAMP-iptime)<0.005');
Open;
ReplaceText:=fields[0].AsString;
end;
end;
if tagstring='date' then
begin
decodedate(date,yy,mm,dd);
s:='<font color="blue">';
s:=s+'今天是'+inttostr(yy)+'年'+inttostr(mm)+'月'+inttostr(dd)+'日';
s:=s+' 星期'+chweek[dayofweek(date)];
s:=s+'</font>';
ReplaceText:=s;
end;
if tagstring='alluser' then //显示访问人次
begin
s:='';
with cx do
begin
SQL.Clear;
SQL.Add('select * from ALLUSER');
Open;
allnum:=fields[0].AsInteger;
SQL.clear;
sql.Add('select max(ID) from IP');
open;
rc:=inttostr(fields[0].asinteger+allnum);
end;
img:='';
if length(rc)<7 then
begin
for i:=1 to (7-length(rc)) do
rc:='0'+rc;
end;
for i:=1 to length(rc) do
img:=img+'<img src ="/image/'+rc[i]+'.gif" border=0>';
s:=s+img;
ReplaceText:=s;
end;
end;
建立显示技术资料事件()
在webmodule 里面建立一个action ,name为book,pathinfo 为/book.
建立action 事件:
procedure Twm.wmbookAction(Sender: TObject; Request: TWebRequest;
Response: TWebResponse; var Handled: Boolean);
begin
Response.content:=title.Content+bookpage.Content+foot.Content;
end;
建立book.html
book.html:
<title>delphi 窑洞------技术资料</title>
<table align="center" width="95%">
<td>页数:<#bookpage></td>
</table>
<table align="center" cellspacing="2" cellpadding="2" border="0" width="95%" >
<TR align=left vAlign=top>
<TD align=left background="/image/shadow.gif"
width=5> </TD>
<td font class="pt9">
<#showbook>
</td></TR>
</table>
在 web module 里面放置一个PageProdure , name为 book,
在book 的onhtmltag 里面写以下事件:
procedure Twm.bookpageHTMLTag(Sender: TObject; Tag: TTag;
const TagString: String; TagParams: TStrings; var ReplaceText: String);
const
showcount=25;
var
no:integer;
s:string;
where:string;
stype:string;
begin
if request.QueryFields.Values['no']='' then
no:=1
else
begin
s:=request.QueryFields.Values['no'];
try
no:=strtoint(s);
except
no:=1;
end;
if no<1 then no:=1;
end;
if TagString='bookpage' then
begin
with cx do
begin
SQL.Clear;
SQL.Add('select count(*) from BOOK');
SQL.Add(where);
Open;
ReplaceText:=showpage(fields[0].asinteger,showcount,no,'&type='+stype);
end;
end;
if TagString='showbook' then
begin
ReplaceText:=showbook(showcount,no-1,where)
end;
end;
里面用了一个showpage 函数,该函数为
function Twm.showpage(allnum, showcount, no:integer;stype:string): string;
var
lt:integer;
ys:integer;
i,start:integer;
s:string;
begin
start:=(no-1)*showcount;
lt:=allnum mod showcount;
ys:= allnum div showcount;
if lt<>0 then
ys:=ys+1;
s:='';
for i:=1 to ys do
begin
if i<>(start div showcount+1) then
s:=s+' <a href="/delphi'+request.Pathinfo+'?no='+inttostr(i)+stype+'">['+inttostr(i)+']</a>'
else
s:=s+' <font color=red><b>'+inttostr(i)+'</b></font>';
end;
result:=s;
end;
里面用了一个showbook 函数,该函数为
function Twm.showbook(showcount, no: integer;where:string): string;
var
s:string;
wz:integer;
news,jys:string;
begin
with cx do
begin
SQL.Clear;
SQL.Add('select num,name,indate,stype,dnum,jy,fsize from book');
SQL.Add(where);
SQL.Add('order by indate desc');
Open;
s:=gridhead+gridheadline;
s:=s+gridtdhead+'日期'+gridtdend;
s:=s+gridtdhead+'名称'+gridtdend;
s:=s+gridtdhead+'类别'+gridtdend;
s:=s+gridtdhead+'推荐度'+gridtdend;
s:=s+gridtdhead+'大小'+gridtdend;
s:=s+gridtdhead+'下载'+gridtdend;
s:=s+' </tr>';
wz:=0;
while ((wz<showcount) and (not eof)) do
begin
news:='';
if (now-fieldbyname('indate').AsDateTime)<3 then
news:='<img src="/image/new9.gif">';
jys:='<img src="/image/hx.gif"><img src="/image/hx.gif">';
if fieldbyname('jy').AsInteger>=80 then
jys:=jys+'<img src="/image/hx.gif">';
if fieldbyname('jy').AsInteger>=85 then
jys:=jys+'<img src="/image/hx.gif">';
if fieldbyname('jy').AsInteger>=90 then
jys:=jys+'<img src="/image/hx.gif">';
s:=s+gridline;
s:=s+gridtdhead+fieldbyname('indate').asstring+gridtdend;
s:=s+gridtdhead+'<font color="#0000ff">'+news+fieldbyname('name').asstring+'</font>'+gridtdend;
s:=s+gridtdhead+fieldbyname('stype').asstring+gridtdend;
s:=s+gridtdhead+jys+gridtdend;
s:=s+gridtdhead+fieldbyname('FSIZE').asstring+gridtdend;
if getsession('delphiuser')<>'' then
s:=s+gridtdhead+'<font color="blue">'+fieldbyname('dnum').asstring+'</font>'
+'<a href="/delphi/downbook?num='+fieldbyname('num').asstring+ '"><img src="/image/download.gif" border=0> </a> '+gridtdend
else
s:=s+gridtdhead+'<font color="blue">'+fieldbyname('dnum').asstring+'</font>'+gridtdend;
s:=s+' </tr>';
next;
wz:=wz+1;
end;
s:=s+' </table>';
end;
result:=s;
end;
好了,编译该工程,然后加载生成的dll,就可以正确显示数据的内容了。
本篇文章具体介绍了web module 里面的数据库的读写以及显示,希望可以给大家一点帮助,
谢谢大家的阅读!
Xalion
2002-11-27