选择显示字体大小

使用简单的select就可以实现文本的索引访问


use lib "."; # if nt,use lib "path-to-jtdb_directory";
use jtdb "1.01";
$main::split = ","; # notice!, it's necessary! must be $main::split,
# records split by ","
my &#36;db = &quot;<path-to>/dbname&quot;;
@main::recordnames = &db_connect(&#36;db); # necessary! must be @main::recordnames,
# get recordnames from db-info file
my &#36;sqlstr = &quot;select * from &#36;db&quot;;
my @resoult = &executestr(&#36;sqlstr);
my &#36;line;
foreach &#36;line (@resoult)
{
my &#36;keys;
foreach &#36;keys (keys %&#36;line)
{
print &#36;keys.&quot; : &quot;.&#36;line->{&#36;keys}.&quot; &quot;;
}
print &quot;<br>\n&quot;;
}

---------------------------

用这样简单的方式操作文本数据,其实也不是难事儿,看看这个模块吧。。

http://ub4k91.chinaw3.com/download/jtdb.htm

jtdb v1.01

#-------------------------------------------------------------------
package jtdb;

# ----------------------------------------------------------------------
# 程序名称:平面文本sql查询模块,jtdb v1.01
#
# 作者:阿恩 (aren.liu) / 成都金想网络技术有限公司
#
# 电话:028-4290153
#
# 传呼:96968-223046
#
# 一妹:boyaren@sina.com
#
# 主叶:http://www.justake.com http://jtbbs.nt.souying.com
#
# -----------------------------------------------------------------------
# 版权所有 成都金想网络技术有限公司 来趣山庄
# copyright (c) 2000 justake.com, jinxiang co.,ltd. all rights reserved
# -----------------------------------------------------------------------
# v 1.01 2000/12/27
# 实现 create_db功能
# v 1.00 2000/12/26
# 设想并实现平面文本数据库sql查询最基本功能
# 可实现 select,insert,delete,update 基本功能
# ------------------------------------------- 请保留以上版权 ------------

require 5.002;

use strict;
use vars qw(@isa @export &#36;version);
use exporter;

&#36;version = '1.01';
&#36;main::txt = &quot;.txt&quot;;

@isa = qw(exporter);

@export = qw
(
&db_connect
&create_db
&executestr
&readtxtfile
&writetxtfile
);
#------------------------------------------------
sub create_db
{
my (&#36;jtdb,&#36;recordnames) = @_;

my &#36;jtdb_info = &#36;jtdb.&quot;_info&quot;.&#36;main::txt;
my &#36;dbname = &#36;jtdb.&#36;main::txt;

?ify(&quot;数据库已经存在,请选择其他数据库数据库创建失败!&quot;,1) if (-e &#36;dbname);

open (jtdb,&quot;>&#36;dbname&quot;);
close(jtdb);

open (jtdbinfo,&quot;>&#36;jtdb_info&quot;);
print jtdbinfo &#36;recordnames.&quot;\n&quot;;
close(jtdbinfo);

return (1);
}
#------------------------------------------------
sub db_connect
{
#my &#36;dbname = substr(&#36;_[0],0,length(&#36;_[0])-4);
my &#36;dbname = &#36;_[0];
?ify(&quot;不能找到数据库信息文件,数据库连接失败!&quot;,1) if (!(-e &#36;dbname.&quot;_info&quot;.&#36;main::txt));
my @jtdb_info = &readtxtfile(&#36;dbname.&quot;_info&quot;.&#36;main::txt);
chomp(@jtdb_info);
?ify(&quot;数据库信息文件已经损坏或丢失,连接数据库失败!&quot;,1) if (&#36;jtdb_info[0] eq &quot;&quot;);

my @keys = split(/&#36;main::split/,&#36;jtdb_info[0]);
my &#36;key;
foreach &#36;key (@keys)
{
&#36;key =~ s/^\s+//g;
&#36;key =~ s/\s+&#36;//g;
}
return @keys;
}
#------------------------------------------------
sub db_save
{
my (&#36;jtdb,@tosave) = @_;

my &#36;dbname = &#36;jtdb.&#36;main::txt;
my &#36;just = &#36;jtdb.&quot;.lock&quot;;

while(-f &#36;just)
{select(undef,undef,undef,0.1);} #锁文件
open(lockfile,&quot;>&#36;just&quot;);

open (fd,&quot;>&#36;dbname&quot;);
my &#36;line;
foreach &#36;line (@tosave)
{
foreach (@main::recordnames)
{
print fd &#36;line->{&#36;_}.&#36;main::split;
}
print fd &quot;\n&quot;;
}
close(fd);

close(lockfile);
unlink(&#36;just);
return (1);
}
#------------------------------------------------
sub executestr
{
my @sqlcmds;
my &#36;sqlcmd;

grep{/\s*(\s+)\s+(.*)/ and &#36;sqlcmd = lc(&#36;1);} @_;

if (&#36;sqlcmd eq &quot;select&quot;)
{
grep{/\s*(select)\s+(\s+\s*(\s*\,+?\s*\s+)*)\s+from\s+(\s+)((\s+where\s+(.*)\s*)*)/i and &#36;sqlcmd = lc(&#36;1);@sqlcmds = (&#36;2,&#36;4,&#36;7);} @_;
&sql_select(@sqlcmds);
}
elsif (&#36;sqlcmd eq &quot;insert&quot;)
{
grep{/\s*(insert)\s+into\s+(\s+)((\s+\((\s*\s+\s*(\s*\,+?\s*\s+)*\s*)+?\))*?)\s+values\s*\((.*)\)\s*/i and &#36;sqlcmd = lc(&#36;1);@sqlcmds = (&#36;2,&#36;5,&#36;7);} @_;
&sql_insert(@sqlcmds);
}
elsif (&#36;sqlcmd eq &quot;delete&quot;)
{
grep{/\s*(delete)\s+from\s+(\s+)\s+where\s+(.*)\s*/i and &#36;sqlcmd = lc(&#36;1);@sqlcmds = (&#36;2,&#36;3);} @_;
&sql_delete(@sqlcmds);
}
elsif (&#36;sqlcmd eq &quot;update&quot;)
{
grep{/\s*(update)\s+(\s+)\s+set\s+(.*)\s+where\s+(.*)\s*/i and &#36;sqlcmd = lc(&#36;1);@sqlcmds = (&#36;2,&#36;3,&#36;4);} @_;
&sql_update(@sqlcmds);
}
else
{?ify(&quot;你输入的数据库操作语句不正确,或目前的版本尚未支持,请检查!&quot;);}
}
#------------------------------------------------
sub sql_update
{
my (&#36;jtdb,&#36;set,&#36;where) = @_;

my @resoult = &executestr(&quot;select * from &#36;jtdb&quot;);

if (&#36;where ne &quot;&quot;)
{
my &#36;key = '';
foreach &#36;key (@main::recordnames)
{
&#36;where =~ s/&#36;key/\&#36;_->{'&#36;key'}/ig;
}
}else {?ify(&quot;你没有提供修改条件,请用 where 语句提供!&quot;);}

if (&#36;set ne &quot;&quot;)
{
my &#36;key = '';
foreach &#36;key (@main::recordnames)
{
&#36;set =~ s/&#36;key\s*\=\s*(\'+?\&quot;+?)(.*)(\'+?\&quot;+?)\s*(\,*?)/\&#36;_->{'&#36;key'}\=&#36;1&#36;2&#36;3\;/ig;
}
}else {?ify(&quot;你没有提供修改项目,请用 set 语句提供!&quot;);}

foreach (@resoult)
{
if (eval(&#36;where))
{
eval(&#36;set);
}
}

&db_save(&#36;jtdb,@resoult);

return (1);
}
#------------------------------------------------
sub sql_delete
{
my (&#36;jtdb,&#36;where) = @_;

my @resoult = &executestr(&quot;select * from &#36;jtdb&quot;);

if (&#36;where ne &quot;&quot;)
{
my &#36;key = '';
foreach &#36;key (@main::recordnames)
{
&#36;where =~ s/&#36;key/\&#36;_->{'&#36;key'}/ig;
}
}else {?ify(&quot;你没有提供删除条件,请用 where 语句提供!&quot;);}

my @return = grep(eval(&#36;where)==0,@resoult);

&db_save(&#36;jtdb,@return);

#my &#36;just = &#36;jtdb.&quot;.lock&quot;;

#while(-f &#36;just)
#{select(undef,undef,undef,0.1);} #锁文件
#open(lockfile,&quot;>&#36;just&quot;);

#open (fd,&quot;>&#36;jtdb&quot;);
#my &#36;line;
#foreach &#36;line (@return)
#{
# foreach (@main::recordnames)
# {
# print fd &#36;line->{&#36;_}.&#36;main::split;
# }
# print fd &quot;\n&quot;;
#}
#close(fd);

#close(lockfile);
#unlink(&#36;just);

return (1);
}
#------------------------------------------------
sub sql_insert
{
my (&#36;jtdb,&#36;keys,&#36;values) = @_;

?ify(&quot;找不到要操作的数据库,操作失败!&quot;) if (!(-e &#36;jtdb));

my @values = split(/\,/,&#36;values);
my &#36;addline;
if (&#36;keys ne &quot;&quot;)
{
#my @main::recordnames = split(/&#36;main::split/,&#36;main::recordnames);
my @keys = split(/\,/,&#36;keys);
my &#36;i;
my @addline;
for (&#36;i=0;&#36;i<@main::recordnames ;&#36;i++)
{
my &#36;n;
for (&#36;n=0;&#36;n<@keys;&#36;n++)
{
if (&#36;keys[&#36;n] eq &#36;main::recordnames[&#36;i])
{
&#36;addline[&#36;i] = &#36;values[&#36;n];
last;
}
}
}
&#36;addline = join(&#36;main::split,@addline);
}
else
{
?ify(&quot;你输入的语句有错误!如果不指定插入字段,values 值必须和数据库字段相对应,并且数量相等。&quot;) if(@values != @main::recordnames);
&#36;addline = join(&#36;main::split,@values);
}
&writetxtfile(&#36;jtdb,&#36;addline.&#36;main::split.&quot;\n&quot;);
return (1);
}
#------------------------------------------------
sub sql_select
{
my (&#36;select,&#36;from,&#36;where) = @_;

if (&#36;where ne &quot;&quot;)
{
#my @keys = split(/&#36;main::split/,&#36;main::recordnames);
my &#36;key = '';
foreach &#36;key (@main::recordnames)
{
#&#36;key =~ s/^\s+//g;
#&#36;key =~ s/\s+&#36;//g;
&#36;where =~ s/&#36;key/\&#36;record->{'&#36;key'}/ig;
}
}else {&#36;where = 1}

my &#36;dbinfo = &dbhoh(&#36;from);

my (&#36;key,&#36;record,&#36;recordname,&#36;return)=('','','',[]);
foreach &#36;key (keys %&#36;dbinfo)
{
my &#36;record = &#36;dbinfo->{&#36;key};
my @select = split(/\,/,&#36;select);
@select = @main::recordnames if (&#36;select =~ /\s*\*\s*/);

my &#36;linehash = {};
foreach &#36;recordname (@select)
{
&#36;recordname =~ s/^\s+//g;
&#36;recordname =~ s/\s+&#36;//g;

&#36;linehash->{&#36;recordname} = &#36;record->{&#36;recordname} if (eval(&#36;where));
}
push(@&#36;return, &#36;linehash);
}
return @&#36;return; #返回查询结果,存储在 &#36;return 中,array of array
}
#------------------------------------------------
sub dbhoh #得到数据结构 hash of hash
{
my &#36;jtdb = &#36;_[0].&#36;main::txt;
my @database = &readtxtfile(&#36;jtdb);
chomp(@database);
#my &#36;main::recordnames = shift(@database); #get @col_names at the first line of txt_db,shift it
#my &#36;keys = &getkeys(&#36;main::recordnames);
my &#36;keys = &getkeys(@main::recordnames);
my (&#36;line,&#36;return) = ('',{});
foreach &#36;line (@database)
{
my &#36;keyshash = &getref(&#36;line,&#36;keys);
&#36;return->{&#36;keyshash->{id}} = &#36;keyshash;
}
return &#36;return;
}
#------------------------------------------------
sub getkeys #得到关键字,book<perl 5 complete>(中文) page(226)
{
#my &#36;line = &#36;_[0];
#my @keys = split(/&#36;main::split/,&#36;line);
my @keys = @_;
my (&#36;key,&#36;return,&#36;i) = ('',{},0);
foreach &#36;key (@keys)
{
#&#36;key =~ s/^\s+//g;
#&#36;key =~ s/\s+&#36;//g;
&#36;return->{&#36;i++} = &#36;key;
}
return &#36;return;
}
#------------------------------------------------
sub getref #得到关键字对应元素,book<perl 5 complete>(中文) page(227)
{
my (&#36;line,&#36;keys) = @_;
my (&#36;element,@elements) = @_;
my &#36;return = {};
my &#36;i;
@elements = split(/&#36;main::split/,&#36;line);
for (&#36;i=0;&#36;i<@elements ;&#36;i++)
{
&#36;element = &#36;elements[&#36;i];
&#36;element =~ s/^\s+//g;
&#36;element =~ s/\s+&#36;//g;
&#36;return->{&#36;keys->{&#36;i}}=&#36;element;
}
return &#36;return;
}
#------------------------------------------------
sub readtxtfile
{
my &#36;just = &#36;_[0].&quot;.lock&quot;;

while(-f &#36;just)
{select(undef,undef,undef,0.1);}
open(lockfile,&quot;>&#36;just&quot;);

open(readtxtfile,&quot;&#36;_[0]&quot;);
my @readtxtfile=<readtxtfile>;
close(readtxtfile);

close(lockfile);
unlink(&#36;just);

return @readtxtfile;
}
#------------------------------------------------
sub writetxtfile
{
my &#36;just = &#36;_[0].&quot;.lock&quot;;

while(-f &#36;just)
{select(undef,undef,undef,0.1);}
open(lockfile,&quot;>&#36;just&quot;);

if (&#36;_[2] == 1)
{open (writetxtfile,&quot;>&#36;_[0]&quot;);}
else{open (writetxtfile,&quot;>>&#36;_[0]&quot;);}
print writetxtfile &#36;_[1];
close(writetxtfile);

close(lockfile);
unlink(&#36;just);

return(1);
}
#------------------------------------------------
sub notify
{
use cgi;
my &#36;query = new cgi;
print &#36;query->header() if (&#36;_[1] == 1);
print &#36;_[0];
exit;
}
#------------------------------------------------

1;

__end__

=head1 name

jtdb -- a modules of control a txt-database width sql-words

=head1 synopsis

use lib &quot;.&quot;; # if nt,use lib &quot;path-to-jtdb_directory&quot;;
use jtdb &quot;1.01&quot;;

&#36;main::split = &quot;,&quot;; # notice!, it's necessary! must be &#36;main::split,
# records split by &quot;,&quot;

my &#36;db = &quot;<path-to>/dbname&quot;;

@main::recordnames = &db_connect(&#36;db); # necessary! must be @main::recordnames,
# get recordnames from db-info file

my &#36;sqlstr = &quot;select * from &#36;db&quot;;
my @resoult = &executestr(&#36;sqlstr);

my &#36;line;
foreach &#36;line (@resoult)
{
my &#36;keys;
foreach &#36;keys (keys %&#36;line)
{
print &#36;keys.&quot; : &quot;.&#36;line->{&#36;keys}.&quot; &quot;;
}
print &quot;<br>\n&quot;;
}

=head1 description

this modules, jtdb.pm, is a tool of control txt-database width sql-words.
for now,only select,insert,delete,update can be used in this script,and it's
very simple.

it is only opening-words, and i think some one will make it fullness and
mightiness one day! so,you can modify it at will! and i hope you tell us
the headway of this modules and share it width everybody. at last, i hope
you do not remove my copyright,if u will...

enjoy it!

=item db_connect

open dbname_info.txt and get @recordnames

=item executestr

execute sql-script,and return a array of array

my @resoult = &executestr(&#36;sqlstr);

my &#36;line;
foreach &#36;line (@resoult)
{
print &#36;line->{'id'}.&quot;\n&quot;;
print &#36;line->{'name'}.&quot;\n&quot;;
}

=item create_db

usage:

my &#36;ids = &quot;id,name,pass,lover&quot;; # now,&#36;main::split = &quot;,&quot;

# if &#36;ids = &quot;idnamepasslover&quot; then &#36;main::split = &quot;&quot;
my &#36;dbname = &quot;jtdatabase&quot;;
create_db(&quot;<path-to>/&quot;.&#36;dbname,&#36;ids);

# then,<path-to>/jtdatabase.txt and <path-to>/jtdatabase_info.txt has been
# created !

=head2 sql-string

select id,name from &#36;db where id>6
select * from from &#36;db where name=~ m&quot;aren&quot;i and email ne &quot;&quot;

notices: at the block of where ,u can use a-short-perl-code !!
--------------------------------------------------------------

insert into &#36;db (id,name) values(2009,aren)
insert into &#36;db values ( 2009,aren,12345,mylover)

notices: do not use ' or &quot; at values-list

insert into &#36;db values ( '2009','aren','12345','mylover')
will set id=&quot;'2009'&quot; and name=&quot;'aren'&quot; and ...
--------------------------------------------------------------

delete from &#36;db where id =~ /j/
--------------------------------------------------------------

update &#36;db set name='jack',pass=\&quot;123\&quot;,lover='jack\&quot;lover' where id = 3

=head1 bugs


author aren <boyaren@sina.com> http://www.justake.com

=cut

  


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

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   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons