Python pyodbc fetchmany()如何选择输出更新查询

时间:2017-07-18 21:06:37

标签: python-3.x pyodbc

我有代码fetchmany()将输出例如:10条记录 我为print语句添加了每个0 1 2 3 4 5的迭代值,现在我想要用户输入0或1,它应该选择列。对于那些输入,所以我可以更新那些列的SQL记录

cur.execute("select events.SERIALNUM, emp.LASTNAME, emp.SSNO, 
events.EVENT_TIME_UTC from AccessControl.dbo.emp, 
AccessControl.dbo.events where emp.id = events.empid and emp.SSNO=? 
order by EVENT_TIME_UTC desc ", empid)
rows = cur.fetchmany(att_date)
n = 0
for row in rows :

event_date = row.EVENT_TIME_UTC 
utc = event_date.replace(tzinfo=from_zone)

utc_to_local = utc.astimezone(to_zone)
local_time = utc_to_local.strftime('%H:%M:%S')
 att_date = utc_to_local.strftime('%d:%m:%y')


 print (n, row.SERIALNUM, row.LASTNAME, row.SSNO, att_date, local_time)
 n = n + 1


seri_al = input("Copy And Past the serial number u want to modifiy: ")

这将输出以下数据

0 1500448188 FIRST NAME  03249 2017-07-19 17:01:17
1 1500448187 FIRST NAME  03249 2017-07-19 17:01:15

例如:

seri_al = input("Copy And Past the serial number u want to modifiy: ")

而不是复制和粘贴' 1500448188'这些数字我希望用户只输入' 0'并映射那个并更新sql查询和where子句序列号。

1 个答案:

答案 0 :(得分:0)

您似乎已经知道如何使用input来提示用户的选择。您唯一缺少的是在循环遍历行时将项添加到字典中。这是一个略有抽象的例子:

rows = [('1500448188',),('1500448187',)]  # test data
selections = dict()
n = 0
for row in rows:
    selections[n] = row[0]
    print(n, repr(row[0]))
    n += 1
select = input("Enter the index (0, 1, ...) you want to select: ")
selected_key = selections[int(select)]
print("You selected " + repr(selected_key))

打印

0 '1500448188'
1 '1500448187'
Enter the index (0, 1, ...) you want to select: 1
You selected '1500448187'