在PB中如何实现数据模糊查询
敬业的IT人
互联网
佚名
2008-1-4 12:09:09
- ·技巧:如何实现图片的加密
·如何实现将vsflexgrid中修改的数据反馈
·如何实现网络唤醒开机
·如何实现照片“反冲负冲”特效
·在JSP中如何实现MD5加密
·如何实现网络分段
·如何实现教育网与外网之间快速切换
·如何实现PSTN网络与NGN网络的互通
·TCP/IP协议是如何实现网络模型的
·C++编译器如何实现异常处理
本文主要介绍如何在pb中根据数据窗口中的字段对数据窗口进行模糊查询。本系统的代码示例采用Power Builder6.5进行演示。
代码及设计:
1. 新建一个窗口,命名为w_query。在窗口中放入一个数据窗口控件,命名为dw_master。在dw_master中放入一个数据窗口;放置一个按钮,命名为cb_query。见下图:

2. 新建一个结构(structure)命名为str_result_column,其参数如下:

3. 在dw_master增加两个用户事件,命名为ue_actiborder="0" align="center" alt="在PB中如何实现数据模糊查询(图三)" />
5. 在窗口的Open事件中增加如下代码:
dw_master.setTransObject(sqlca)
6. 在dw_master的ue_actiborder="0" align="center" alt="在PB中如何实现数据模糊查询(图四)" />
其中"执行"按钮名为"cb_exec"、"返回"按钮名为"cb_exit"、"返回"按钮下的数据窗口名为dw_column,其dataObject为d_column_set、中间的数据窗口名为dw_where,其dataObject为d_where。
9. d_column_set的设计完成界面如下所示:

10. 其sql为:

11. dw_where的设计完成界面如下:

12. 其sql为:
- ·技巧:如何实现图片的加密
·如何实现将vsflexgrid中修改的数据反馈
·如何实现网络唤醒开机
·如何实现照片“反冲负冲”特效
·在JSP中如何实现MD5加密
·如何实现网络分段
·如何实现教育网与外网之间快速切换
·如何实现PSTN网络与NGN网络的互通
·TCP/IP协议是如何实现网络模型的
·C++编译器如何实现异常处理
9.5 DataWindow: dw_where的 ue_where 事件
功能:形成WHERE子句,并更新语法框。
string hzcol, ywcol, sValue, sType //, sWhere
//sWhere 现为实例变量,在wroot_query中为局部变量.
string sOper, sLog, sLeft_kh,sRight_kh, tmpsValue
long left_kh,right_kh //左、右括号数
integer i, rownum, delnum //, typenum
dwItemStatus l_status
if ib_changed = true then
ib_changed = false
else
return 0
end if
rownum = dw_where.RowCount()
//去掉dw_where中MaxEditRow行以前所有中间为空或
//者输入不完整的行, 并更新MaxEditRow.
i = 1
delnum = 0
DO WHILE i <= MaxEditRow
l_status = dw_where.GetItemStatus(i,0, Primary!)
if l_status <> New! then
hzcol = GetItemString(i,"column1")
sValue = GetItemString(i,"value")
if (hzcol = "" or isnull(hzcol)) or (sValue = "" or isnull(sValue)) then
dw_where.DeleteRow(i)
delnum += 1
MaxEditRow += -1
Continue
end if
else
dw_where.DeleteRow(i)
delnum += 1
MaxEditRow += -1
Continue
end if
i += 1
LOOP
For i = 1 to DelNum
dw_where.InsertRow(0)
Next
//检查左右括号是否匹配, 即其数量是否一样多.
For i = 1 to MaxEditRow
l_status = dw_where.GetItemStatus(i,0, Primary!)
if l_status <> New! then
left_kh += inv_string.of_countoccurrences(GetItemString(i,"precol"),"(")
right_kh += inv_string.of_countoccurrences(GetItemString(i,"value"),")")
end if
Next
i = left_kh - right_kh
if i <> 0 then
if i > 0 then
sValue = "查询条件中左括号比右括号多了" + String(i) + "个"
else
sValue = "查询条件中左括号比右括号少了" + String(-i) + "个"
end if
if MessageBox("综合查询输入错误",sValue + ",请改正;" + &
"~r~n~r~n否则,所有查询条件将被忽略。",None!,OKCancel!,2)=1 then
return 1
else
dw_where.setfocus()
return 0
end if
end if
//形成WHERE子句,并更新语法框。
sWhere = ""
For i = 1 to MaxEditRow
hzcol = GetItemString(i,"column1")
sOper = space(1) + GetItemString(i,"operator") + space(1)
// 去掉空格键
sValue = Trim(GetItemString(i,"value"))
sLeft_kh = GetItemString(i,"precol") //保存左括号
if isNull(sLeft_kh) then sLeft_kh = ""
if Pos(sValue,")",1) > 0 then //保存右括号
sRight_kh = Right(sValue,(Len(sValue) - Pos(sValue,")",1) + 1))
else
sRight_kh = ""
end if
sValue = inv_string.of_globalreplace(sValue,"'","") //去掉sValue中的单引号.
sValue = inv_string.of_globalreplace(sValue,'"','') //去掉sValue中的双引号.
sValue = inv_string.of_globalreplace(sValue,")","") //去掉sValue中的右括号.
sLog = GetItemString(i,"logical")
if sLog = "" or isNull(sLog) then
sLog = "and"
dw_where.SetItem(i,"logical","and")
end if
ywcol = wf_getYwName(hzcol) //表名.列名
sType = lower(wf_getYwType(hzcol))
CHOOSE CASE sType
CASE "char","character","string","nchar","nvarchar","varchar","time"
if trim(sOper) = "like" or trim(sOper) = "not like" then
sWhere += " (" + sLeft_kh + ywcol + sOper + "'%" + sValue + "%') " + sRight_kh + sLog
else
sWhere += " (" + sLeft_kh + ywcol + sOper + "'" + sValue + "') " + sRight_kh + sLog
end if
CASE "numeric","decimal","decimaln","dec","double","integer","int","smallint",&
"number","long","real","uint","ulong","unsignedint","unsignedinteger","unsignedlong"
if trim(sOper) = "like" or trim(sOper) = "not like" then
if MessageBox("提示信息",hzcol + "不是字符型,不能使用<含有>或<不含有>操作符," + &
"~r~n~r~n请改正; 否则,所有查询条件将被忽略。",None!,OKCancel!,2)=1 then
return 1
else
dw_where.setfocus()
return 0
end if
end if
if isNumber(sValue) then
sWhere += " (" + sLeft_kh + ywcol + sOper + sValue + ") " + sRight_kh + sLog
else
if MessageBox("综合查询输入错误",hzcol + "的值应为数字型,请改正;" + &
"~r~n~r~n否则,所有查询条件将被忽略。",None!,OKCancel!,2)=1 then
Return 1
else
dw_where.setfocus()
return 0
end if
end if
CASE "date","datetime","datetimn","smalldatetime","timestamp"
if trim(sOper) = "like" or trim(sOper) = "not like" then
if MessageBox("提示信息",hzcol + "不是字符型,不能使用<含有>或<不含有>操作符," + &
"~r~n~r~n请改正; 否则,所有查询条件将被忽略。",None!,OKCancel!,2)=1 then
return 1
else
dw_where.setfocus()
return 0
end if
end if
if sType = "date" then
if not isdate(sValue) then
if MessageBox("综合查询输入错误",hzcol + "的值应为日期型,请改正;" + &
"~r~n~r~n否则,所有查询条件将被忽略.",None!,OKCancel!,2)=1 then
Return 1
else
dw_where.setfocus()
return 0
end if
end if
sValue = wf_dateconvert(sValue)
sWhere += " (" + sLeft_kh + ywcol + sOper + "'" + sValue + "') " + sRight_kh + sLog
else
//datetime型的字段在sybase中转换为字符串的类型
sValue = wf_datetime(sValue)
if sValue = "error" then
if MessageBox("综合查询输入错误",hzcol + "的值应为日期时间型,请改正;" + &
"~r~n~r~n否则,所有查询条件将被忽略。",None!,OKCancel!,2)=1 then
Return 1
else
dw_where.setfocus()
return 0
end if
end if
sWhere += " (" + sLeft_kh + "convert(varchar(8),"+ywcol+",112)" + sOper + " '"+sValue+"') " + sRight_kh +
sLog
end if
CASE ELSE
beep(1)
MessageBox("综合查询",sType + "这个数据类型未在本模块中定义。")
Return 0
END CHOOSE
Next
//去掉最后一个逻辑符。
Long pok, pp
pp = 0
DO WHILE True
pok = pp
pp = Pos(sWhere, ")", pp + 1)
if pp = 0 then Exit
LOOP
if pok > 0 then sWhere = Trim(Left(sWhere, pok))
return 1
//最后一个逻辑符去掉结束.
//至此, 用户的WHERE语句部分已经形成完毕于sWhere中.
9.6 DataWindow: dw_where的 ue_mousemove 事件
功能:控制鼠标的移动。
string s_object
s_object = This.GetObjectAtPointer()
if left(s_object,5) = "value" then
MainWindow.SetMicroHelp("此时按鼠标右键可粘贴现有值")
else
MainWindow.SetMicroHelp("准备好")
end if
9.7 DataWindow: dw_where的ue_retrieve事件
功能:对数据窗口进行查询,详细请见代码。
// 如果数据窗口dw_result上并无ue_action_refresh事件,
// 则用户必须重载本事件,编写自己的脚本.
// 如:
// dw_result.retrieve()
//
// 或者如果数据窗口dw_result有retrieve参数,
// 则用户也必须重载本事件,编写自己的脚本.
// 如:
// string ls_id
// ls_id = ...
// dw_result.retrieve(ls_id)
dw_result.triggerevent("ue_action_refresh")
- 最新文章
- PowerBuilder应用开发系列讲座(7)[01-04]
- PowerBuilder应用开发系列讲座(30)[01-04]
- PowerBuilder应用开发系列讲座(18)[01-04]
- PB动态报表格式自由定义的实现[01-04]
- 在PB中实现热键的方法[01-04]
- 如何实现对数据库单个字段进行加密[01-04]
- 相关文章
