python oracle 将结果放入字典并匹配给定的列表
python oracle gets result into dictionary and matches the given list
如何将 sql 结果中的给定列表匹配到字典中?
这是我的代码:
import cx_Oracle
con = cx_Oracle.connect('fmstech','fmstech','fmsdev')
cur = con.cursor()
cur.execute("select * from num_prefix where network = 'GLOBE'")
globe = ['639988800000', '639066256904', '0150422 153023']
results = {}
for lines in cur:
results[lines[0]]=lines[1]
globe1 = globe[1][2:5]
if globe1 in results:
print '906 is exist'
else:
print '906 is not exist'
cur.close()
con.close()
但我得到了结果:906 is not exist
解决方法是这样的。使用 int()
globe1 = int(globe[1][2:5])
示例 1: with out int()
results = {817: 'globe', 906: 'globe'}
globe = ['639988800000', '639066256904', '0150422 153023']
globe1 = globe[1][2:5]
if globe1 in results:
print '906 is exist'
else:
print '906 is not exist'
o/p
906 is not exist
示例 2:使用 int()
results = {817: 'globe', 906: 'globe'}
globe = ['639988800000', '639066256904', '0150422 153023']
globe1 = int(globe[1][2:5])
if globe1 in results:
print '906 is exist'
else:
print '906 is not exist'
o/p
906 is exist
原因:
在字典中,这里的键是数字,而 globel 在您的代码中是字符串。所以将其转换为 NUMBER 并进行比较。
如何将 sql 结果中的给定列表匹配到字典中? 这是我的代码:
import cx_Oracle
con = cx_Oracle.connect('fmstech','fmstech','fmsdev')
cur = con.cursor()
cur.execute("select * from num_prefix where network = 'GLOBE'")
globe = ['639988800000', '639066256904', '0150422 153023']
results = {}
for lines in cur:
results[lines[0]]=lines[1]
globe1 = globe[1][2:5]
if globe1 in results:
print '906 is exist'
else:
print '906 is not exist'
cur.close()
con.close()
但我得到了结果:906 is not exist
解决方法是这样的。使用 int()
globe1 = int(globe[1][2:5])
示例 1: with out int()
results = {817: 'globe', 906: 'globe'}
globe = ['639988800000', '639066256904', '0150422 153023']
globe1 = globe[1][2:5]
if globe1 in results:
print '906 is exist'
else:
print '906 is not exist'
o/p
906 is not exist
示例 2:使用 int()
results = {817: 'globe', 906: 'globe'}
globe = ['639988800000', '639066256904', '0150422 153023']
globe1 = int(globe[1][2:5])
if globe1 in results:
print '906 is exist'
else:
print '906 is not exist'
o/p
906 is exist
原因:
在字典中,这里的键是数字,而 globel 在您的代码中是字符串。所以将其转换为 NUMBER 并进行比较。