试用EF开发WEB应用程序(11): 在线留言系统

题记:用“易语言.飞扬”(EF)开发WEB应用程序,此前还没有先例。但因为EF本地开发包(EFNDK)已经发布,用C/C++开发一个EF类库,使其支持EF开发WEB应用程序,应该并非难事。当然也可想而知,其中必有诸多难点有待解决。此系列文章,为本人探索过程之记录,对外人未必有多大价值。如有网友乐观其事,还请理性待之。作者:liigo。转载请务必注明出处:http://blog.csdn.net/liigo/在线留言


试用EF开发WEB应用程序(11):在线留言系统

在线留言系统入口 (非常感谢好友龚辟愚提供网络服务器)

此留言板的“易语言.飞扬”(EF)完整源代码如下:

引入fastcgi,sqlite,工具;

公开类启动类
{
静态Sqlite数据库_db;

公开静态启动()
{
_db
= new Sqlite数据库();
_db.打开(
" ../db/msgboard.file " , true );
if (_db.表是否存在( " messages " ) == false )
createTable();

FCGIfcgi
= new FCGI;
while (fcgi.Accept() >= 0 )
{
if (fcgi.REQUEST_METHOD.到小写() == " post " )
fcgi.ReadContentAsQueryString();

文本html
= html_template.替换全部( " $(title) " , " 留言板(由EF开发) " );

if (fcgi.QUERY_STRING( " name " ) != "" || fcgi.QUERY_STRING( " message " ) != "" )
insertMessage(fcgi);

// deleteallmessages
if (fcgi.QUERY_STRING( " cleandb " ) == " byliigo " )
_db.执行SQL(
" deletefrommessageswhere1=1 " );

stringmessages;
int count;
(messages,count)
= listAllMessages();
html
= html.替换( " $(messages) " ,messages);
html
= html.替换( " $(count) " ,count.到文本());

fcgi.Output(html.到UTF8());
}
_db.关闭();
}

private static createTable()
{
Sqlite表结构tdef
= new Sqlite表结构();
tdef.添加字段(
" id " ,Sqlite字段类型.主键整数);
tdef.添加字段(
" name " ,Sqlite字段类型.文本);
tdef.添加字段(
" message " ,Sqlite字段类型.文本);
tdef.添加字段(
" ip " ,Sqlite字段类型.文本);
tdef.添加字段(
" date " ,Sqlite字段类型.文本);
_db.创建表(
" messages " ,tdef);
}

private static boolinsertMessage(FCGIfcgi)
{
private static stringGetDate()
{
日期时间类t
= new 日期时间类();
return t.年().到文本() + " / " + t.月().到文本() + " / " + t.日().到文本() + " " + t.时().到文本() + " : " + t.分().到文本() + " : " + t.秒().到文本();
}

stringname
= fcgi.QUERY_STRING( " name " );
if (name == "" )name = " EF爱好者 " ;
stringmessage
= fcgi.QUERY_STRING( " message " );
if (message == "" )message = " 他/她很懒,什么都没说:) " ;
stringip
= fcgi.REMOTE_ADDR;
stringdate
= GetDate();

stringsql
= " insertintomessages(name,message,ip,date)values(' " + name + " ',' " + message + " ',' " + ip + " ',' " + date + " ') " ;
return _db.执行SQL(sql);
}

private static string, int listAllMessages()
{
private static stringfixHTMLChars(strings)
{
s
= s.替换全部( " & " , " & " );
s
= s.替换全部( " < " , " &lt; " );
s
= s.替换全部( " > " , " &gt; " );

// someonenotethis?:)
s = s.替换全部( " [b] " , " <b> " );s = s.替换全部( " [/b] " , " </b> " );
s
= s.替换全部( " [red] " , " <fontcolor="red"> " );s = s.替换全部( " [/red] " , " </font> " );
s
= s.替换全部( " [blue] " , " <fontcolor="blue"> " );s = s.替换全部( " [/blue] " , " </font> " );
s
= s.替换全部( " [green] " , " <fontcolor="green"> " );s = s.替换全部( " [/green] " , " </font> " );
return s;
}

private static stringformatMessage(stringname,stringmessage,stringip,stringdate)
{
static stringmsg_template = " <p>$(name)($(ip))于$(date)留言:$(message)</p> " ;
stringmsg
= msg_template.替换( " $(name) " ,name);
msg
= msg.替换( " $(ip) " ,ip);
msg
= msg.替换( " $(message) " ,fixHTMLChars(message));
msg
= msg.替换( " $(date) " ,date);
return msg;
}

Sqlite记录集query
= new Sqlite记录集(_db, " select*frommessagesorderby_ROWID_desc " );
stringmessages
= "" ;
int count = 0 ;
if (query.打开())
{
count
= query.取记录数();
while (query.到下一记录())
{
messages
+= formatMessage(query.读字段值( " name " ,Sqlite字段类型.文本),query.读字段值( " message " ,Sqlite字段类型.文本),query.读字段值( " ip " ,Sqlite字段类型.文本),query.读字段值( " date " ,Sqlite字段类型.文本));
}
}
return (messages,count);
}

常量文本html_template
= [ " Content-type:text/html

< html >< head >
< metahttp - equiv = " content-type " content = " text/html;charset=utf-8 " >
< title > $(title) </ title >
</ head >
< body >
< h1 > $(title) </ h1 >
< hr ></ hr >

< formmethod = " post " >
< p > 姓名: < inputtype = " text " name = " name " size = 20 /></ p >
< p > 留言: < inputtype = " text " name = " message " size = 100 /></ p >

< p >< inputtype = " submit " value = " 提交留言 " ></ input ></ p >
</ form >

< p >< ahref = " msgboard.txt " target = " _blank " > 此留言板的EF源代码 </ a ></ p >

< hr ></ hr >
< h1 > 所有留言(共$(count)条) </ h1 >

$(messages)

< hr ></ hr >
< p > byliigo, < ahref = " http://blog.csdn.net/liigo/ " > http://blog.csdn.net/liigo/</a></p>
</ body >
</ html >
" ];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值