试用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 >
" ];
}

你可能感兴趣的:(sql,应用服务器,Web,sqlite,网络应用)