返回列表 回复 发帖

尝试用sql查询语句操纵普通文本数据库

作者:Aren.Liu
日期:2000-12-27 9:00:24

尝试用sql查询语句操纵普通文本数据库!使用简单的select就可以实现文本的索引访问,用update……
  1. use lib "."; # If NT,use lib "path-to-jtdb_directory";
  2. use JTDB "1.01";
  3. $main::split = ","; # Notice!, It';s necessary! must be $main::split,
  4. # Records split by ","
  5. my &#36;db = "<path-to>/dbname";
  6. @main::recordNames = &db_connect(&#36;db); &#35; Necessary! must be @main::recordNames,
  7. &#35; Get RecordNames from db-info file
  8. my &#36;sqlStr = "SELECT * FROM &#36;db";
  9. my @resoult = &executeStr(&#36;sqlStr);
  10. my &#36;line;
  11. foreach &#36;line (@resoult)
  12. {
  13. my &#36;keys;
  14. foreach &#36;keys (keys %&#36;line)
  15. {
  16. print &#36;keys." : ".&#36;line->{&#36;keys}." ";
  17. }
  18. print "<br>\n";
  19. }
复制代码
---------------------------
用这样简单的方式操作文本数据,其实也不是难事儿,看看这个模块吧。。

http://ub4k91.chinaw3.com/download/jtdb.htm
JTDB v1.01
  1. &#35;-------------------------------------------------------------------
  2. package JTDB;
  3. &#35; ----------------------------------------------------------------------
  4. &#35; 程序名称:平面文本SQL查询模块,JTDB V1.01
  5. &#35;
  6. &#35; 作者:阿恩 (Aren.Liu) / 成都金想网络技术有限公司
  7. &#35;
  8. &#35; 电话:028-4290153
  9. &#35;
  10. &#35; 传呼:96968-223046
  11. &#35;
  12. &#35; 一妹:boyaren@sina.com
  13. &#35;
  14. &#35; 主叶:http://www.justake.com http://jtbbs.nt.souying.com
  15. &#35;
  16. &#35; -----------------------------------------------------------------------
  17. &#35; 版权所有 成都金想网络技术有限公司 来趣山庄
  18. &#35; Copyright (C) 2000 Justake.com, JinXiang Co.,Ltd. All Rights Reserved
  19. &#35; -----------------------------------------------------------------------
  20. &#35; V 1.01 2000/12/27
  21. &#35; 实现 create_db功能
  22. &#35; V 1.00 2000/12/26
  23. &#35; 设想并实现平面文本数据库SQL查询最基本功能
  24. &#35; 可实现 select,insert,delete,update 基本功能
  25. &#35; ------------------------------------------- 请保留以上版权 ------------
  26. require 5.002;
  27. use strict;
  28. use vars qw(@ISA @EXPORT &#36;VERSION);
  29. use Exporter;
  30. &#36;VERSION = ';1.01';;
  31. &#36;main::txt = ".txt";
  32. @ISA = qw(Exporter);
  33. @EXPORT = qw
  34. (
  35. &db_connect
  36. &create_db
  37. &executeStr
  38. &readtxtfile
  39. &writetxtfile
  40. );
  41. &#35;------------------------------------------------
  42. sub create_db
  43. {
  44. my (&#36;jtdb,&#36;recordNames) = @_;
  45. my &#36;jtdb_info = &#36;jtdb."_info".&#36;main::txt;
  46. my &#36;dbname = &#36;jtdb.&#36;main::txt;
  47. &notify("数据库已经存在,请选择其他数据库,数据库创建失败!",1) if (-e &#36;dbname);
  48. open (JTDB,">&#36;dbname");
  49. close(JTDB);
  50. open (JTDBINFO,">&#36;jtdb_info");
  51. print JTDBINFO &#36;recordNames."\n";
  52. close(JTDBINFO);
  53. return (1);
  54. }
  55. &#35;------------------------------------------------
  56. sub db_connect
  57. {
  58. &#35;my &#36;dbname = substr(&#36;_[0],0,length(&#36;_[0])-4);
  59. my &#36;dbname = &#36;_[0];
  60. &notify("不能找到数据库信息文件,数据库连接失败!",1) if (!(-e &#36;dbname."_info".&#36;main::txt));
  61. my @jtdb_info = &readtxtfile(&#36;dbname."_info".&#36;main::txt);
  62. chomp(@jtdb_info);
  63. &notify("数据库信息文件已经损坏或丢失,连接数据库失败!",1) if (&#36;jtdb_info[0] eq "");
  64. my @keys = split(/&#36;main::split/,&#36;jtdb_info[0]);
  65. my &#36;key;
  66. foreach &#36;key (@keys)
  67. {
  68. &#36;key =~ s/^\s+//g;
  69. &#36;key =~ s/\s+&#36;//g;
  70. }
  71. return @keys;
  72. }
  73. &#35;------------------------------------------------
  74. sub db_save
  75. {
  76. my (&#36;jtdb,@toSave) = @_;
  77. my &#36;dbname = &#36;jtdb.&#36;main::txt;
  78. my &#36;just = &#36;jtdb.".lock";
  79. while(-f &#36;just)
  80. {select(undef,undef,undef,0.1);} &#35;锁文件
  81. open(LOCKFILE,">&#36;just");
  82. open (FD,">&#36;dbname");
  83. my &#36;line;
  84. foreach &#36;line (@toSave)
  85. {
  86. foreach (@main::recordNames)
  87. {
  88. print FD &#36;line->{&#36;_}.&#36;main::split;
  89. }
  90. print FD "\n";
  91. }
  92. close(FD);
  93. close(LOCKFILE);
  94. unlink(&#36;just);
  95. return (1);
  96. }
  97. &#35;------------------------------------------------
  98. sub executeStr
  99. {
  100. my @sqlcmds;
  101. my &#36;sqlcmd;
  102. grep{/\s*(\S+)\s+(.*)/ and &#36;sqlcmd = lc(&#36;1);} @_;
  103. if (&#36;sqlcmd eq "select")
  104. {
  105. 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);} @_;
  106. &sql_select(@sqlcmds);
  107. }
  108. elsif (&#36;sqlcmd eq "insert")
  109. {
  110. 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);} @_;
  111. &sql_insert(@sqlcmds);
  112. }
  113. elsif (&#36;sqlcmd eq "delete")
  114. {
  115. grep{/\s*(DELETE)\s+FROM\s+(\S+)\s+WHERE\s+(.*)\s*/i and &#36;sqlcmd = lc(&#36;1);@sqlcmds = (&#36;2,&#36;3);} @_;
  116. &sql_delete(@sqlcmds);
  117.   }
  118.   elsif (&#36;sqlcmd eq "update")
  119.   {
  120. 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);} @_;
  121. &sql_update(@sqlcmds);
  122.    }
  123.   else
  124.   {&notify("你输入的数据库操作语句不正确,或目前的版本尚未支持,请检查!");}
  125. }
  126. &#35;------------------------------------------------
  127. sub sql_update
  128. {
  129. my (&#36;jtdb,&#36;set,&#36;where) = @_;
  130. my @resoult = &executeStr("SELECT * FROM &#36;jtdb");
  131. if (&#36;where ne "")
  132. {
  133. my &#36;key = ';';;
  134. foreach &#36;key (@main::recordNames)
  135. {
  136. &#36;where =~ s/&#36;key/\&#36;_->{';&#36;key';}/ig;
  137. }
  138. }else {&notify("你没有提供修改条件,请用 WHERE 语句提供!");}
  139. if (&#36;set ne "")
  140. {
  141. my &#36;key = ';';;
  142. foreach &#36;key (@main::recordNames)
  143. {
  144. &#36;set =~ s/&#36;key\s*\=\s*(\';+?|\"+?)(.*)(\';+?|\"+?)\s*(\,*?)/\&#36;_->{';&#36;key';}\=&#36;1&#36;2&#36;3\;/ig;
  145. }
  146. }else {&notify("你没有提供修改项目,请用 SET 语句提供!");}
  147. foreach (@resoult)
  148. {
  149. if (ev&#97;l(&#36;where))
  150. {
  151. ev&#97;l(&#36;set);
  152. }
  153. }
  154. &db_save(&#36;jtdb,@resoult);
  155. return (1);
  156. }
  157. &#35;------------------------------------------------
  158. sub sql_delete
  159. {
  160. my (&#36;jtdb,&#36;where) = @_;
  161. my @resoult = &executeStr("SELECT * FROM &#36;jtdb");
  162. if (&#36;where ne "")
  163. {
  164. my &#36;key = ';';;
  165. foreach &#36;key (@main::recordNames)
  166. {
  167. &#36;where =~ s/&#36;key/\&#36;_->{';&#36;key';}/ig;
  168. }
  169. }else {&notify("你没有提供删除条件,请用 WHERE 语句提供!");}
  170. my @return = grep(ev&#97;l(&#36;where)==0,@resoult);
  171. &db_save(&#36;jtdb,@return);
  172. &#35;my &#36;just = &#36;jtdb.".lock";
  173. &#35;while(-f &#36;just)
  174. &#35;{select(undef,undef,undef,0.1);} &#35;锁文件
  175. &#35;open(LOCKFILE,">&#36;just");
  176. &#35;open (FD,">&#36;jtdb");
  177. &#35;my &#36;line;
  178. &#35;foreach &#36;line (@return)
  179. &#35;{
  180. &#35;foreach (@main::recordNames)
  181. &#35;{
  182. &#35;print FD &#36;line->{&#36;_}.&#36;main::split;
  183. &#35; }
  184. &#35; print FD "\n";
  185. &#35;}
  186. &#35;close(FD);
  187. &#35;close(LOCKFILE);
  188. &#35;unlink(&#36;just);
  189. return (1);
  190. }
  191. &#35;------------------------------------------------
  192. sub sql_insert
  193. {
  194. my (&#36;jtdb,&#36;keys,&#36;values) = @_;
  195. &notify("找不到要操作的数据库,操作失败!") if (!(-e &#36;jtdb));
  196. my @values = split(/\,/,&#36;values);
  197. my &#36;addLine;
  198. if (&#36;keys ne "")
  199. {
  200. &#35;my @main::recordNames = split(/&#36;main::split/,&#36;main::recordNames);
  201. my @keys = split(/\,/,&#36;keys);
  202. my &#36;i;
  203. my @addLine;
  204. for (&#36;i=0;&#36;i<@main::recordNames ;&#36;i++)
  205. {
  206. my &#36;n;
  207. for (&#36;n=0;&#36;n<@keys;&#36;n++)
  208. {
  209. if (&#36;keys[&#36;n] eq &#36;main::recordNames[&#36;i])
  210. {
  211. &#36;addLine[&#36;i] = &#36;values[&#36;n];
  212. last;
  213. }
  214. }
  215. }
  216. &#36;addLine = join(&#36;main::split,@addLine);
  217. }
  218. else
  219. {
  220. &notify("你输入的语句有错误!如果不指定插入字段,VALUES 值必须和数据库字段相对应,并且数量相等。") if(@values != @main::recordNames);
  221. &#36;addLine = join(&#36;main::split,@values);
  222.   }
  223. &writetxtfile(&#36;jtdb,&#36;addLine.&#36;main::split."\n");
  224. return (1);
  225. }
  226. &#35;------------------------------------------------
  227. sub sql_select
  228. {
  229. my (&#36;select,&#36;from,&#36;where) = @_;
  230. if (&#36;where ne "")
  231. {
  232. &#35;my @keys = split(/&#36;main::split/,&#36;main::recordNames);
  233. my &#36;key = ';';;
  234. foreach &#36;key (@main::recordNames)
  235. {
  236. &#35;&#36;key =~ s/^\s+//g;
  237. &#35;&#36;key =~ s/\s+&#36;//g;
  238. &#36;where =~ s/&#36;key/\&#36;record->{';&#36;key';}/ig;
  239. }
  240. }else {&#36;where = 1}
  241. my &#36;dbinfo = &dbHoH(&#36;from);
  242. my (&#36;key,&#36;record,&#36;recordName,&#36;return)=(';';,';';,';';,[]);
  243. foreach &#36;key (keys %&#36;dbinfo)
  244. {
  245. my &#36;record = &#36;dbinfo->{&#36;key};
  246. my @select = split(/\,/,&#36;select);
  247. @select = @main::recordNames if (&#36;select =~ /\s*\*\s*/);
  248. my &#36;lineHash = {};
  249. foreach &#36;recordName (@select)
  250. {
  251. &#36;recordName =~ s/^\s+//g;
  252. &#36;recordName =~ s/\s+&#36;//g;
  253. &#36;lineHash->{&#36;recordName} = &#36;record->{&#36;recordName} if (ev&#97;l(&#36;where));
  254. }
  255. push(@&#36;return, &#36;lineHash);
  256. }
  257. return @&#36;return; &#35;返回查询结果,存储在 &#36;return 中,Array of Array
  258. }
  259. &#35;------------------------------------------------
  260. sub dbHoH &#35;得到数据结构 Hash of Hash
  261. {
  262. my &#36;jtdb = &#36;_[0].&#36;main::txt;
  263. my @database = &readtxtfile(&#36;jtdb);
  264. chomp(@database);
  265. &#35;my &#36;main::recordNames = shift(@database); &#35;get @col_names at the first line of txt_db,shift it
  266. &#35;my &#36;keys = &getKeys(&#36;main::recordNames);
  267. my &#36;keys = &getKeys(@main::recordNames);
  268. my (&#36;line,&#36;return) = (';';,{});
  269. foreach &#36;line (@database)
  270. {
  271. my &#36;keysHash = &getRef(&#36;line,&#36;keys);
  272. &#36;return->{&#36;keysHash->{id}} = &#36;keysHash;
  273. }
  274. return &#36;return;
  275. }
  276. &#35;------------------------------------------------
  277. sub getKeys &#35;得到关键字,BOOK<Perl 5 Complete>(中文) page(226)
  278. {
  279. &#35;my &#36;line = &#36;_[0];
  280. &#35;my @keys = split(/&#36;main::split/,&#36;line);
  281. my @keys = @_;
  282. my (&#36;key,&#36;return,&#36;i) = (';';,{},0);
  283. foreach &#36;key (@keys)
  284. {
  285. &#35;&#36;key =~ s/^\s+//g;
  286. &#35;&#36;key =~ s/\s+&#36;//g;
  287. &#36;return->{&#36;i++} = &#36;key;
  288. }
  289. return &#36;return;
  290. }
  291. &#35;------------------------------------------------
  292. sub getRef &#35;得到关键字对应元素,BOOK<Perl 5 Complete>(中文) page(227)
  293. {
  294. my (&#36;line,&#36;keys) = @_;
  295. my (&#36;element,@elements) = @_;
  296. my &#36;return = {};
  297. my &#36;i;
  298. @elements = split(/&#36;main::split/,&#36;line);
  299. for (&#36;i=0;&#36;i<@elements ;&#36;i++)
  300. {
  301. &#36;element = &#36;elements[&#36;i];
  302. &#36;element =~ s/^\s+//g;
  303. &#36;element =~ s/\s+&#36;//g;
  304. &#36;return->{&#36;keys->{&#36;i}}=&#36;element;
  305. }
  306. return &#36;return;
  307. }
  308. &#35;------------------------------------------------
  309. sub readtxtfile
  310. {
  311. my &#36;just = &#36;_[0].".lock";
  312. while(-f &#36;just)
  313. {select(undef,undef,undef,0.1);}
  314. open(LOCKFILE,">&#36;just");
  315. open(READTXTFILE,"&#36;_[0]");
  316. my @readtxtfile=<READTXTFILE>;
  317. close(READTXTFILE);
  318. close(LOCKFILE);
  319. unlink(&#36;just);
  320. return @readtxtfile;
  321. }
  322. &#35;------------------------------------------------
  323. sub writetxtfile
  324. {
  325. my &#36;just = &#36;_[0].".lock";
  326. while(-f &#36;just)
  327. {select(undef,undef,undef,0.1);}
  328. open(LOCKFILE,">&#36;just");
  329. if (&#36;_[2] == 1)
  330. {open (WRITETXTFILE,">&#36;_[0]");}
  331. else{open (WRITETXTFILE,">>&#36;_[0]");}
  332. print WRITETXTFILE &#36;_[1];
  333. close(WRITETXTFILE);
  334. close(LOCKFILE);
  335. unlink(&#36;just);
  336. return(1);
  337. }
  338. &#35;------------------------------------------------
  339. sub notify
  340. {
  341. use CGI;
  342. my &#36;query = new CGI;
  343. print &#36;query->header() if (&#36;_[1] == 1);
  344. print &#36;_[0];
  345. exit;
  346. }
  347. &#35;------------------------------------------------
  348. 1;
  349. __END__
  350. =head1 NAME
  351. JTDB -- A modules of control a txt-database width SQL-words
  352. =head1 SYNOPSIS
  353. use lib "."; &#35; If NT,use lib "path-to-jtdb_directory";
  354. use JTDB "1.01";
  355. &#36;main::split = ","; &#35; Notice!, It';s necessary! must be &#36;main::split,
  356. &#35; Records split by ","
  357. my &#36;db = "<path-to>/dbname";
  358. @main::recordNames = &db_connect(&#36;db); &#35; Necessary! must be @main::recordNames,
  359. &#35; Get RecordNames from db-info file
  360. my &#36;sqlStr = "SELECT * FROM &#36;db";
  361. my @resoult = &executeStr(&#36;sqlStr);
  362. my &#36;line;
  363. foreach &#36;line (@resoult)
  364. {
  365. my &#36;keys;
  366. foreach &#36;keys (keys %&#36;line)
  367. {
  368. print &#36;keys." : ".&#36;line->{&#36;keys}."";
  369. }
  370. print "<br>\n";
  371. }
  372. =head1 DESCRIPTION
  373. This modules, JTDB.pm, is a tool of control  txt-database  width  SQL-words.
  374. For now,only SELECT,INSERT,DELETE,UPDATE can be used in this script,and It';s
  375. very simple.
  376. It is only  opening-words, and I think  some one will  make it fullness and
  377. mightiness one day! So,you can modify it at will!    and I hope you tell us
  378. the headway of this modules and share it width everybody.   at last, I hope
  379. you do not remove my copyright,if u will...
  380. Enjoy it!
  381. =item db_connect
  382. open dbname_info.txt and get @recordNames
  383. =item executeStr
  384. Execute sql-script,and return a Array of Array
  385. my @resoult = &executeStr(&#36;sqlStr);
  386. my &#36;line;
  387. foreach &#36;line (@resoult)
  388. {
  389. print &#36;line->{';id';}."\n";
  390. print &#36;line->{';name';}."\n";
  391. }
  392. =item create_db
  393. usage:
  394. my &#36;ids = "id,name,pass,lover"; &#35; Now,&#36;main::split = ","
  395. &#35; If &#36;ids = "id||name||pass||lover" then &#36;main::split = "||"
  396. my &#36;dbname = "jtdatabase";
  397. create_db("<path-to>/".&#36;dbname,&#36;ids);
  398. &#35; Then,<path-to>/jtdatabase.txt and <path-to>/jtdatabase_info.txt has been
  399. &#35; created !
  400. =head2 SQL-String
  401. select id,name from &#36;db where id>6
  402. select * from from &#36;db where name=~ m"Aren"i and email ne ""
  403. notices: at the block of WHERE ,u can use a-short-perl-code !!
  404. --------------------------------------------------------------
  405. INSERT INTO &#36;db (id,name) values(2009,Aren)
  406. insert into &#36;db values ( 2009,Aren,12345,mylover)
  407. notices: do not use '; or " at values-list
  408. insert into &#36;db values ( ';2009';,';Aren';,';12345';,';mylover';)
  409. will set id="';2009';" and name="';Aren';" and ...
  410. --------------------------------------------------------------
  411. DELETE FROM &#36;db WHERE id =~ /J/
  412. --------------------------------------------------------------
  413. update &#36;db set name=';jack';,pass=\"123\",lover=';jack\"lover'; where id = 3
  414. =head1 BUGS
  415. Author Aren <boyaren@sina.com> http://www.justake.com
  416. =cut
复制代码

                     我是一个呼吸着现在的空气而生活在过去的人
               这样的注定孤独,孤独的身处闹市却犹如置身于荒漠
                                     我已习惯了孤独,爱上孤独
                                 他让我看清了自我,还原了自我
                             让我再静静的沉思中得到快乐和满足
                                   再孤独的世界里我一遍又一遍
                                   不厌其烦的改写着自己的过去
                                             延伸到现在与未来
                                       然而那只是泡沫般的美梦
                                 产生的时刻又伴随着破灭的到来
                         在灰飞烟灭的瞬间我看到的是过程的美丽
                                      而不是结果的悲哀。。。
返回列表