如何检查 PowerBuilder DataWindow 中的欺骗
How to check for dupes in PowerBuilder DataWindow
我的数据源 Person
中有这个 table,有多个联系电话:
+----------+------+------+------+------+
| NAME | con1 | con2 | con3 | con4 |
+----------+------+------+------+------+
| Jack | 123 | 3214 | 7458 | 454 |
+----------+------+------+------+------+
| Mack | 113 | 3224 | 9458 | 954 |
+----------+------+------+------+------+
| Mary | 133 | 3215 | 3458 | 054 |
+----------+------+------+------+------+
如何验证来自 SLE 对象的字符串是否已经在我的 table 中?
例如:用户在SLE对象中输入9458
,然后点击一个名为Save
的按钮,App然后告诉用户联系人号码9458
已经在数据库中.
我想这样做以避免重复。
有很多方法可以做到这一点。
如果您一次输入一个合同价值(假设价值是“123”),您可以在 table 中创建一个 INSERT 语句,条件为
AND contract_num <> '123'
。
如果 id 已经存在于 table 中,这将失败。你勾选了SQL的success/failure然后通知用户
解决方案 1:在数据窗口中查找重复行 - 通过表达式(否 SQL)
这个概念是按要比较的列对数据进行排序,并使用数据窗口表达式(或过滤器)突出显示重复的行。例如,通过在背景颜色属性上向列或计算列添加表达式,将重复行的列背景颜色设置为红色。
这是通过引用列名(在表达式中)并将其与相同的列名进行比较来完成的,[-1] 表示前一行。请务必先通过代码或菜单对数据窗口进行排序。
PowerScript 代码
// First sort the data using PowerScript
dw_1.setsort('your_pk_or_composite_key')
dw_1.sort()
表达式代码
// datawindow column expression on background color for column or computed field
if (primary_or_concat_key = primary_or_concat_key[-1]), rgb(255,0,0), rgb(255,255,255))
解决方案 2:在数据窗口中查找重复行 - 通过过滤器(否 SQL)
这与数据窗口列表达式的概念相同,但使用了数据窗口过滤器。如果要比较的行很多,这会使查找重复项变得更加容易。一旦代码为 运行,只有具有重复的行将可见,所有其他行将从主数据窗口缓冲区中过滤掉。
如果您希望将此作为验证过程的一部分,您可以将其放入 event/function 并防止在查找重复项时保存。
PowerScript 代码
// sort by key or composite columns concatenated
dw_1.SetSort ('key_or_concatenated_cols A')
dw_1.Sort()
// now run the filter, any rows remaining are duplicates
dw_1.SetFilter ('key_or_concatenated_cols = key_or_concatenated_cols [-1]')
dw_1.Filter()
// number of duplicates stored in local variable
integer li_count
li_count = dw_1.RowCount()
// reset the datawindow filter
dw_1.SetFilter('')
dw_1.Filter()
提示:在 PowerBuilder 中使用表达式时检查列中的空值始终是一个好主意,因为 comparing/concatenating 空值会导致不可预测的行为。
如果您想使用数据库查找重复项,Matt Balent 的解决方案非常适合。
我的数据源 Person
中有这个 table,有多个联系电话:
+----------+------+------+------+------+
| NAME | con1 | con2 | con3 | con4 |
+----------+------+------+------+------+
| Jack | 123 | 3214 | 7458 | 454 |
+----------+------+------+------+------+
| Mack | 113 | 3224 | 9458 | 954 |
+----------+------+------+------+------+
| Mary | 133 | 3215 | 3458 | 054 |
+----------+------+------+------+------+
如何验证来自 SLE 对象的字符串是否已经在我的 table 中?
例如:用户在SLE对象中输入9458
,然后点击一个名为Save
的按钮,App然后告诉用户联系人号码9458
已经在数据库中.
我想这样做以避免重复。
有很多方法可以做到这一点。
如果您一次输入一个合同价值(假设价值是“123”),您可以在 table 中创建一个 INSERT 语句,条件为
AND contract_num <> '123'
。
如果 id 已经存在于 table 中,这将失败。你勾选了SQL的success/failure然后通知用户
解决方案 1:在数据窗口中查找重复行 - 通过表达式(否 SQL)
这个概念是按要比较的列对数据进行排序,并使用数据窗口表达式(或过滤器)突出显示重复的行。例如,通过在背景颜色属性上向列或计算列添加表达式,将重复行的列背景颜色设置为红色。
这是通过引用列名(在表达式中)并将其与相同的列名进行比较来完成的,[-1] 表示前一行。请务必先通过代码或菜单对数据窗口进行排序。
PowerScript 代码
// First sort the data using PowerScript
dw_1.setsort('your_pk_or_composite_key')
dw_1.sort()
表达式代码
// datawindow column expression on background color for column or computed field
if (primary_or_concat_key = primary_or_concat_key[-1]), rgb(255,0,0), rgb(255,255,255))
解决方案 2:在数据窗口中查找重复行 - 通过过滤器(否 SQL)
这与数据窗口列表达式的概念相同,但使用了数据窗口过滤器。如果要比较的行很多,这会使查找重复项变得更加容易。一旦代码为 运行,只有具有重复的行将可见,所有其他行将从主数据窗口缓冲区中过滤掉。
如果您希望将此作为验证过程的一部分,您可以将其放入 event/function 并防止在查找重复项时保存。
PowerScript 代码
// sort by key or composite columns concatenated
dw_1.SetSort ('key_or_concatenated_cols A')
dw_1.Sort()
// now run the filter, any rows remaining are duplicates
dw_1.SetFilter ('key_or_concatenated_cols = key_or_concatenated_cols [-1]')
dw_1.Filter()
// number of duplicates stored in local variable
integer li_count
li_count = dw_1.RowCount()
// reset the datawindow filter
dw_1.SetFilter('')
dw_1.Filter()
提示:在 PowerBuilder 中使用表达式时检查列中的空值始终是一个好主意,因为 comparing/concatenating 空值会导致不可预测的行为。
如果您想使用数据库查找重复项,Matt Balent 的解决方案非常适合。