本章要点
本章介绍常规的基于数据库的全文搜索技术。包括:精确搜索、范围搜索和模糊搜索。本章是继续学习基于数据库的全文搜索的基础,是常规知识的概要总结。
3.1常规的数据库搜索
常规的数据库搜索,就是基于标准SQL语句的句法进行的数据查询。通常要使用“=”进行精确搜索,使用“between”、“in”符号设定范围搜索,使用“like”实现模糊搜索。结合数据库的内置函数(如:instr等),还可以实现其他类型的搜索,因数据库而异。
使用ASP3.0,ASP.NET1.1,PHP5.1.4,JSP2.4等技术均可实现常规的数据库搜索,本章各举一例来说明这些情况。
关于SQL Server2000数据库的安装见附录A,其基本使用方法见附录B。
关于Oracle9i数据库的安装见附录C,其基本使用方法见附录D。
关于MySQL5.0.19数据库的安装见附录E,其基本使用方法见附录F。
3.2使用ASP实现常规的数据库搜索
这一节的案例演示了基于数据库的搜索,使用ASP3.0技术,以及SQL Server2000数据库中的pubs数据库和authors表格。
3.2.1使用ASP实现精确搜索
精确搜索,使用“=”来表达,如以下案例所示。(本书程序的关键代码都用粗体表示。)
案例名称:精确搜索
程序名称:exact.asp
<%
set conn=server.CreateObject("adodb.connection")
set rs=server.CreateObject("adodb.recordset")
conn.Open"server=LOCALHOST;driver={sql server};uid=sa;pwd=;
database=pubs;"
search="White"
sql="select*from authors where au lname=′"& search &"""
rs.Open sql,conn,1,1
while not rs.eof
response.Write rs("au id")&"——"& rs("au lname")&"——"& rs("au fname")&"<br>"
rs.movenext
wend
rs.close
conn.close
set rs=nothing
set conn=nothing
%>
这个程序将语句"select*from authors where au lname="white""传递给数据库服务器。
它直接在数据库中执行。这就是一个用“=”表达的精确检索。
3.2.2使用ASP实现范围搜索
范围搜索,就是设定搜索条件在某个范围之中,然后进行搜索。下面的案例演示了基于数据库的范围搜索。
案例名称:范围搜索
程序名称:range.asp
<%
set conn=server.CreateObject("adodb.connection")
set rs=server.CreateObject("adodb.recordset")
conn.Open"server=LOCALHOST;driver={sql server};uid=sa;pwd=;database=pubs;"
sql="select*from authors where au lname in("White","Green",′Carson")"
rs.Open sql,conn,1,1
while not rs.eof
response.Write rs("au id")&"——"& rs("au lname")&"——"& rs("au fname")&"<br>"
rs.movenext
wend
rs.close
conn.close
set rs=nothing
set conn=nothing
%>
这个程序将语句"select*from authors where au lname in("White","Green",′Carson")"传递给数据库服务器。它直接在数据库中执行。这就是一个用“in”表达的精确检索。
常用的还有between谓词实现的搜索,如以下的案例所示。
案例名称:between谓词的使用
程序名称:between.asp
<%
set conn=server.CreateObject("adodb.connection")
set rs=server.CreateObject("adodb.recordset")
conn.Open"server=localhost;driver={sql server};uid=sa;pwd=;database=pubs;"
sql="select*from authors where contract between1and2"
rs.Open sql,conn,1,1
while not rs.eof
response.Write rs("au id")&"——"& rs("au lname")&"——"& rs("au fname")&"<br>"
rs.movenext
wend
rs.close
conn.close
set rs=nothing
set conn=nothing
%>
这个程序将语句"select*from authors where contract between1and2"传递给数据库服务器。它直接在数据库中执行的结果。这就是一个用“between”表达的精确检索。
常规的数据库搜索,就是基于标准SQL语句的句法进行的数据查询。通常要使用“=”进行精确搜索,使用“between”、“in”符号设定范围搜索,使用“like”实现模糊搜索。结合数据库的内置函数,还可以实现其他类型的搜索,因数据库而异。
3.2.3使用ASP实现模糊搜索
与前两类搜索相比,模糊搜索用得更加广泛,在标准SQL语句中使用“like”谓词来实现,通常的模糊匹配模式有“%”和“”。“%”表示任意多个任意字符,“”表示一个任意字符。下面的案例演示了它们的用法。
案例名称:模糊搜索-1
程序名称:fuzzy1.asp
<%
set conn=server.CreateObject("adodb.connection")
set rs=server.CreateObject("adodb.recordset")
conn.Open"server=localhost;driver={sql server};uid=sa;pwd=;database=pubs;"
sql="select*from authors where au lname like"hite""
rs.Open sql,conn,1,1
while not rs.eof
response.Write rs("au id")&"——"& rs("au lname")&"——"& rs("au fname")&"<br>"
rs.movenext
wend
rs.close
conn.close
set rs=nothing
set conn=nothing
%>
这里用的模式是“hite”,也就是在“hite”前面有一个字符,在authors表格的au lname字段中只有“White”符合要求。语句"select*from authors where au lname like"hite""在数据库中的运行结果。
使用“%”进行模式匹配的情况比“”多一些,下面的案例演示了它的用法。
案例名称:模糊搜索-2
程序名称:fuzzy2.asp
<%
set conn=server.CreateObject("adodb.connection")
set rs=server.CreateObject("adodb.recordset")
conn.Open"server=localhost;driver={sql server};uid=sa;pwd=;database=pubs;"
sql="select*from authors where au lname like"%M%""
rs.Open sql,conn,1,1
while not rs.eof
response.Write rs("au id")&"——"& rs("au lname")&"——"& rs("au fname")&"<br>"
rs.movenext
wend
rs.close
conn.close
set rs=nothing
set conn=nothing
%>
模式“%M%”匹配的是所有au lname字段包含字母“M”的记录。
在前两个例子的基础上,做一个稍微复杂一点测试,将模式变为“a%e%”,符合这个模式的au lname字段包括MacFeather,Karsen,Panteley。在查询分析器中执行语句"select*from authors where au lname like"a%e%""。
3.3使用ASP.NET实现常规的数据库搜索
这一节的案例演示了基于数据库的搜索,使用ASP.NET1.1技术,以及SQL Server2000数据库中的pubs数据库和authors表格。
3.3.1使用ASP.NET实现精确搜索
精确搜索,使用“=”来表达,如下面案例所示。
案例名称:精确搜索
程序名称:exact.aspx
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<script language="VB"runat="server">
Sub Page Load(SenderAs Object,EAs EventArgs)
Dim connAs New SqlConnection
("server=localhost;database=pubs;uid=sa;pwd=;")
dim searchAs String="White"
dim sqlAs String="select*from authors where au lname=′"& search &"""
Dim cmdAs New SqlCommand(sql,conn)
Dim drAs SqlDataReader
try
conn.Open()
dr=cmd.ExecuteReader()
while dr.read
response.write(dr("au id")&"——"& dr("au lname")&"——"& dr("au fname")&"<br>")
end while
catch ex as Exception
response.write("出错了!")
finally
if not dr is nothing then
dr.close
end if
if not conn is nothing then
conn.close
end if
end try
End Sub
</script>
3.3.2使用ASP.NET实现范围搜索
案例名称:范围搜索
程序名称:range.aspx
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<script language="VB"runat="server">
Sub Page Load(SenderAs Object,EAs EventArgs)
Dim connAs New SqlConnection
("server=localhost;database=pubs;uid=sa;pwd=;")
dim sqlAs String=
"select*from authors where au lname in("White","Green",′Carson")"
Dim cmdAs New SqlCommand(sql,conn)
Dim drAs SqlDataReader
try
conn.Open()
dr=cmd.ExecuteReader()
while dr.read
response.write(dr("au id")&"——"& dr("au lname")&"——"& dr("au fname")&"<br>")
end while
catch ex as Exception
response.write("出错了!")
finally
if not dr is nothing then
dr.close
end if
if not conn is nothing then
conn.close
end if
end try
End Sub
</script>
3.3.3使用ASP.NET实现模糊搜索
这里演示使用“%”进行模式匹配的情况。