﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-gyn-随笔分类-Database</title><link>http://www.cnitblog.com/gyn/category/7726.html</link><description>Win32下的Perl，无用的select，停滞的Tk，结束吧....</description><language>zh-cn</language><lastBuildDate>Wed, 28 Sep 2011 11:58:22 GMT</lastBuildDate><pubDate>Wed, 28 Sep 2011 11:58:22 GMT</pubDate><ttl>60</ttl><item><title>sqlite schedule-用于管理sqlite计划事件的GUI工具（下载）</title><link>http://www.cnitblog.com/gyn/archive/2009/01/13/53748.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Tue, 13 Jan 2009 11:41:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2009/01/13/53748.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/53748.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2009/01/13/53748.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/53748.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/53748.html</trackback:ping><description><![CDATA[
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">
						<img height="1" src="/WebResource.axd?d=pLXXeGbWF7eXU8SMs2-GFZvUWY2JNH05dFx5YzJhGUYAYJAFEaTEq36NAhTPy7_KekvzDFwt8wvQWdByvJIGWdEq6x2KpKD80&amp;t=633043190666685000" width="1" />
						<a href="/Files/gyn/sqlite%20schedule.rar">点击下载</a> （刚修订了一些bug，甚至连逻辑也改了，所以下面的内容也就没什么意义，要源代码的可电邮我<a href="mailto:gyn_tadao@yahoo.com.cn">gyn_tadao@yahoo.com.cn</a>，要运行下载代码请先安装TCL解析器<a href="http://www.activestate.com/activetcl/downloads/">http://www.activestate.com/activetcl/downloads/</a>）因为对</span>
				<span lang="EN-US">
						<font face="Courier New">bugd</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">中的</span>
				<span lang="EN-US">
						<font face="Courier New">item</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">状态需要定时地进行判断，以确定其是否过期。也就是说，对于</span>
				<span lang="EN-US">
						<font face="Courier New">due</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">中的值小于当前日期但</span>
				<span lang="EN-US">
						<font face="Courier New">status</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">却未</span>
				<span lang="EN-US">
						<font face="Courier New">fixed</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">的，可以认为该</span>
				<span lang="EN-US">
						<font face="Courier New">item</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">就是过期的了，需要将</span>
				<span lang="EN-US">
						<font face="Courier New">status</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">更新为</span>
				<span lang="EN-US">
						<font face="Courier New">outdated</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">。这种事情对于</span>
				<span lang="EN-US">
						<font face="Courier New">sqlserver</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">之类的数据库，一般只需要做一个简单的</span>
				<span lang="EN-US">
						<font face="Courier New">schedule</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">就可以了，但是在</span>
				<span lang="EN-US">
						<font face="Courier New">bugd</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">中我采用的是</span>
				<span lang="EN-US">
						<font face="Courier New">sqlite</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">作为服务器端的数据库，所以这个</span>
				<span lang="EN-US">
						<font face="Courier New">schedule</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">不得不自己编了。</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">之前，我使用</span>
				<span lang="EN-US">
						<font face="Courier New">perl</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">来写了一个，之后思索着索性写一个</span>
				<span lang="EN-US">
						<font face="Courier New">sqlite</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">的</span>
				<span lang="EN-US">
						<font face="Courier New">schedule</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">工具吧，最好还是带</span>
				<span lang="EN-US">
						<font face="Courier New">GUI</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">的。这并不难，</span>
				<span lang="EN-US">
						<font face="Courier New">bugd</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">中的组件正好可以利用。说干就干，两天的功夫就完成了。跟</span>
				<span lang="EN-US">
						<font face="Courier New">bugd</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">很像，算是一个派生物了吧，但是复杂度小了很多，一则实现的功能比较单一，不需要复杂的界面配套；二来，不是</span>
				<span lang="EN-US">
						<font face="Courier New">CS</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">的结构，考虑的问题都集中在本地。真正的难点是怎么维护一个对各个事件需要发生的时间的判断，同时又可以方便地增加和删除</span>
				<span lang="EN-US">
						<font face="Courier New">schedule</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">事件。在这里，我维护了</span>
				<font face="Courier New">
				</font>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">一个名为</span>
				<span lang="EN-US">
						<font face="Courier New">rl</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">的队列，意思是</span>
				<span lang="EN-US">
						<font face="Courier New">runing-list</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">，将需要进入</span>
				<span lang="EN-US">
						<font face="Courier New">schedule</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">的事件名称放入到该队列中，要停止的时候再将其从该队列中移除，由专门的函数循环处理该队列。<br /></span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">
						<img height="338" alt="sqlites.PNG" src="http://www.cnitblog.com/images/cnitblog_com/gyn/sqlites.PNG" width="310" border="0" />
						<br />该函数叫</span>
				<span lang="EN-US">
						<font face="Courier New">runSchedule</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">，形式如下。其中的</span>
				<span lang="EN-US">
						<font face="Courier New">yet</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">用于判断是否已经在当天检查了时间队列</span>
				<span lang="EN-US">
						<font face="Courier New">rl</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">。</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">proc runSchedule {} {<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /?><o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 1">    </span>global rl loop_interval yet<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 1">    </span>update idletask<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 1">    </span>if [at 00:00:00] {<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 2">       </span>if $yet {<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 3">           </span>schedule $rl<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 3">           </span>set yet 0<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 2">       </span>}<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 1">    </span>}<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 1">    </span>after [expr $loop_interval * 1000] runSchedule<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">}</font>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">在原来的函数中，我是使用</span>
				<span lang="EN-US">
						<font face="Courier New">after idle</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">来进行循环的，由于使用了</span>
				<span lang="EN-US">
						<font face="Courier New">update idletasks</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">，该程序界面运行第很流畅，但是在资源管理器中</span>
				<span lang="EN-US">
						<font face="Courier New">cpu</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">的使用率达到了</span>
				<span lang="EN-US">
						<font face="Courier New">100%</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">，没办法是能改为一个秒级的</span>
				<span lang="EN-US">
						<font face="Courier New">interval</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">。</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">每个新的一天到来的时候，真正的队列处理在</span>
				<span lang="EN-US">
						<font face="Courier New">schedule</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">被处理。该函数如下：</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">proc schedule {rl} {<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 1">    </span>global cmd scdl dbl<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 1">    </span>set sep1 @<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 1">    </span>set sep2 |<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 1">    </span>
								<o:p>
								</o:p>
						</font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 1">    </span>foreach n $rl {<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 2">       </span>set m [dict get $cmd $n]<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 2">       </span>set s [dict get $scdl $n]<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 2">       </span>set d [dict get $dbl $n]<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 2">       </span>foreach {ery dy tm} [split $s $sep1] {}<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 2">       </span>switch $ery {<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 3">           </span>month {<span style="mso-tab-count: 1">    </span>set d [clock format [clock seconds] -format "%d"]}<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 3">           </span>week<span style="mso-spacerun: yes">  </span>{<span style="mso-tab-count: 1"></span>set d [clock format [clock seconds] -format "%a"]}<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 3">           </span>default {}<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 2">       </span>}<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 2">       </span>if [info exists d] {<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 3">           </span>foreach e [split $dy $sep2] {<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 4">              </span>if {$e eq $d} {<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 5">                  </span>runCommand $tm $d $m<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 4">              </span>}<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 3">           </span>}<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 1">    </span>
								<span style="mso-tab-count: 1">    </span>} else {<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 3">           </span>runCommand $tm $m<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 2">       </span>}<span style="mso-tab-count: 1">   </span><o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 1">    </span>}<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">}</font>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">在</span>
				<span lang="EN-US">
						<font face="Courier New">schedule</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">中，根据</span>
				<span lang="EN-US">
						<font face="Courier New">rl</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">中的事件名称，将通过</span>
				<span lang="EN-US">
						<font face="Courier New">cmd</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">、</span>
				<span lang="EN-US">
						<font face="Courier New">scdl</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">和</span>
				<span lang="EN-US">
						<font face="Courier New">dbl</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">三个</span>
				<span lang="EN-US">
						<font face="Courier New">dict</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">，调出对应的命令，运行时间和所在的数据库。之后通过</span>
				<span lang="EN-US">
						<font face="Courier New">runCommand</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">来根据具体时间来运行</span>
				<span lang="EN-US">
						<font face="Courier New">SQL</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">命令。</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">proc runCommand {tm db cmd} {<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 1">    </span>global loop_interval<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 1">    </span>if ![at $tm] {<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 2">       </span>update idletask<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 2">       </span>after [expr $loop_interval * 1000] [list runCommand $tm $cmd]<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 1">    </span>} else {<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 2">       </span>sqlite3 d $db<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 2">       </span>if [catch {d eval $cmd} err] {<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 3">           </span>updateStatusInfo [join $err _]<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 2">       </span>}<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 2">       </span>d close<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 1">    </span>}<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">}</font>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">最后需要说明的是</span>
				<span lang="EN-US">
						<font face="Courier New">at</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">函数，它是用来判断是否过了所给定的时间的，也就是说只要当前时间大于输入参数，它就返回为真，不然为否。所以真正意义上应该叫做</span>
				<span lang="EN-US">
						<font face="Courier New">after</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">，但是</span>
				<span lang="EN-US">
						<font face="Courier New">after</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">已经是</span>
				<span lang="EN-US">
						<font face="Courier New">tcl</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">默认函数了，所以改成了</span>
				<span lang="EN-US">
						<font face="Courier New">at</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">。</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">proc at {tm} {<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 1">    </span>global yet<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 1">    </span>if {[clock scan $tm] &gt;= [clock seconds]} {set yet 1; return 1}<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">
								<span style="mso-tab-count: 1">    </span>return 0<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<font face="Courier New">}</font>
				</span>
		</p>
<img src ="http://www.cnitblog.com/gyn/aggbug/53748.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2009-01-13 19:41 <a href="http://www.cnitblog.com/gyn/archive/2009/01/13/53748.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>sqlite无记录操作</title><link>http://www.cnitblog.com/gyn/archive/2006/12/17/20678.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Sun, 17 Dec 2006 06:46:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2006/12/17/20678.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/20678.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2006/12/17/20678.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/20678.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/20678.html</trackback:ping><description><![CDATA[
		<p class="MsoNormal">
				<span lang="EN-US">Sqlite</span>
				<span style="font-family: 宋体;">中借鉴了</span>
				<span lang="EN-US">oracle</span>
				<span style="font-family: 宋体;">、</span>
				<span lang="EN-US">db2</span>
				<span style="font-family: 宋体;">以及</span>
				<span lang="EN-US">postgresql</span>
				<span style="font-family: 宋体;">中得</span>
				<span lang="EN-US">null</span>
				<span style="font-family: 宋体;">操作方法，也就是借由</span>
				<span lang="EN-US">coalesce</span>
				<span style="font-family: 宋体;">的方法来处理</span>
				<span lang="EN-US">null</span>
				<span style="font-family: 宋体;">类型的数据。当遇到返回</span>
				<span lang="EN-US">null</span>
				<span style="font-family: 宋体;">数据时，</span>
				<span lang="EN-US">coalesce</span>
				<span style="font-family: 宋体;">将第二个参数代替</span>
				<span lang="EN-US">null</span>
				<span style="font-family: 宋体;">。举例如下：</span>
		</p>
		<p class="MsoNormal">
				<span style="font-family: 宋体;">有表</span>
				<span lang="EN-US">phones</span>
				<span style="font-family: 宋体;">：</span>
				<br />
				<span lang="EN-US">create table phones(name text, number integer);</span>
				<br />
				<span lang="EN-US">insert into phones values(‘josh’, 86123413);</span> <br /><span lang="EN-US">insert into phones values(‘mariah’, 89804517);</span><br /><span lang="EN-US">insert into phones values(‘samantha’, NULL);</span></p>
		<p class="MsoNormal">
				<span style="font-family: 宋体;">很不幸，</span>
				<span lang="EN-US">samantha</span>
				<span style="font-family: 宋体;">的办公室还没有装电话，所以在表里虽然有她的名字但只能用</span>
				<span lang="EN-US">NULL</span>
				<span style="font-family: 宋体;">来做填充。如果有人希望在该数据库中查询她的电话时，将得到一个</span>
				<span lang="EN-US">NULL</span>
				<span style="font-family: 宋体;">的数据。当然首先是要设置</span>
				<span lang="EN-US">null</span>
				<span style="font-family: 宋体;">的表示字符串，通常情况下我们使用</span>
				<span lang="EN-US">’NULL’</span>
				<span style="font-family: 宋体;">来表示。为了更清楚地表达这种不存在的状况就需要使用</span>
				<span lang="EN-US">coalesce</span>
				<span style="font-family: 宋体;">。<br /></span>
				<span lang="EN-US">Select coalesce(number, ‘no phone number for ’||name) as number from phones where name = ‘samantha’;</span>
		</p>
		<p class="MsoNormal">
				<span style="font-family: 宋体;">这样的查询可以得到合适的结果。至此似乎一切顺利，可事实上却忽视了一种最基本的情况。</span>
		</p>
		<p class="MsoNormal">
				<span lang="EN-US">Select coalesce(number, ‘no phone number for ’||name) as number from phones where name = ‘tata’;</span>
		</p>
		<p class="MsoNormal">
				<span style="font-family: 宋体;">公司里大概是没有一个叫</span>
				<span lang="EN-US">tata</span>
				<span style="font-family: 宋体;">的人，也许我们甚至难以确定他的性别。可是即便使用了</span>
				<span lang="EN-US">coalesce</span>
				<span style="font-family: 宋体;">也将一无所获。</span>
				<span lang="EN-US">Sqlite</span>
				<span style="font-family: 宋体;">里就是这样的，相关的文档里没有作出很好的解释。但它的确是返回了一个</span>
				<span lang="EN-US">NULL</span>
				<span style="font-family: 宋体;">值的，这个可以使用</span>
				<span lang="EN-US">nullif</span>
				<span style="font-family: 宋体;">来得到证实。<br /></span>
				<span lang="EN-US">Select nullif((Select number from phones where name = ‘tata’), NULL) as result;</span>
		</p>
		<p class="MsoNormal">
				<span style="font-family: 宋体;">还好可以确定它返回了一个</span>
				<span lang="EN-US">NULL</span>
				<span style="font-family: 宋体;">，只要将</span>
				<span lang="EN-US">nullif</span>
				<span style="font-family: 宋体;">嵌到</span>
				<span lang="EN-US">coalesce</span>
				<span style="font-family: 宋体;">中便可以得到希望的结果了。</span>
				<br />
				<span lang="EN-US">Select coalesce(nullif((Select number from phones where name = ‘tata’), NULL) , ‘no such person’) as result;</span>
		</p>
<img src ="http://www.cnitblog.com/gyn/aggbug/20678.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2006-12-17 14:46 <a href="http://www.cnitblog.com/gyn/archive/2006/12/17/20678.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>sqlite远程连接示例</title><link>http://www.cnitblog.com/gyn/archive/2006/12/14/20463.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Thu, 14 Dec 2006 01:29:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2006/12/14/20463.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/20463.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2006/12/14/20463.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/20463.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/20463.html</trackback:ping><description><![CDATA[
		<p class="MsoNormal" style="text-indent: 21pt;">
				<span style="font-family: Arial;" lang="EN-US">Sqlite</span>
				<span style="font-family: 宋体;">是一个只有几百</span>
				<span style="font-family: Arial;" lang="EN-US">k</span>
				<span style="font-family: 宋体;">大小的、</span>
				<span style="font-family: 宋体;">优秀的嵌入式数据库，本身不带有远程连接的功能。由于其身材小，速度快并且为众多的语言支持，所以在单机领域得到了广泛的使用。但由于天然不支持服务</span>
				<span style="font-family: Arial;" lang="EN-US">/</span>
				<span style="font-family: 宋体;">客户端的模式，使其在遇到小型规模数据库远程连接的情况时不得不借助于附加的编程。</span>
				<span style="font-family: Arial;" lang="EN-US">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="text-indent: 21pt;">
				<span style="font-family: 宋体;">单位有三百多人，作为网管每天要接到很多电话，有很多一时无法想起对应的名字，翻查通讯录不仅速度慢而且容易漏过。为了方便起见，在</span>
				<span style="font-family: Arial;" lang="EN-US">sqlite</span>
				<span style="font-family: 宋体;">上建立了一个通讯录数据表，然后用</span>
				<span style="font-family: Arial;" lang="EN-US">perl</span>
				<span style="font-family: 宋体;">编写了一个查询脚本。效果不错的同时，也有同事希望可以得到一份拷贝。但是这时遇到一个问题，如何保证通讯录在不同人手中保持版本的统一性。最好的办法是建立一个服务</span>
				<span style="font-family: Arial;" lang="EN-US">/</span>
				<span style="font-family: 宋体;">客户端的模式，在本机上维护一个数据库，而其它人通过连接数据库得到相关的查询结果。</span>
				<span style="font-family: Arial;" lang="EN-US">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="text-indent: 21pt;">
				<span style="font-family: 宋体;">具体的解决方法是这样的：在本机上维护一个到</span>
				<span style="font-family: Arial;" lang="EN-US">sqlite</span>
				<span style="font-family: 宋体;">数据库的连接并建立一个来自于客户端的</span>
				<span style="font-family: Arial;" lang="EN-US">socket</span>
				<span style="font-family: 宋体;">的监听，接受远程查询条件并回复查询结果；客户端志负责发送和接受这些最简单的任务。</span>
				<span style="font-family: Arial;" lang="EN-US">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="text-indent: 21pt;">
				<span style="font-family: 宋体;">客户端的代码：</span>
				<span style="font-family: Arial;" lang="EN-US">
						<o:p>
						</o:p>
				</span>
		</p>
		<div style="border: 1pt solid windowtext; padding: 1pt 4pt;">
				<p class="MsoNormal" style="border: medium none ; padding: 0cm;">
						<span style="font-family: Arial;" lang="EN-US">use strict;<br />my $in_buffer = undef;<br />my $PF_INET = 2;<br />my $port = 2345;<br />my $remote_addr =
pack('SnC4x8',$PF_INET,$port,192,168,138,228);<br />my $SOCK_DGRAM = 2;<br />socket(UDP_CLIENT, $PF_INET, $SOCK_DGRAM,
getprotobyname('udp'));<br />while(1){<br /><span style="">       </span>print("</span>
						<span style="font-family: 宋体;">输入名字或号码：</span>
						<span style="font-family: Arial;" lang="EN-US">");<br /><span style="">       </span>my
$out_buffer=&lt;STDIN&gt;;<br /><span style="">       </span>chomp($out_buffer);<br /><span style="">       </span>if($out_buffer
eq "exit"){last;}<br /><span style="">       </span>send(UDP_CLIENT,
$out_buffer, 0, $remote_addr);<br /><span style="">       </span>print("waiting
for reply...\n");<br /><span style="">       </span>recv(UDP_CLIENT,
$in_buffer, 100, 0);<br /><span style="">       </span>chomp($in_buffer);<br /><span style="">       </span>print("$in_buffer\n");<br />}<br />close(UDP_CLIENT);<o:p></o:p></span>
				</p>
		</div>
		<p class="MsoNormal">
				<span style="font-family: Arial;" lang="EN-US">
						<span style="">       </span>
				</span>
				<span style="font-family: 宋体;">服务器端的代码：</span>
				<span style="font-family: Arial;" lang="EN-US">
						<o:p>
						</o:p>
				</span>
		</p>
		<div style="border: 1pt solid windowtext; padding: 1pt 4pt;">
				<p class="MsoNormal" style="border: medium none ; padding: 0cm;">
						<span style="font-family: Arial;" lang="EN-US">BEGIN{<br /><span style="">       </span>if( $^O
eq 'MSWin32' ){<br /><span style="">              </span>require
Win32::Console;<br /><span style="">              </span>Win32::Console::Free();<br /><span style="">       </span>}<br />}<o:p><br /></o:p></span>
				</p>
				<p class="MsoNormal" style="border: medium none ; padding: 0cm;">
						<span style="font-family: Arial;" lang="EN-US">use strict;<br />use DBI;<o:p><br /></o:p></span>
				</p>
				<p class="MsoNormal" style="border: medium none ; padding: 0cm;">
						<span style="font-family: Arial;" lang="EN-US">#database parameters<br />my $db_path = 'd:/src/cc/phones.db';<br />my $dbh =
DBI-&gt;connect("dbi:SQLite:$db_path", {PrintError =&gt; 0}) or die
$DBI::errstr;<br />my $sth = undef;<o:p><br /></o:p></span>
				</p>
				<p class="MsoNormal" style="border: medium none ; padding: 0cm;">
						<span style="font-family: Arial;" lang="EN-US">#socket server parameters<br />my $in_buffer = undef;<br />my $out_buffer = undef;<br />my $PF_INET = 2;<br />my $port = 2345;<br />my $local_addr =
pack('SnC4x8',$PF_INET,$port,192,168,138,228);<br />my $SOCK_DGRAM = 2;<br />socket(UDP_SERVER, $PF_INET,$SOCK_DGRAM,
getprotobyname('udp')) or die("$!");<br />bind(UDP_SERVER, $local_addr) or die("$!");<br />listen(UDP_SERVER, 10);<o:p><br /></o:p></span>
				</p>
				<p class="MsoNormal" style="border: medium none ; padding: 0cm;">
						<span style="font-family: Arial;" lang="EN-US">while(1){<br /><span style="">       </span>#receive
query then send result<br /><span style="">       </span>last
unless my $remote_addr = recv(UDP_SERVER,$in_buffer,100,0);<br /><span style="">       </span>chomp($in_buffer);<br /><span style="">       </span>if($in_buffer
=~ /^[0-9]{6}$/){<br /><span style="">              </span>$sth
= $dbh-&gt;prepare("select * from phones where number = $in_buffer");<br /><span style="">              </span>PROCEDURE:<br /><span style="">              </span>$sth-&gt;execute();<br /><span style="">              </span>my
@items = $sth-&gt;fetchrow_array();<br /><span style="">              </span>if(scalar(@items)){<br /><span style="">                     </span>$out_buffer
= $items[0].'</span>
						<span style="font-family: 宋体;">的虚拟网号码是</span>
						<span style="font-family: Arial;" lang="EN-US">'.$items[1];<br /><span style="">              </span>}else{<br /><span style="">                     </span>$out_buffer
= '</span>
						<span style="font-family: 宋体;">查无此人</span>
						<span style="font-family: Arial;" lang="EN-US">';<br /><span style="">              </span>}<br /><span style="">       </span>}else{<br /><span style="">              </span>$sth
= $dbh-&gt;prepare("select * from phones where name = '$in_buffer'");<br /><span style="">              </span>goto
PROCEDURE;<br /><span style="">       </span>}<span style=""><br />       </span>send(UDP_SERVER,$out_buffer,0,$remote_addr);<br />}<o:p><br /></o:p></span>
				</p>
				<p class="MsoNormal" style="border: medium none ; padding: 0cm;">
						<span style="font-family: Arial;" lang="EN-US">#disconnect from sqlite<br />$dbh-&gt;disconnect();<span style="">   </span><o:p></o:p></span>
				</p>
		</div>
<img src ="http://www.cnitblog.com/gyn/aggbug/20463.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2006-12-14 09:29 <a href="http://www.cnitblog.com/gyn/archive/2006/12/14/20463.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>A Short Guide to DBI（转）</title><link>http://www.cnitblog.com/gyn/archive/2006/09/07/16509.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Thu, 07 Sep 2006 12:50:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2006/09/07/16509.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/16509.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2006/09/07/16509.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/16509.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/16509.html</trackback:ping><description><![CDATA[
		<p>A Short Guide to DBI<br />Perl's Database Interface Module<br />by Mark-Jason Dominus<br />October 22, 1999 <br />Short guide to DBI (The Perl Database Interface Module) </p>
		<p> </p>
		<p>General information about relational databases</p>
		<p>Relational databases started to get to be a big deal in the 1970's, andthey're still a big deal today, which is a little peculiar, because they're a 1960's technology.</p>
		<p>A relational database is a bunch of rectangular tables. Each row of a table is a record about one person or thing; the record contains several pieces of information called fields. Here is an example table: </p>
		<p>
				<br /> LASTNAME   FIRSTNAME   ID   POSTAL_CODE   AGE  SEX<br />        Gauss      Karl        119  19107         30   M<br />        Smith      Mark        3    T2V 3V4       53   M<br />        Noether    Emmy        118  19107         31   F<br />        Smith      Jeff        28   K2G 5J9       19   M<br />        Hamilton   William     247  10139         2    M<br />The names of the fields are LASTNAME, FIRSTNAME, ID, POSTAL_CODE, AGE, and SEX. Each line in the table is a record, or sometimes a row or tuple. For example, the first row of the table represents a 30-year-old male whose name is Karl Gauss, who lives at postal code 19107, and whose ID number is 119.</p>
		<p>Sometimes this is a very silly way to store information. When the information naturally has a tabular structure it's fine. When it doesn't, you have to squeeze it into a table, and some of the techniques for doing that are more successful than others. Nevertheless, tables are simple and are easy to understand, and most of the high-performance database systems you can buy today operate under this 1960's model.</p>
		<p>
				<br />About SQL</p>
		<p>SQL stands for Structured Query Language. It was invented at IBM in the 1970's. It's a language for describing searches and modifications to a relational database.</p>
		<p>SQL was a huge success, probably because it's incredibly simple and anyone can pick it up in ten minutes. As a result, all the important database systems support it in some fashion or another. This includes the big players, like Oracle and Sybase, high-quality free or inexpensive database systems like MySQL, and funny hacks like Perl's DBD::CSV module, which we'll see later.</p>
		<p>There are four important things one can do with a table:</p>
		<p>
				<br />SELECT<br />Find all the records that have a certain property</p>
		<p>
				<br />INSERT<br />Add new records</p>
		<p>
				<br />DELETE<br />Remove old records</p>
		<p>
				<br />UPDATE<br />Modify records that are already there </p>
		<p>Those are the four most important SQL commands, also called queries. Suppose that the example table above is named people. Here are examples of each of the four important kinds of queries: </p>
		<p>
				<br /> SELECT firstname FROM people WHERE lastname = 'Smith'<br />(Locate the first names of all the Smiths.) </p>
		<p>
				<br /> DELETE FROM people WHERE id = 3<br />(Delete Mark Smith from the table) </p>
		<p>
				<br /> UPDATE people SET age = age+1 WHERE id = 247<br />(William Hamilton just had a birthday.) </p>
		<p>
				<br /> INSERT INTO people VALUES ('Euler', 'Leonhard', 248, NULL, 58, 'M')<br />(Add Leonhard Euler to the table.) </p>
		<p>There are a bunch of other SQL commands for creating and discarding tables, for granting and revoking access permissions, for committing and abandoning transactions, and so forth. But these four are the important ones. Congratulations; you are now a SQL programmer. For the details, go to any reasonable bookstore and pick up a SQL quick reference.</p>
		<p>Every database system is a little different. You talk to some databases over the network and make requests of the database engine; other databases you talk to through files or something else.</p>
		<p>Typically when you buy a commercial database, you get a library with it. The vendor has written some functions for talking to the database in some language like C, compiled the functions, and the compiled code is the library. You can write a C program that calls the functions in the library when it wants to talk to the database.</p>
		<p>Every vendor's library is different. The names of the functions vary, and the order in which you call them varies, and the details of passing queries to the functions and getting the data back out will vary. Some libraries, like Oracle's, are very thin—they just send the query over to the network to the real database and let the giant expensive real database engine deal with it directly. Other libraries will do more predigestion of the query, and more work afterwards to turn the data into a data structure. Some databases will want you to spin around three times and bark like a chicken; others want you to stand on your head and drink out of your sneaker.</p>
		<p>
				<br />What DBI is For<br />There's a saying that any software problem can be solved by adding a layer of indirection. That's what Perl's DBI (`Database Interface') module is all about. It was written by Tim Bunce.</p>
		<p>DBI is designed to protect you from the details of the vendor libraries. It has a very simple interface for saying what SQL queries you want to make, and for getting the results back. DBI doesn't know how to talk to any particular database, but it does know how to locate and load in DBD (`Database Driver') modules. The DBD modules have the vendor libraries in them and know how to talk to the real databases; there is one DBD module for every different database.</p>
		<p>When you ask DBI to make a query for you, it sends the query to the appropriate DBD module, which spins around three times or drinks out of its sneaker or whatever is necessary to communicate with the real database. When it gets the results back, it passes them to DBI. Then DBI gives you the results. Since your program only has to deal with DBI, and not with the real database, you don't have to worry about barking like a chicken.</p>
		<p> </p>
		<p>Here's your program talking to the DBI library. You are using two databases at once. One is an Oracle database server on some other machine, and another is a DBD::CSV database that stores the data in a bunch of plain text files on the local disk.</p>
		<p>Your program sends a query to DBI, which forwards it to the appropriate DBD module; let's say it's DBD::Oracle. DBD::Oracle knows how to translate what it gets from DBI into the format demanded by the Oracle library, which is built into it. The library forwards the request across the network, gets the results back, and returns them to DBD::Oracle. DBD::Oracle returns the results to DBI as a Perl data structure. Finally, your program can get the results from DBI.</p>
		<p>On the other hand, suppose that your program was querying the text files. It would prepare the same sort of query in exactly the same way, and send it to DBI in exactly the same way. DBI would see that you were trying to talk to the DBD::CSV database and forward the request to the DBD::CSV module. The DBD::CSV module has Perl functions in it that tell it how to parse SQL and how to hunt around in the text files to find the information you asked for. It then returns the results to DBI as a Perl data structure. Finally, your program gets the results from DBI in exactly the same way that it would have if you were talking to Oracle instead.</p>
		<p>There are two big wins that result from this organization. First, you don't have to worry about the details of hunting around in text files or talking on the network to the Oracle server or dealing with Oracle's library. You just have to know how to talk to DBI.</p>
		<p>Second, if you build your program to use Oracle, and then the following week upper management signs a new Strategic Partnership with Sybase, it's easy to convert your code to use Sybase instead of Oracle. You change exactly one line in your program, the line that tells DBI to talk to DBD::Oracle, and have it use DBD::Sybase instead. Or you might build your program to talk to a cheap, crappy database like MS Access, and then next year when the application is doing well and getting more use than you expected, you can upgrade to a better database next year without changing any of your code.</p>
		<p>There are DBD modules for talking to every important kind of SQL database. DBD::Oracle will talk to Oracle, and DBD::Sybase will talk to Sybase. DBD::ODBC will talk to any ODBC database including Microsoft Acesss. (ODBC is a Microsoft invention that is analogous to DBI itself. There is no DBD module for talking to Access directly.) DBD::CSV allows SQL queries on plain text files. DBD::mysql talks to the excellent MySQL database from TCX DataKonsultAB in Sweden. (MySQL is a tremendous bargain: It's $200 for commercial use, and free for noncommerical use.)</p>
		<p>Example of How to Use DBI<br />Here's a typical program. When you run it, it waits for you to type a last name. Then it searches the database for people with that last name and prints out the full name and ID number for each person it finds. For example: </p>
		<p>
				<br /> Enter name&gt; Noether<br />                118: Emmy Noether</p>
		<p>        Enter name&gt; Smith<br />                3: Mark Smith<br />                28: Jeff Smith</p>
		<p>        Enter name&gt; Snonkopus<br />                No names matched `Snonkopus'.<br />        <br />        Enter name&gt; ^D<br />Here is the code: </p>
		<p>
				<br /> use DBI;</p>
		<p>        my $dbh = DBI-&gt;connect('DBI:Oracle:payroll')<br />                or die "Couldn't connect to database: " . DBI-&gt;errstr;<br />        my $sth = $dbh-&gt;prepare('SELECT * FROM people WHERE lastname = ?')<br />                or die "Couldn't prepare statement: " . $dbh-&gt;errstr;</p>
		<p>        print "Enter name&gt; ";<br />        while ($lastname = &lt;&gt;) {               # Read input from the user<br />          my @data;<br />          chomp $lastname;<br />          $sth-&gt;execute($lastname)             # Execute the query<br />            or die "Couldn't execute statement: " . $sth-&gt;errstr;</p>
		<p>          # Read the matching records and print them out          <br />          while (@data = $sth-&gt;fetchrow_array()) {<br />            my $firstname = $data[1];<br />            my $id = $data[2];<br />            print "\t$id: $firstname $lastname\n";<br />          }</p>
		<p>          if ($sth-&gt;rows == 0) {<br />            print "No names matched `$lastname'.\n\n";<br />          }</p>
		<p>          $sth-&gt;finish;<br />          print "\n";<br />          print "Enter name&gt; ";<br />        }<br />          <br />        $dbh-&gt;disconnect;</p>
		<p>
				<br /> use DBI;<br />This loads in the DBI module. Notice that we don't have to load in any DBD module. DBI will do that for us when it needs to.</p>
		<p>
				<br /> my $dbh = DBI-&gt;connect('DBI:Oracle:payroll');<br />                or die "Couldn't connect to database: " . DBI-&gt;errstr;<br />The connect call tries to connect to a database. The first argument, DBI:Oracle:payroll, tells DBI what kind of database it is connecting to. The Oracle part tells it to load DBD::Oracle and to use that to communicate with the database. If we had to switch to Sybase next week, this is the one line of the program that we would change. We would have to change Oracle to Sybase.</p>
		<p>payroll is the name of the database we will be searching. If we were going to supply a username and password to the database, we would do it in the connect call: </p>
		<p>
				<br /> my $dbh = DBI-&gt;connect('DBI:Oracle:payroll', 'username', 'password')<br />                or die "Couldn't connect to database: " . DBI-&gt;errstr;<br />If DBI connects to the database, it returns a database handle object, which we store into $dbh. This object represents the database connection. We can be connected to many databases at once and have many such database connection objects.</p>
		<p>If DBI can't connect, it returns an undefined value. In this case, we use die to abort the program with an error message. DBI-&gt;errstr returns the reason why we couldn't connect—``Bad password'' for example. </p>
		<p>
				<br /> my $sth = $dbh-&gt;prepare('SELECT * FROM people WHERE lastname = ?')<br />                or die "Couldn't prepare statement: " . $dbh-&gt;errstr;<br />The prepare call prepares a query to be executed by the database. The argument is any SQL at all. On high-end databases, prepare will send the SQL to the database server, which will compile it. If prepare is successful, it returns a statement handle object which represents the statement; otherwise it returns an undefined value and we abort the program. $dbh-&gt;errstr will return the reason for failure, which might be ``Syntax error in SQL''. It gets this reason from the actual database, if possible.</p>
		<p>The ? in the SQL will be filled in later. Most databases can handle this. For some databases that don't understand the ?, the DBD module will emulate it for you and will pretend that the database understands how to fill values in later, even though it doesn't.</p>
		<p>
				<br /> print "Enter name&gt; ";<br />Here we just print a prompt for the user. </p>
		<p>
				<br /> while ($lastname = &lt;&gt;) {               # Read input from the user<br />          ...<br />        }<br />This loop will repeat over and over again as long as the user enters a last name. If they type a blank line, it will exit. The Perl &lt;&gt; symbol means to read from the terminal or from files named on the command line if there were any.</p>
		<p>
				<br /> my @data;<br />This declares a variable to hold the data that we will get back from the database.</p>
		<p>
				<br /> chomp $lastname;<br />This trims the newline character off the end of the user's input.</p>
		<p>
				<br /> $sth-&gt;execute($lastname)             # Execute the query<br />            or die "Couldn't execute statement: " . $sth-&gt;errstr;<br />execute executes the statement that we prepared before. The argument $lastname is substituted into the SQL in place of the ? that we saw earlier. execute returns a true value if it succeeds and a false value otherwise, so we abort if for some reason the execution fails.</p>
		<p>
				<br /> while (@data = $sth-&gt;fetchrow_array()) {<br />            ...<br />           }<br />fetchrow_array returns one of the selected rows from the database. You get back an array whose elements contain the data from the selected row. In this case, the array you get back has six elements. The first element is the person's last name; the second element is the first name; the third element is the ID, and then the other elements are the postal code, age, and sex.</p>
		<p>Each time we call fetchrow_array, we get back a different record from the database. When there are no more matching records, fetchrow_array returns the empty list and the while loop exits. </p>
		<p>
				<br /> my $firstname = $data[1];<br />             my $id = $data[2];<br />These lines extract the first name and the ID number from the record data. </p>
		<p>
				<br /> print "\t$id: $firstname $lastname\n";<br />This prints out the result. </p>
		<p> if ($sth-&gt;rows == 0) {<br />            print "No names matched `$lastname'.\n\n";<br />          }<br />The rows method returns the number of rows of the database that were selected. If no rows were selected, then there is nobody in the database with the last name that the user is looking for. In that case, we print out a message. We have to do this after the while loop that fetches whatever rows were available, because with some databases you don't know how many rows there were until after you've gotten them all. </p>
		<p>
				<br /> $sth-&gt;finish;<br />          print "\n";<br />          print "Enter name&gt; ";<br />Once we're done reporting about the result of the query, we print another prompt so that the user can enter another name. finish tells the database that we have finished retrieving all the data for this query and allows it to reinitialize the handle so that we can execute it again for the next query. </p>
		<p>
				<br /> $dbh-&gt;disconnect;<br />When the user has finished querying the database, they type a blank line and the main while loop exits. disconnect closes the connection to the database. </p>
		<p>Cached Queries<br />Here's a function which looks up someone in the example table, given their ID number, and returns their age:</p>
		<p>
				<br /> sub age_by_id {<br />          # Arguments: database handle, person ID number<br />          my ($dbh, $id) = @_;<br />          my $sth = $dbh-&gt;prepare('SELECT age FROM people WHERE id = ?')<br />            or die "Couldn't prepare statement: " . $dbh-&gt;errstr;</p>
		<p> $sth-&gt;execute($id) <br />            or die "Couldn't execute statement: " . $sth-&gt;errstr;</p>
		<p> my ($age) = $sth-&gt;fetchrow_array();<br />          return $age;<br />        }<br />It prepares the query, executes it, and retrieves the result. </p>
		<p>There's a problem here though. Even though the function works correctly, it's inefficient. Every time it's called, it prepares a new query. Typically, preparing a query is a relatively expensive operation. For example, the database engine may parse and understand the SQL and translate it into an internal format. Since the query is the same every time, it's wasteful to throw away this work when the function returns.</p>
		<p>Here's one solution: </p>
		<p>
				<br /> { my $sth;<br />          sub age_by_id {<br />            # Arguments: database handle, person ID number<br />            my ($dbh, $id) = @_;</p>
		<p> if (! defined $sth) {<br />              $sth = $dbh-&gt;prepare('SELECT age FROM people WHERE id = ?')<br />                or die "Couldn't prepare statement: " . $dbh-&gt;errstr;<br />            }</p>
		<p> $sth-&gt;execute($id) <br />              or die "Couldn't execute statement: " . $sth-&gt;errstr;</p>
		<p> my ($age) = $sth-&gt;fetchrow_array();<br />            return $age;<br />          }<br />        }<br />There are two big changes to this function from the previous version. First, the $sth variable has moved outside of the function; this tells Perl that its value should persist even after the function returns. Next time the function is called, $sth will have the same value as before.</p>
		<p>Second, the prepare code is in a conditional block. It's only executed if $sth does not yet have a value. The first time the function is called, the prepare code is executed and the statement handle is stored into $sth. This value persists after the function returns, and the next time the function is called, $sth still contains the statement handle and the prepare code is skipped.</p>
		<p>Here's another solution: </p>
		<p>
				<br /> sub age_by_id {<br />          # Arguments: database handle, person ID number<br />          my ($dbh, $id) = @_;<br />          my $sth = $dbh-&gt;prepare_cached('SELECT age FROM people WHERE id = ?')<br />            or die "Couldn't prepare statement: " . $dbh-&gt;errstr;</p>
		<p> $sth-&gt;execute($id) <br />            or die "Couldn't execute statement: " . $sth-&gt;errstr;</p>
		<p> my ($age) = $sth-&gt;fetchrow_array();<br />          return $age;<br />        }<br />Here the only change to to replace prepare with prepare_cached. The prepare_cached call is just like prepare, except that it looks to see if the query is the same as last time. If so, it gives you the statement handle that it gave you before.</p>
		<p>Transactions<br />Many databases support transactions. This means that you can make a whole bunch of queries which would modify the databases, but none of the changes are actually made. Then at the end you issue the special SQL query COMMIT, and all the changes are made simultaneously. Alternatively, you can issue the query ROLLBACK, in which case all the queries are thrown away.</p>
		<p>As an example of this, consider a function to add a new employee to a database. The database has a table called employees that looks like this: </p>
		<p>
				<br /> FIRSTNAME  LASTNAME   DEPARTMENT_ID<br />        Gauss      Karl       17<br />        Smith      Mark       19<br />        Noether    Emmy       17<br />        Smith      Jeff       666<br />        Hamilton   William    17<br />and a table called departments that looks like this: </p>
		<p>
				<br /> ID   NAME               NUM_MEMBERS<br />        17   Mathematics        3<br />        666  Legal              1<br />        19   Grounds Crew       1<br />The mathematics department is department #17 and has three members: Karl Gauss, Emmy Noether, and William Hamilton.</p>
		<p>Here's our first cut at a function to insert a new employee. It will return true or false depending on whether or not it was successful: </p>
		<p>
				<br /> sub new_employee {<br />          # Arguments: database handle; first and last names of new employee;<br />          # department ID number for new employee's work assignment<br />          my ($dbh, $first, $last, $department) = @_;<br />          my ($insert_handle, $update_handle);</p>
		<p> my $insert_handle = <br />            $dbh-&gt;prepare_cached('INSERT INTO employees VALUES (?,?,?)'); <br />          my $update_handle = <br />            $dbh-&gt;prepare_cached('UPDATE departments <br />                                     SET num_members = num_members + 1<br />                                   WHERE id = ?');</p>
		<p> die "Couldn't prepare queries; aborting"<br />            unless defined $insert_handle &amp;&amp; defined $update_handle;</p>
		<p> $insert_handle-&gt;execute($first, $last, $department) or return 0;<br />          $update_handle-&gt;execute($department) or return 0;<br />          return 1;   # Success<br />        }<br />We create two handles, one for an insert query that will insert the new employee's name and department number into the employees table, and an update query that will increment the number of members in the new employee's department in the department table. Then we execute the two queries with the appropriate arguments. </p>
		<p>There's a big problem here: Suppose, for some reason, the second query fails. Our function returns a failure code, but it's too late, it has already added the employee to the employees table, and that means that the count in the departments table is wrong. The database now has corrupted data in it. </p>
		<p>The solution is to make both updates part of the same transaction. Most databases will do this automatically, but without an explicit instruction about whether or not to commit the changes, some databases will commit the changes when we disconnect from the database, and others will roll them back. We should specify the behavior explicitly.</p>
		<p>Typically, no changes will actually be made to the database until we issue a commit. The version of our program with commit looks like this: </p>
		<p>
				<br /> sub new_employee {<br />          # Arguments: database handle; first and last names of new employee;<br />          # department ID number for new employee's work assignment<br />          my ($dbh, $first, $last, $department) = @_;<br />          my ($insert_handle, $update_handle);</p>
		<p> my $insert_handle = <br />            $dbh-&gt;prepare_cached('INSERT INTO employees VALUES (?,?,?)'); <br />          my $update_handle = <br />            $dbh-&gt;prepare_cached('UPDATE departments <br />                                     SET num_members = num_members + 1<br />                                   WHERE id = ?');</p>
		<p> die "Couldn't prepare queries; aborting"<br />            unless defined $insert_handle &amp;&amp; defined $update_handle;</p>
		<p> my $success = 1;<br />          $success &amp;&amp;= $insert_handle-&gt;execute($first, $last, $department);<br />          $success &amp;&amp;= $update_handle-&gt;execute($department);</p>
		<p> my $result = ($success ? $dbh-&gt;commit : $dbh-&gt;rollback);<br />          unless ($result) { <br />            die "Couldn't finish transaction: " . $dbh-&gt;errstr <br />          }<br />          return $success;<br />        }<br />We perform both queries, and record in $success whether they both succeeded. $success will be true if both queries succeeded, false otherwise. If the queries succeded, we commit the transaction; otherwise, we roll it back, cancelling all our changes.</p>
		<p>The problem of concurrent database access is also solved by transactions. Suppose that queries were executed immediately, and that some other program came along and examined the database after our insert but before our update. It would see inconsistent data in the database, even if our update would eventually have succeeded. But with transactions, all the changes happen simultaneously when we do the commit, and the changes are committed automatically, which means that any other program looking at the database either sees all of them or none.</p>
		<p> </p>
		<p>do<br />If you're doing an UPDATE, INSERT, or DELETE there is no data that comes back from the database, so there is a short cut. You can say </p>
		<p>
				<br /> $dbh-&gt;do('DELETE FROM people WHERE age &gt; 65');<br />for example, and DBI will prepare the statement, execute it, and finish it. do returns a true value if it succeeded, and a false value if it failed. Actually, if it succeeds it returns the number of affected rows. In the example it would return the number of rows that were actually deleted. (DBI plays a magic trick so that the value it turns is true even when it is 0. This is bizarre, because 0 is usually false in Perl. But it's convenient because you can use it either as a number or as a true-or-false success code, and it works both ways.)</p>
		<p>
				<br />AutoCommit<br />If your transactions are simple, you can save yourself the trouble of having to issue a lot of commits. When you make the connect call, you can specify an AutoCommit option which will perform an automatic commit operation after every successful query. Here's what it looks like: </p>
		<p>
				<br /> my $dbh = DBI-&gt;connect('DBI:Oracle:payroll', <br />                               {AutoCommit =&gt; 1},<br />                              )<br />                or die "Couldn't connect to database: " . DBI-&gt;errstr;</p>
		<p>Automatic Error Handling<br />When you make the connect call, you can specify a RaiseErrors option that handles errors for you automatically. When an error occurs, DBI will abort your program instead of returning a failure code. If all you want is to abort the program on an error, this can be convenient:</p>
		<p>
				<br /> my $dbh = DBI-&gt;connect('DBI:Oracle:payroll', <br />                               {RaiseError =&gt; 1},<br />                              )<br />                or die "Couldn't connect to database: " . DBI-&gt;errstr;</p>
		<p>Don't do This<br />People are always writing code like this: </p>
		<p>
				<br /> while ($lastname = &lt;&gt;) {<br />          my $sth = $dbh-&gt;prepare("SELECT * FROM people <br />                                   WHERE lastname = '$lastname'");<br />          $sth-&gt;execute();<br />          # and so on ...<br />        }<br />Here we interpolated the value of $lastname directly into the SQL in the prepare call. </p>
		<p>This is a bad thing to do for three reasons. </p>
		<p>First, prepare calls can take a long time. The database server has to compile the SQL and figure out how it is going to run the query. If you have many similar queries, that is a waste of time.</p>
		<p>Second, it will not work if $lastname contains a name like O'Malley or D'Amico or some other name with an '. The ' has a special meaning in SQL, and the database will not understand when you ask it to prepare a statement that looks like </p>
		<p>
				<br /> SELECT * FROM people WHERE lastname = 'O'Malley'<br />It will see that you have three 's and complain that you don't have a fourth matching ' somewhere else.</p>
		<p>Finally, if you're going to be constructing your query based on a user input, as we did in the example program, it's unsafe to simply interpolate the input directly into the query, because the user can construct a strange input in an attempt to trick your program into doing something it didn't expect. For example, suppose the user enters the following bizarre value for $input:</p>
		<p> x' or lastname = lastname or lastname = 'y<br />Now our query has become something very surprising:</p>
		<p> SELECT * FROM people WHERE lastname = 'x' <br />         or lastname = lastname or lastname = 'y'<br />The part of this query that our sneaky user is interested in is the second or clause. This clause selects all the records for which lastname is equal to lastname; that is, all of them. We thought that the user was only going to be able to see a few records at a time, and now they've found a way to get them all at once. This probably wasn't what we wanted.<br /></p>
<img src ="http://www.cnitblog.com/gyn/aggbug/16509.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2006-09-07 20:50 <a href="http://www.cnitblog.com/gyn/archive/2006/09/07/16509.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Postgresql8.1的索引</title><link>http://www.cnitblog.com/gyn/archive/2006/08/01/14496.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Tue, 01 Aug 2006 00:35:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2006/08/01/14496.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/14496.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2006/08/01/14496.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/14496.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/14496.html</trackback:ping><description><![CDATA[
		<h1 style="margin: 17pt 0cm 16.5pt;">
				<span lang="EN-US">Postgresql8.1</span>
				<span style="font-family: 宋体;">的索引</span>
		</h1>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<o:p> </o:p>
				</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">索引是提高数据库性能的常用方法。索引可以大大加速对特定字段机器组合的检索或查找速度，但与此同时又会增加数据库的负荷，所以如何有效的利用索引成为了一名</span>
				<span lang="EN-US">DBA</span>
				<span style="font-family: 宋体;">必须掌握的技术。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<o:p> </o:p>
				</span>
		</p>
		<h3 style="margin: 13pt 0cm;">
				<span style="font-family: 宋体;">
						<font size="5">介绍</font>
				</span>
		</h3>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<o:p> </o:p>
				</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">对于下面这样一个表：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">Create table test1(</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">       </span>Id integer,</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">       </span>Content varchar</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">)</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">在该表上将大量采用如下查询：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">Select content from test1 where id=constant;</span>
				<span style="font-family: 宋体;">（</span>
				<span lang="EN-US">constant</span>
				<span style="font-family: 宋体;">表示一个常熟）</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">通常数据库不得不一行一行地扫描整个</span>
				<span lang="EN-US">test1</span>
				<span style="font-family: 宋体;">表来寻找所有匹配的数据。如果，最终得到的数据只占</span>
				<span lang="EN-US">test1</span>
				<span style="font-family: 宋体;">中很小的一部分，那么使用如上的方法显然很低效。如果我们在</span>
				<span lang="EN-US">id</span>
				<span style="font-family: 宋体;">列上维护一个索引用于定位匹配的行，那么数据库将只需通过检索搜查树上的若干层就可以得到所需的结果。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">下面的命令将在</span>
				<span lang="EN-US">id</span>
				<span style="font-family: 宋体;">上建立索引：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">Create index test1_id_index on test1(id);</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">当然，我们可以自由地给索引取一个名字，只要之后可以方便地想起来就行。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">删除索引地命令：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">Drop index test1_id_index;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">很大程度上，一个有效的索引是建立在事前分析的基础上的，一旦建立了一个索引，就不需要再有太多地干预了，不过也可以定期使用</span>
				<span lang="EN-US">analyze</span>
				<span style="font-family: 宋体;">命令来更新统计数据，使数据库查询规划器能做出有效的判断。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">除了需要避免不必要的索引对系统带来过多的负荷，索引无论使对查找、删除还是更新，都将带来一定程度上性能的提升。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<o:p> </o:p>
				</span>
		</p>
		<h3 style="margin: 13pt 0cm;">
				<span style="font-family: 宋体;">
						<font size="5">索引的类型</font>
				</span>
		</h3>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="color: red; font-family: 宋体;">关键字：操作符集</span>
				<span style="color: red;">
				</span>
				<span style="color: red; font-family: 宋体;">一定类型的索引只能在相关操作符集中触发</span>
				<span style="color: red;" lang="EN-US">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">Postgresql8.1</span>
				<span style="font-family: 宋体;">提供</span>
				<span lang="EN-US">b-tree</span>
				<span style="font-family: 宋体;">，</span>
				<span lang="EN-US">r-tree</span>
				<span style="font-family: 宋体;">，</span>
				<span lang="EN-US">hash</span>
				<span style="font-family: 宋体;">，</span>
				<span lang="EN-US">gist</span>
				<span style="font-family: 宋体;">这几种索引类型。每一种索引，因为所使用的算法不同，所以适用的场合也有所不同。缺省的情况下，系统将使用</span>
				<span lang="EN-US">b-tree</span>
				<span style="font-family: 宋体;">索引，它也可以适用于大多数的情况。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<o:p> </o:p>
				</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">b-tree</span>
				<span style="font-family: 宋体;">可以处理按照某种顺序排列的数据的等值或范围查询，当索引所在列使用一下几种比较符号时，查询规划器将考虑使用索引。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">&gt;</span>
				<span style="font-family: 宋体;">，</span>
				<span lang="EN-US">&lt;</span>
				<span style="font-family: 宋体;">，</span>
				<span lang="EN-US">&gt;=</span>
				<span style="font-family: 宋体;">，</span>
				<span lang="EN-US">&lt;=</span>
				<span style="font-family: 宋体;">，</span>
				<span lang="EN-US">=</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">构造等效于这些符号的组合也将会启动索引，如</span>
				<span lang="EN-US">between</span>
				<span style="font-family: 宋体;">，</span>
				<span lang="EN-US">in</span>
				<span style="font-family: 宋体;">等。需要注意的使</span>
				<span lang="EN-US">is null</span>
				<span style="font-family: 宋体;">不等于</span>
				<span lang="EN-US">=</span>
				<span style="font-family: 宋体;">，所以不会启动索引。当匹配模式是一个常量并且常量位于开头处时，如：</span>
				<span lang="EN-US">col like ‘foo%’</span>
				<span style="font-family: 宋体;">、</span>
				<span lang="EN-US">col ~ ‘^foo’</span>
				<span style="font-family: 宋体;">，也将启动索引。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<o:p> </o:p>
				</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">r-tree</span>
				<span style="font-family: 宋体;">适用与处理二维空间的数据时，要创建一个</span>
				<span lang="EN-US">r-tree</span>
				<span style="font-family: 宋体;">索引可以使用如下命令：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">create indes INDEX_NAME on TABLE_NAME using rtree (COLUMN_NAME);</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">当一个索引了的列涉及到使用下列操作符之一进行比较的时候，</span>
				<span lang="EN-US">PostgreSQL </span>
				<span style="font-family: 宋体;">的查询规划器都会考虑使用</span>
				<span lang="EN-US">R-tree </span>
				<span style="font-family: 宋体;">索引∶</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">&lt;&lt;</span>
				<span style="font-family: 宋体;">，</span>
				<span lang="EN-US">&amp;&lt;</span>
				<span style="font-family: 宋体;">，</span>
				<span lang="EN-US">&amp;&gt;</span>
				<span style="font-family: 宋体;">，</span>
				<span lang="EN-US">&gt;&gt;</span>
				<span style="font-family: 宋体;">，</span>
				<span lang="EN-US">&lt;&lt;|</span>
				<span style="font-family: 宋体;">，</span>
				<span lang="EN-US">&amp;&lt;|</span>
				<span style="font-family: 宋体;">，</span>
				<span lang="EN-US">|&gt;&amp;</span>
				<span style="font-family: 宋体;">，</span>
				<span lang="EN-US">|&gt;&gt;</span>
				<span style="font-family: 宋体;">，</span>
				<span lang="EN-US">~</span>
				<span style="font-family: 宋体;">，</span>
				<span lang="EN-US">@</span>
				<span style="font-family: 宋体;">，</span>
				<span lang="EN-US">~=</span>
				<span style="font-family: 宋体;">，</span>
				<span lang="EN-US">&amp;&amp;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<o:p> </o:p>
				</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">散列（</span>
				<span lang="EN-US">hash</span>
				<span style="font-family: 宋体;">）索引只能处理简单的等值查询。就目前而言，由于</span>
				<span lang="EN-US">hash</span>
				<span style="font-family: 宋体;">索引的性能不比</span>
				<span lang="EN-US">b-tree</span>
				<span style="font-family: 宋体;">的好，再加上它的尺寸较大、制作时间较长，并且不支持</span>
				<span lang="EN-US">wal</span>
				<span style="font-family: 宋体;">日志，所以不建议使用。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<o:p> </o:p>
				</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">Gist</span>
				<span style="font-family: 宋体;">不是一种单独的索引类型，而是一种架构，可以在这种架构上实现不同的索引策略。根据策略的不同，可以使用使用</span>
				<span lang="EN-US">gist</span>
				<span style="font-family: 宋体;">索引的操作符也不尽相同。需要注意的是，</span>
				<span lang="EN-US">gist</span>
				<span style="font-family: 宋体;">上有实现</span>
				<span lang="EN-US">r-tree</span>
				<span style="font-family: 宋体;">的一组操作符，而且支持</span>
				<span lang="EN-US">wal</span>
				<span style="font-family: 宋体;">日志，因此比</span>
				<span lang="EN-US">r-tree</span>
				<span style="font-family: 宋体;">更加适用。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<o:p> </o:p>
				</span>
		</p>
		<h3 style="margin: 13pt 0cm;">
				<span style="font-family: 宋体;">
						<font size="5">多字段日志</font>
				</span>
		</h3>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="color: red; font-family: 宋体;">关键字：前导字段</span>
				<span style="color: red;">
				</span>
				<span style="color: red; font-family: 宋体;">索引的前导字段对查询性能起着至关重要的作用</span>
				<span style="color: red;" lang="EN-US">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">一个索引可以定义在表中的多个字段上。比如，你有如下的表：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">Create table test2(</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">       </span>Major int,</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">       </span>Minor int,</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">Name varchar</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">);</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">并且经常使用如下的查询语句：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">Select * from test2 where major=X and minor=Y;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">那么在</span>
				<span lang="EN-US">major</span>
				<span style="font-family: 宋体;">和</span>
				<span lang="EN-US">mino</span>
				<span style="font-family: 宋体;">上定义一个索引会比较合适，</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">Create index multi_index on test2(major,minor);</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">目前只有</span>
				<span lang="EN-US">b-tree</span>
				<span style="font-family: 宋体;">和</span>
				<span lang="EN-US">gist</span>
				<span style="font-family: 宋体;">支持多字段索引，缺省只有</span>
				<span lang="EN-US">32</span>
				<span style="font-family: 宋体;">个字段。可以在编译</span>
				<span lang="EN-US">postgresql</span>
				<span style="font-family: 宋体;">之前，通过修改</span>
				<span lang="EN-US">pg_config_manual.h</span>
				<tt>
						<span style="font-size: 12pt; color: black;">
								<font face="宋体">中的相关参数来改变。<span lang="EN-US"><o:p></o:p></span></font>
						</span>
				</tt>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">只要在查询条件中包含了多字段索引的子集或者全部，那么就有可能触发索引，但是只有在查询条件中使用索引的最前端字段（前导字段）时，索引才最为有效。准确的规则是在索引的前导字段中使用等值约束，再在第二个字段上加上一个非等值查询，这样将限制需要扫描索引的范围。在这两个字段之后的条件将在索引中查询，这样就减少了对表的操作，但这不会减少需要扫描的索引。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">比如：有一个建立在</span>
				<span lang="EN-US">(a,b,c)</span>
				<span style="font-family: 宋体;">上的索引，有一个查询的条件：</span>
				<span lang="EN-US">a=5 and b&gt;44 and c&lt;=77</span>
				<span style="font-family: 宋体;">。索引将从符合</span>
				<span lang="EN-US">a=5 and b=44</span>
				<span style="font-family: 宋体;">条件的第一个条目开始扫描，直到所有符合</span>
				<span lang="EN-US">a=5</span>
				<span style="font-family: 宋体;">条件的条目。此时，将跳过对</span>
				<span lang="EN-US">c&gt;=77</span>
				<span style="font-family: 宋体;">的条目的扫描，但之后还是会扫描的。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">这个索引也可以用在没有对</span>
				<span lang="EN-US">a</span>
				<span style="font-family: 宋体;">进行约束的查询上，但这势必会大大降低性能，因为这将扫描整个索引。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">只有当前导字段出现在查询条件中时，才会启用</span>
				<span lang="EN-US">gist</span>
				<span style="font-family: 宋体;">索引。附加的条件将限制返回的索引条目，但前导字段还是起了决定性的作用，它决定需要扫描字段的数量。如果前导索引相应的字段中有较多的相同值，那么即使在附加索引字段上添加查询条件，查询的整体性能依然将不太理想。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">必须很谨慎地使用多字段索引。在大多数情况下，单字段索引已足够了，而且还节省了生成时间和数据库空间。除非对某个表的查许非常固定，一般不要使用多余</span>
				<span lang="EN-US">3</span>
				<span style="font-family: 宋体;">个字段的索引。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<o:p> </o:p>
				</span>
		</p>
		<h3 style="margin: 13pt 0cm;">
				<span style="font-family: 宋体;">
						<font size="5">多个索引的组合</font>
				</span>
		</h3>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="color: red; font-family: 宋体;">关键字：</span>
				<span style="color: red;" lang="EN-US">bitmap bitmap</span>
				<span style="color: red; font-family: 宋体;">保存按物理位置排序的表中行的入口</span>
				<span style="color: red;" lang="EN-US">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">总结上面两节，单个索引只有适用于以下这种情况：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">查询语句中使用了与索引相关的操作符，并且包含索引对应字段的子集字段的交集。举例说就是，如（</span>
				<span lang="EN-US">a</span>
				<span style="font-family: 宋体;">，</span>
				<span lang="EN-US">b</span>
				<span style="font-family: 宋体;">）的索引可以在如查询条件</span>
				<span lang="EN-US">where a=50 and b=70</span>
				<span style="font-family: 宋体;">的情况下使用，但如果查询条件时</span>
				<span lang="EN-US">where a=50 or b=70</span>
				<span style="font-family: 宋体;">就无法使用该索引了。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">从</span>
				<span lang="EN-US">8.1</span>
				<span style="font-family: 宋体;">版本开始，</span>
				<span lang="EN-US">postgresql</span>
				<span style="font-family: 宋体;">就增加了组合多索引的功能来解决单个索引无法处理的情况。如</span>
				<span lang="EN-US">where x=44 or x=88 or x=43 or x=12</span>
				<span style="font-family: 宋体;">这样的查询，可以分解为</span>
				<span lang="EN-US">4</span>
				<span style="font-family: 宋体;">个单独的查询，</span>
				<span lang="EN-US">postgresql</span>
				<span style="font-family: 宋体;">将</span>
				<span lang="EN-US">4</span>
				<span style="font-family: 宋体;">个单独查询的结果用</span>
				<span lang="EN-US">or</span>
				<span style="font-family: 宋体;">运算组合在一起得到最终的结果。再来一个例子是，</span>
				<span lang="EN-US">where x=45 and y=46</span>
				<span style="font-family: 宋体;">，如果再</span>
				<span lang="EN-US">x</span>
				<span style="font-family: 宋体;">和</span>
				<span lang="EN-US">y</span>
				<span style="font-family: 宋体;">上各有一个索引，那么</span>
				<span lang="EN-US">postgresql</span>
				<span style="font-family: 宋体;">将分别在这两个索引上扫描，然后将得到的进行与运算，得到最终的结果。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">为了合并多个索引，</span>
				<span lang="EN-US">postgresql</span>
				<span style="font-family: 宋体;">扫描需要的索引，将符合索引条件的表中行标识在内存中的</span>
				<span lang="EN-US">bitmap</span>
				<span style="font-family: 宋体;">中，</span>
				<span lang="EN-US">bitmap</span>
				<span style="font-family: 宋体;">进行与、或运算得到最终的结果。</span>
				<span lang="EN-US">Bitmap</span>
				<span style="font-family: 宋体;">中记载了按照磁盘顺序排序的表中行的物理位置（查询的结果将对行进行标识），这就意味着任何使用合并的查询将丢失在索引上的排序。如果有</span>
				<span lang="EN-US">order by</span>
				<span style="font-family: 宋体;">这样的排序语句，那么排序的行为将在查询的最后进行，这就降低了性能。所以，有时候规划器会忽略多索引的存在，而只使用单索引。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">在大多数最简单的应用中，可能会有多种索引的组合可供使用，数据库的开发员必须作出合理的选择以优化数据库的性能。有时候，使用多字段索引会很有效，但有时候建立单字段索引再将它们组合起来会更灵活。例如在一表上有对</span>
				<span lang="EN-US">x</span>
				<span style="font-family: 宋体;">、以、</span>
				<span lang="EN-US">x</span>
				<span style="font-family: 宋体;">和</span>
				<span lang="EN-US">y</span>
				<span style="font-family: 宋体;">的三种查询。可以分别在</span>
				<span lang="EN-US">x</span>
				<span style="font-family: 宋体;">、</span>
				<span lang="EN-US">y</span>
				<span style="font-family: 宋体;">上建立单独的索引，利用索引组合可以实现对三种查询的优化。也可以使用一个（</span>
				<span lang="EN-US">x</span>
				<span style="font-family: 宋体;">，</span>
				<span lang="EN-US">y</span>
				<span style="font-family: 宋体;">）的多字段索引，单它对</span>
				<span lang="EN-US">y</span>
				<span style="font-family: 宋体;">的查询无能为力。综合两种方法，可以建立一个（</span>
				<span lang="EN-US">x</span>
				<span style="font-family: 宋体;">，</span>
				<span lang="EN-US">y</span>
				<span style="font-family: 宋体;">）多字段索引，再加上一个建立在</span>
				<span lang="EN-US">y</span>
				<span style="font-family: 宋体;">字段上的索引，唯一的缺点是在</span>
				<span lang="EN-US">x</span>
				<span style="font-family: 宋体;">上的查询性能将弱于建立在</span>
				<span lang="EN-US">x</span>
				<span style="font-family: 宋体;">上的单独索引。最后一种选择是，建立三种查询的各个索引，只是这会降低更新数据库的性能，因此只能用在查询远多于更新，且三种查询都很普遍的情况下。如果其中有一个字段的索查询量远大于其它的字段及其组合，那么建立单独的两个索引会比较合适。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<o:p> </o:p>
				</span>
		</p>
		<h3 style="margin: 13pt 0cm;">
				<span style="font-family: 宋体;">
						<font size="5">唯一索引</font>
				</span>
		</h3>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="color: red; font-family: 宋体;">关键字：实现唯一性的细节</span>
				<span style="color: red;" lang="EN-US">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">索引可以强制其所对应字段或字段集合的唯一性。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">Create unique index NAME on TABLE (COLUMN…)</span>
				<span style="font-family: 宋体;">；</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">目前，只有</span>
				<span lang="EN-US">b-tree</span>
				<span style="font-family: 宋体;">支持唯一索引。一旦索引被声明为唯一，那么所对应的字段（集合）就不允许存在重复的值。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">Postgresql</span>
				<span style="font-family: 宋体;">在用户为一字段声明唯一或主约束时，自动生成一个唯一索引。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">事实上，应当只将唯一索引当作一种实现唯一性的细节，一般情况下使用如下：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">Alter table …add constraint…</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">来实现唯一性约束，而不直接创建唯一索引。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<o:p> </o:p>
				</span>
		</p>
		<h3 style="margin: 13pt 0cm;">
				<span style="font-family: 宋体;">
						<font size="5">建立在表达式上的索引</font>
				</span>
		</h3>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="color: red; font-family: 宋体;">关键字：计算值</span>
				<span style="color: red;">
				</span>
				<span style="color: red; font-family: 宋体;">索引中存储计算值，与单索引的查询速度相同</span>
				<span style="color: red;" lang="EN-US">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">索引不仅可以建立在对印表的某些字段上，还可以建立在以某些字段的值为参数的函数和表达式上。这个特性对于优化以计算值为查询条件的搜索很有效。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">例如，查询一个与大小写无关的比较</span>
				<span lang="EN-US">:</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">Select * from test1 wherelower(clo1)=’value’;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">相应的，可以建立如下的索引：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">Create index test_lower_col1_idx on test1(lower(col1));</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">如果我们将这个索引声明为唯一索引，那么就可以实现与大小写无关的唯一性约束了。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">另一个例子是，如果经常使用如下的查询：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">Select * from people where ( first_name||’’||last_name)=’jone smith’;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">可以建立如下的索引：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">Create index full_name_idx on people((first_name || ‘’ || lastname));</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">在</span>
				<span lang="EN-US">create index</span>
				<span style="font-family: 宋体;">语法中，除了单个函数外，对应的字段表达式都要有圆括号包起来。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">维护表达式索引的代价相对较大，因为索引中存储的是真正的计算值而不是在查询中计算，虽然这大大提升了相关的查询速度（与单字段索引的速度相同），但每次执行更新或者插入都需要计算出相应的值。所以，这种索引只用在追求查询速度，而对插入和更新不太计较的情况下。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<o:p> </o:p>
				</span>
		</p>
		<h3 style="margin: 13pt 0cm;">
				<span style="font-family: 宋体;">
						<font size="5">部分索引</font>
				</span>
		</h3>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="color: red; font-family: 宋体;">关键字：索引条件</span>
				<span style="color: red;">
				</span>
				<span style="color: red; font-family: 宋体;">查询只有在匹配索引条件时才可能被触发</span>
				<span style="color: red;" lang="EN-US">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">部分索引是建立在表的子集上的索引，该子集有索引中的条件表达式决定。只有符合表达式条件的表的行才会被建立索引。部分索引是一种特殊的特征，但在一些特定的场合中会很有用。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">建立部分索引的主要理由是避免对普便的数据建立索引。因为一个重复的普遍数据可能在表中占据较可观的比例，在其上建立索引不会起到优化性能的结果，事实上规划器甚至不会启动索引。建立部分索引，不但减小了索引的大小，从而提高了查询的速度，而且加速了更新的速度。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">假设，在数据库中存储了网络服务器的登陆记录。大多数的记录显示来自于内网的访问。如果我们需要检索的仅仅是来自外网的记录，那么就不必在整张表上建立索引，只需建立部分索引。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">Create table access_log(</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">url varchar,</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">client_ip inet,</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">…</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">);</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">可建立如下的部分索引：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">Create index access_log_client_ip_idx on access_log (client_ip) </span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">       </span>Where not (client_ip &gt; inet ‘192.168.<st1:chmetcnv tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="100" unitname="’" w:st="on">100.0’</st1:chmetcnv> and client_ip &lt; inet ‘192.168.<st1:chmetcnv tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="100.255" unitname="’" w:st="on">100.255’</st1:chmetcnv>);</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">一个典型的可以使用以上索引的查询是：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">Select * from access_log where url = ‘/index.htm’ and client_ip = ‘212.78.<st1:chmetcnv tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="10.32" unitname="’" w:st="on">10.32’</st1:chmetcnv>::inet;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">一个不能使用该索引的例子：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">Select * from access_log where client_ip = inet ‘192.168.<st1:chmetcnv tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="100.23" unitname="’" w:st="on">100.23’</st1:chmetcnv>;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">注意到像这样的部分索引需要可预计的普遍数值。如果数值的分布因为应用程序的性质而是固定的，或者是不随时间变化的静态值，那么建立这样的部分索引并不困难。但如果普遍数据来自于不固定的大量一致性数据（拥有相同的索引对应字段的值）装载，那么就需要管理员不断地修改索引。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<o:p> </o:p>
				</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">另一个部分索引地应用是剔除某些典型查询不感兴趣的数据。在能收到于以上情况相同的效果的同时，它还避免进入不干兴趣的数据，即使索引对访问这些不感兴趣的数据有好处。建立这种索引必须很小心，应该事先做大量的试验。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">例子：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">如果有这么一个表，包含了已付款和未付款的订单，未付款的订单只占总数据很小的一部分，但却被很频繁地访问，为优化性能，可以在该表上建立为付款订单的部分索引。索引如下：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">Create index orders_unbilled_index on orders(order_nr)</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">       </span>Where billed is not true;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">一个可能启动该索引的查询是：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">Select * from orders where billed is not true and order&lt;10000;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">然而，该索引还可以被不涉及到</span>
				<span lang="EN-US">order_nr</span>
				<span style="font-family: 宋体;">的查询触发：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">Select * from orders where billed is not true and amount&gt;500.00;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">该查询显然不会比直接建立在</span>
				<span lang="EN-US">amount</span>
				<span style="font-family: 宋体;">上的索引高效，因为这相当于在检索一个表，但如果</span>
				<span lang="EN-US">unbilled</span>
				<span style="font-family: 宋体;">的数据量较少的话，它还是一个不错的选择。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">这样的查询将不会使用以上的索引：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">Select * from orders where order_nr=3501;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">这个例子说明了索引字段不必于条件中涉及的字段相同。</span>
				<span lang="EN-US">Postgresql</span>
				<span style="font-family: 宋体;">支持任意条件字段的部分索引，只要这些字段出现在表中。要记住的一点是，只有在索引中的条件符合查询条件时，部分索引才会触发。更准确地说，只有在查询条件中，以数学上地意义包含了部分索引的条件，该索引才会起效。至今，</span>
				<span lang="EN-US">postgresql</span>
				<span style="font-family: 宋体;">还没有一套复杂的理论来保证系统可以识别形式不同、但却有相同意义的数学表达式。（这样功能的实现过于复杂，而且在真正应用中会很慢。）系统可以自动识别简单的不等式包含，比如：“</span>
				<span lang="EN-US">x&lt;1</span>
				<span style="font-family: 宋体;">”包含在“</span>
				<span lang="EN-US">x&lt;2</span>
				<span style="font-family: 宋体;">”中，除此之外必须提供精确匹配索引条件的查询条件。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<o:p> </o:p>
				</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">第三种情况不需要部分索引被查询使用，主要用于在字段的部分数据上建立唯一约束。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">Create table tests(</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">Subject text,</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">target text,</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">success Boolean,</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">…..</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">)</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">Create unique index tests_success_constraint on tests (subject, target) where success;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<o:p> </o:p>
				</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">最后，部分索引可以重载系统的查询规划选择。这发生在因为数据分布较为特殊，而使系统在不该使用索引的时候使用了索引。这时可以建立部分索引使得它在此类查询中不可用。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<o:p> </o:p>
				</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">需要注意的是，建立部分索引意味着索引建立者必须知道查询，特别是要知道索引在什么情况下是有用的。要完全掌握需要丰富的经验，并且要理解</span>
				<span lang="EN-US">postgresql</span>
				<span style="font-family: 宋体;">索引的工作原理。大多数情况下，部分索引不比一个普通的索引更显优势。</span>
		</p>
<img src ="http://www.cnitblog.com/gyn/aggbug/14496.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2006-08-01 08:35 <a href="http://www.cnitblog.com/gyn/archive/2006/08/01/14496.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>postgres8.1字符串常函数概述</title><link>http://www.cnitblog.com/gyn/archive/2006/07/20/13873.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Thu, 20 Jul 2006 01:21:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2006/07/20/13873.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/13873.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2006/07/20/13873.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/13873.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/13873.html</trackback:ping><description><![CDATA[
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-indent: -21pt;">
				<span style="" lang="EN-US">
						<span style="">1.<span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;">         </span></span>
				</span>
				<span lang="EN-US">Char_length(string)</span>
				<span style="font-family: 宋体;">、</span>
				<span lang="EN-US">character_length(string)</span>
				<span style="font-family: 宋体;">和</span>
				<span lang="EN-US">length(string)</span>
				<span style="font-family: 宋体;">地作用是一样地，都用来得到字符串地长度。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-indent: -21pt;">
				<span style="" lang="EN-US">
						<span style="">2.<span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;">         </span></span>
				</span>
				<span lang="EN-US">Lower(string)</span>
				<span style="font-family: 宋体;">和</span>
				<span lang="EN-US">upper(string)</span>
				<span style="font-family: 宋体;">用来转换字符串地大小写</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-indent: -21pt;">
				<span style="" lang="EN-US">
						<span style="">3.<span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;">         </span></span>
				</span>
				<span lang="EN-US">Overlay(string1 placing string2 from number1 for number2)</span>
				<span style="font-family: 宋体;">用来将</span>
				<span lang="EN-US">string1</span>
				<span style="font-family: 宋体;">中</span>
				<span lang="EN-US">number1</span>
				<span style="font-family: 宋体;">开始地</span>
				<span lang="EN-US">number2</span>
				<span style="font-family: 宋体;">个字符替换为</span>
				<span lang="EN-US">string2</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-indent: -21pt;">
				<span style="" lang="EN-US">
						<span style="">4.<span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;">         </span></span>
				</span>
				<span lang="EN-US">Substring(string1 from num/string2 [for num])</span>
				<span style="font-family: 宋体;">，</span>
				<span lang="EN-US">substring</span>
				<span style="font-family: 宋体;">地用法比较复杂，但基本上可分为两种：按位置取词；按正则表达式取词。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-indent: -21pt;">
				<span style="" lang="EN-US">
						<span style="">5.<span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;">         </span></span>
				</span>
				<span lang="EN-US">Trim(leading | trailing | both [characer] from string)</span>
				<span style="font-family: 宋体;">用于在三种位置删除指定的最长字符。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-indent: -21pt;">
				<span style="" lang="EN-US">
						<span style="">6.<span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;">         </span></span>
				</span>
				<span lang="EN-US">Initcap(string)</span>
				<span style="font-family: 宋体;">用于将单词首字大写化，事实上它就类似于：</span>
				<span lang="EN-US">overlay(string placing upper(substring(string from 1 for 1)) from 1 for 1)</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-indent: -21pt;">
				<span style="" lang="EN-US">
						<span style="">7.<span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;">         </span></span>
				</span>
				<span style="font-family: 宋体;">相对于</span>
				<span lang="EN-US">overlay</span>
				<span style="font-family: 宋体;">的按位置替换，</span>
				<span lang="EN-US">replace(string,from text,to text)</span>
				<span style="font-family: 宋体;">则是按词替换</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-indent: -21pt;">
				<span style="" lang="EN-US">
						<span style="">8.<span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;">         </span></span>
				</span>
				<span style="font-family: 宋体;">最后一个较常用的字符函数是</span>
				<span lang="EN-US">translate</span>
				<span style="font-family: 宋体;">（</span>
				<span lang="EN-US">string,from text, to text</span>
				<span style="font-family: 宋体;">），它将</span>
				<span lang="EN-US">from text</span>
				<span style="font-family: 宋体;">中的字符集按照一对一的关系替换为</span>
				<span lang="EN-US">to text</span>
				<span style="font-family: 宋体;">中的字符集。</span>
		</p>
<img src ="http://www.cnitblog.com/gyn/aggbug/13873.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2006-07-20 09:21 <a href="http://www.cnitblog.com/gyn/archive/2006/07/20/13873.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hibernat_tutorial快速入门</title><link>http://www.cnitblog.com/gyn/archive/2006/06/25/12836.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Sun, 25 Jun 2006 09:03:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2006/06/25/12836.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/12836.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2006/06/25/12836.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/12836.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/12836.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 把官方文档开始例子的实施方法总结了一下，用于快速入门之用。								最终程序的目录结构																												.								+lib								  &lt;Hibernate and third-party libraries&gt;								+src				...&nbsp;&nbsp;<a href='http://www.cnitblog.com/gyn/archive/2006/06/25/12836.html'>阅读全文</a><img src ="http://www.cnitblog.com/gyn/aggbug/12836.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2006-06-25 17:03 <a href="http://www.cnitblog.com/gyn/archive/2006/06/25/12836.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>采编系统客户端网络的改造</title><link>http://www.cnitblog.com/gyn/archive/2006/06/02/11472.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Fri, 02 Jun 2006 08:23:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2006/06/02/11472.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/11472.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2006/06/02/11472.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/11472.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/11472.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 采编系统客户端网络的改造														 																										关键词：						postgresql														数据库，						vlan														，						acl														，三层交换，路由							...&nbsp;&nbsp;<a href='http://www.cnitblog.com/gyn/archive/2006/06/02/11472.html'>阅读全文</a><img src ="http://www.cnitblog.com/gyn/aggbug/11472.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2006-06-02 16:23 <a href="http://www.cnitblog.com/gyn/archive/2006/06/02/11472.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>用来建立“局”文中数据库的脚本</title><link>http://www.cnitblog.com/gyn/archive/2006/05/15/10567.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Mon, 15 May 2006 13:25:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2006/05/15/10567.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/10567.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2006/05/15/10567.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/10567.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/10567.html</trackback:ping><description><![CDATA[create table net_top(switch_pos int, floor int);<br />create table net_port(room int primary key, port1 int default 0, port2 int default 0);<br />create table net_mac_addr(room int references net_port(room),<br />mac_addr varchar(30) unique check (mac_addr ~ '^(00)[0-9A-F]{10}$'), <br />ip_addr varchar(30) check (ip_addr ~ '[0-9]{3}(\.[0-9]{1,3}){3}$'),<br />owner varchar(20)<br />);<br />create view net_room_view as<br />select * from net_top t left outer join net_port p <br />on (p.room/100=t.floor or p.room=t.floor);<br />create view net_owner_view as<br />select n.room, n.mac_addr, n.ip_addr, n.owner, p.port1, p.port2<br />from net_mac_addr n left outer join net_port p<br />on (n.room=p.room);<br /><br />存在'E:\dbase_related\net_mgr.sql'中，然后在命令行中执行<br />psql -U postgres -f e:\dbase_related\net_mgr.sql<br />就行了。<img src ="http://www.cnitblog.com/gyn/aggbug/10567.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2006-05-15 21:25 <a href="http://www.cnitblog.com/gyn/archive/2006/05/15/10567.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>局域网电脑管理系统之一：信息数据库的架构</title><link>http://www.cnitblog.com/gyn/archive/2006/05/11/10374.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Thu, 11 May 2006 13:37:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2006/05/11/10374.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/10374.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2006/05/11/10374.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/10374.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/10374.html</trackback:ping><description><![CDATA[
		<h1 style="margin: 12pt 0cm 3pt; text-align: center;" align="center">
				<span style="font-family: 宋体;">
						<font size="5">局域网电脑管理系统之一</font>
				</span>
		</h1>
		<h3 style="margin: 12pt 0cm 3pt; text-align: center;" align="center">
				<span style="font-family: 宋体;">信息数据库的架构</span>
		</h3>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US"> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">使用</span>
				<span lang="EN-US">postgresql8.1</span>
				<span style="font-family: 宋体;">建立信息数据库。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US"> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">建立三张表：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt; text-indent: -18pt;">
				<span style="font-family: Wingdings;" lang="EN-US">ü<span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;">      </span></span>
				<span style="font-family: 宋体;">交换机分布表：</span>
				<span lang="EN-US">net_top</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt; text-indent: -18pt;">
				<span style="font-family: Wingdings;" lang="EN-US">ü<span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;">      </span></span>
				<span style="font-family: 宋体;">办公室与井道端口对应表：</span>
				<span lang="EN-US">net_port</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt; text-indent: -18pt;">
				<span style="font-family: Wingdings;" lang="EN-US">ü<span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;">      </span></span>
				<span lang="EN-US">mac</span>
				<span style="font-family: 宋体;">地址表：</span>
				<span lang="EN-US">net_mac_addr</span>
				<br />
				<br />
				<img style="width: 448px; height: 218px;" alt="o_dp.jpg" src="http://www.cnitblog.com/images/cnitblog_com/gyn/2736/o_dp.jpg" width="553" border="0" height="274" />
				<br />
				<br />
				<span style="font-family: 宋体;">建立的程序如下：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">       </span>create table net_top(</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">              </span>switch_pos integer,</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">              </span>floor integer</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">       </span>);</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">       </span>create table net_port(</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">              </span>room integer,</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">              </span>port1 integer,</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">              </span>port2 integer</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">       </span>);</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">       </span>create table net_mac_addr(</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">              </span>room integer,</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">              </span>mac_addr varchar(30),</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">              </span>ip_addr varchar(30),</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">              </span>owner varchar(20)</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">       </span>);</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US"> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">建立视图</span>
				<span lang="EN-US">net_room_port</span>
				<span style="font-family: 宋体;">，将表</span>
				<span lang="EN-US">1</span>
				<span style="font-family: 宋体;">、</span>
				<span lang="EN-US">2</span>
				<span style="font-family: 宋体;">连接起来。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">create view net_room_view as</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">
						<span style="">  </span>select * from net_top t left outer join net_port p </span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 72pt; text-indent: 36pt;">
				<span lang="EN-US">on (p,room/100=t.floor);</span>
				<br />
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: center;" align="center">
				<span lang="EN-US">
				</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<br />
						<img style="width: 467px; height: 219px;" alt="o_net_room_port.jpg" src="http://www.cnitblog.com/images/cnitblog_com/gyn/2736/o_net_room_port.jpg" width="553" border="0" height="274" />
						<br />
						<br /> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">给</span>
				<span lang="EN-US">3</span>
				<span style="font-family: 宋体;">张表添加约束，使之能正确得存储。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">       </span>
				</span>
				<span style="font-family: 宋体;">在</span>
				<span lang="EN-US">net_port</span>
				<span style="font-family: 宋体;">中，将</span>
				<span lang="EN-US">room</span>
				<span style="font-family: 宋体;">设为主键：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">              </span>alter table net_port add constraint room_prim primary key (room);</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">       </span>
				</span>
				<span style="font-family: 宋体;">在</span>
				<span lang="EN-US">net_mac_addr</span>
				<span style="font-family: 宋体;">中将</span>
				<span lang="EN-US">room</span>
				<span style="font-family: 宋体;">设为</span>
				<span lang="EN-US">net_port.room</span>
				<span style="font-family: 宋体;">的外键：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">              </span>alter table net_mac_addr </span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 72pt; text-indent: 36pt;">
				<span lang="EN-US">add foreign key (room) references net_port;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">       </span>
				</span>
				<span style="font-family: 宋体;">在</span>
				<span lang="EN-US">net_mac_addr</span>
				<span style="font-family: 宋体;">中设置</span>
				<span lang="EN-US">mac_addr</span>
				<span style="font-family: 宋体;">和</span>
				<span lang="EN-US">ip_addr</span>
				<span style="font-family: 宋体;">的检查：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">              </span>alter table net_mac_addr add constraint mac_check check</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 72pt; text-indent: 36pt;">
				<span lang="EN-US">(mac_addr ~ ‘^(00) [0-9A-Z]{10 }$’);</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">              </span>alter table net_mac_addr add constraint ip_check</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">                     </span>check (ip_addr ~ ‘[0-9]{3}(\.[0-9]){1,3}$’);</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US"> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">表建立以后，就要进行数据输入。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US"> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">1</span>
				<span style="font-family: 宋体;">：输入表</span>
				<span lang="EN-US">1</span>
				<span style="font-family: 宋体;">的数据：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">       </span>
				</span>
				<span style="font-family: 宋体;">新建一个文本文键</span>
				<span lang="EN-US">e:\switch.txt</span>
				<span style="font-family: 宋体;">，在其中输入数据，如下：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">2<span style="">       </span>1</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">2<span style="">       </span>2</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">2<span style="">       </span>3</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">6<span style="">       </span>4</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">6<span style="">       </span>5</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">6<span style="">       </span>6</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">8<span style="">       </span>7</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">8<span style="">       </span>8</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">8<span style="">       </span>9</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">8<span style="">       </span>10</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">12<span style="">     </span>11</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">12<span style="">     </span>12</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">12<span style="">     </span>13</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">12<span style="">     </span>14</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">12<span style="">     </span>15</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span style="font-family: 宋体;">保存后，将该文本内容拷贝至表</span>
				<span lang="EN-US">1</span>
				<span style="font-family: 宋体;">中。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">copy net_top from ‘e:/switch.txt’;</span>
				<br />
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
				</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<br />
						<img style="width: 465px; height: 214px;" alt="o_net_top.jpg" src="http://www.cnitblog.com/images/cnitblog_com/gyn/2736/o_net_top.jpg" width="553" border="0" height="274" />
						<br /> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">2</span>
				<span style="font-family: 宋体;">：输入表</span>
				<span lang="EN-US">2</span>
				<span style="font-family: 宋体;">的内容</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">       </span>
				</span>
				<span style="font-family: 宋体;">新建一个文本文键</span>
				<span lang="EN-US">e:\room.txt</span>
				<span style="font-family: 宋体;">，编写一个脚本用来输入房间号：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">       </span>
				</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">open(FILE,'&gt;e:/switch.txt');</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US"> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">for($init=1; $init&lt;=15; $init++){</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">       </span>
						<span style="">       </span>for($temp=1; $temp&lt;=15; $temp++){</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">              </span>
						<span style="">       </span>$sum=$init*100+$temp;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">              </span>
						<span style="">       </span>print FILE "$sum"."\n";</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">       </span>
						<span style="">       </span>} </span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">}</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US"> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">close FILE;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">
						<span style="">  </span>
				</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">       </span>
				</span>
				<span style="font-family: 宋体;">然后根据大楼井道分布图所示，将端口填入，以下是其中的一部分：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<span style="">       </span>101<span style=""></span>104<span style=""></span>0</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">102<span style="">   </span>116<span style=""></span>0</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">103<span style="">   </span>105<span style=""></span>0</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">104<span style="">   </span>103<span style=""></span>0</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">105<span style="">   </span>106<span style=""></span>0</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">106<span style="">   </span>0<span style="">     </span>0</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">107<span style="">   </span>107<span style=""></span>0</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">108<span style="">   </span>0<span style="">     </span>0</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">109<span style="">   </span>108<span style=""></span>0</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">110<span style="">   </span>0<span style="">     </span>0</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">11<span style="">     </span>0<span style="">     </span>0</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">112<span style="">   </span>0<span style="">     </span>0</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">113<span style="">   </span>109<span style=""></span>110</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">114<span style="">   </span>0<span style="">     </span>0</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">115<span style="">   </span>0<span style="">     </span>0</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">201<span style="">   </span>204<span style=""></span>0</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">202<span style="">   </span>201<span style=""></span>0</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span style="font-family: 宋体;">总共</span>
				<span lang="EN-US">228</span>
				<span style="font-family: 宋体;">个数据。需要提醒的是，以上所有数据行中的数据都用制表符</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span style="font-family: 宋体;">分割。保存后，将该文本内容拷贝至表</span>
				<span lang="EN-US">1</span>
				<span style="font-family: 宋体;">中。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 36pt;">
				<span lang="EN-US">copy net_port from ‘e:/room.txt’;</span>
				<br />
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
				</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<br />
						<img style="width: 449px; height: 211px;" alt="o_net_port.jpg" src="http://www.cnitblog.com/images/cnitblog_com/gyn/2736/o_net_port.jpg" width="553" border="0" height="274" />
						<br /> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">3</span>
				<span style="font-family: 宋体;">：输入表</span>
				<span lang="EN-US">3</span>
				<span style="font-family: 宋体;">的内容</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span style="font-family: 宋体;">这是最艰巨的，需要一台电脑接着一台得打入</span>
				<span lang="EN-US">ipconfig –all</span>
				<span style="font-family: 宋体;">，然后在逐一输入。为了简化操作，写了一组采集程序。</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span style="font-family: 宋体;">服务器端的程序如下：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US"> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">$in_buffer=undef;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">$PF_INET=2;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">$port=2345;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">$local_addr=pack('SnC4x8',$PF_INET,$port,192,168,138,105);</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">$SOCK_DGRAM=2;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US"> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US"> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US"> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">socket(UDP_SERVER,$PF_INET,$SOCK_DGRAM,getprotobyname('udp')) or die("$!");</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">bind(UDP_SERVER,$local_addr) or die("$!");</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">listen(UDP_SERVER,100);</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US"> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">print("waiting for connection...\n");</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">$count=0;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US"> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">while(1){</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">
						<span style="">  </span>print $count." connection..\n";</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">
						<span style="">  </span>$count++;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">
						<span style="">  </span>if(recv(UDP_SERVER,$in_buffer,100,0)){</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">
						<span style="">         </span>open(FILE,'&gt;&gt;e:/mac_addr.txt');</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">
						<span style="">         </span>chomp($in_buffer);</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">
						<span style="">         </span>print FILE $in_buffer."\n";</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">
						<span style="">         </span>close FILE;<span style="">   </span></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">
						<span style="">  </span>}</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">
						<span style="">  </span>else{next;}</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">}</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US"> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US"> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">close(UDP_SERVER);</span>
				<br />
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">
						<br />
						<img style="width: 434px; height: 245px;" alt="o_net_mgr_serv.jpg" src="http://www.cnitblog.com/images/cnitblog_com/gyn/2736/o_net_mgr_serv.jpg" width="553" border="0" height="357" />
						<br /> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">
				</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US"> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span style="font-family: 宋体;">客户端的程序如下：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">@stack=();</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">$mac_addr=undef;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">$ip_addr=undef;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US"> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">print('</span>
				<span style="font-family: 宋体;">请输入您的办公室门牌号码：</span>
				<span lang="EN-US">');</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">$room_num=&lt;STDIN&gt;;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">chomp($room_num);</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US"> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">print('</span>
				<span style="font-family: 宋体;">请输入您的姓名拼音：</span>
				<span lang="EN-US">');</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">$name=&lt;STDIN&gt;;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">chomp($name);</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US"> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">open(FOO,'-|',"ipconfig -all");</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US"> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">while(&lt;FOO&gt;){</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">
						<span style="">  </span>chomp();</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">
						<span style="">  </span>if($_=~s/(.*)(00(\-[0-9A-Z]{2}){5})(.*)/$2/){</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">
						<span style="">         </span>$mac_addr=join('',split(/-/,$_));</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">
						<span style="">  </span>}</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">
						<span style="">  </span>if($_=~/IP Address/){</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">
						<span style="">         </span>$_=~s/(.*)([0-9]{3}(\.[0-9]{1,3}){3})(.*)/$2/;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">
						<span style="">         </span>$ip_addr=$_;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">
						<span style="">  </span>}</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">}</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US"> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">close FOO;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US"> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">push(@stack, $room_num);</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">push(@stack, $mac_addr);</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">push(@stack, $ip_addr);</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">push(@stack, $name);</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">$out_buffer=join("\t",@stack);</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">print $out_buffer."\n";</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US"> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">$PF_INET=2;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">$port=2345;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">$remote_addr=pack('SnC4x8',$PF_INET,$port,192,168,138,105);</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">$SOCK_DGRAM=2;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">socket(UDP_CLIENT,$PF_INET,$SOCK_DGRAM,getprotobyname('udp'));</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">send(UDP_CLIENT,$out_buffer,0,$remote_addr);</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">close(UDP_CLIENT);</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US"> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US">exit;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt 36pt;">
				<span lang="EN-US"> <o:p></o:p></span> <span lang="EN-US"> <o:p></o:p></span></p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US"> <o:p></o:p></span>
				<br />
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<img style="width: 467px; height: 287px;" alt="o_net_mgr_cli.jpg" src="http://www.cnitblog.com/images/cnitblog_com/gyn/2736/o_net_mgr_cli.jpg" width="553" border="0" height="357" />
						<br /> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">采集的数据被记录到服务器中：<br /></span>
				<br />
				<img style="width: 465px; height: 279px;" alt="o_mac_addr.jpg" src="http://www.cnitblog.com/images/cnitblog_com/gyn/2736/o_mac_addr.jpg" width="553" border="0" height="387" /> </p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
				</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US"> <o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span style="font-family: 宋体;">然后将数据导入数据库：</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">copy net_mac_addr from ‘e:/mac_addr.txt’;</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US"> <o:p></o:p></span>
				<br />
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
				</span>
		</p>
		<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
				<span lang="EN-US">
						<img style="width: 465px; height: 192px;" alt="o_net_mac_addr.jpg" src="http://www.cnitblog.com/images/cnitblog_com/gyn/2736/o_net_mac_addr.jpg" width="553" border="0" height="274" />
						<br /> <o:p></o:p></span>
		</p>
<img src ="http://www.cnitblog.com/gyn/aggbug/10374.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2006-05-11 21:37 <a href="http://www.cnitblog.com/gyn/archive/2006/05/11/10374.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>