【发布时间】:2014-07-07 14:50:02
【问题描述】:
我的代码现在有一些问题。一方面,我有有效的代码,但我认为我可以改进。但是,我尝试改进代码的尝试是……灾难性的。这是我的原始代码:
overall_list = []
customer_list = []
if remote_system_id == 'hibiscus' or remote_system_id is None:
hibiscus_cursor.execute(hibiscus_full_SQLstring, hibiscus_parameters)
row = hibiscus_cursor.fetchone()
resultCounter = 0
while row and resultCounter < remote_system_max_results:
customer_list.append({'Hibiscus Customer Number': str(row[0])})
row = hibiscus_cursor.fetchone()
resultCounter += 1
overall_list.append(customer_list)
customer_list = []
if remote_system_id == 'coxcomb' or remote_system_id is None:
coxcomb_cursor.execute(coxcomb_full_SQLstring, coxcomb_parameters)
row = coxcomb_cursor.fetchone()
resultCounter = 0
while row and resultCounter < remote_system_max_results:
customer_list.append({'Coxcomb Customer Number': str(row[0])})
row = coxcomb_cursor.fetchone()
resultCounter += 1
overall_list.append(customer_list)
以上是有效的代码,但有人告诉我要对其进行更多改进。具体来说,我有两个单独的例程来生成输出。我被告知要合并它,但结果并不理想:
customer_list = []
hibiscus_cursor.execute(hibiscus_full_SQLstring, hibiscus_parameters)
coxcomb_cursor.execute(coxcomb_full_SQLstring, coxcomb_parameters)
row = coxcomb_cursor.fetchone()
resultCounter = 0
while row and resultCounter < remote_system_max_results:
customer_list.append({'Hibiscus Customer Number': str(row[0])})
row = coxcomb_cursor.fetchone()
customer_list.append({'Coxcomb Customer Number': str(row[0])})
row = hibiscus_cursor.fetchone()
resultCounter += 1
上面的代码是我尝试改进它,但没有好的结果。关于为什么我会收到这样的错误的任何想法:
customer_list.append({'Coxcomb Customer Number': str(row[0])})
TypeError: 'NoneType' object has no attribute '__getitem__'
编辑:
更多信息。这是两个独立的数据库,彼此完全不相关。它们唯一可能的关系是它们都包含客户信息。像这样连接:
cnxn = pyodbc.connect(HIBISCUS_CONNECTION)
hibiscus_cursor = cnxn.cursor()
cnxn = pyodbc.connect(COXCOMB_CONNECTION)
coxcomb_cursor = cnxn.cursor()
我正在查询两个数据库,我希望找到类似的内容:
WHERE firstname LIKE 'adam'
在第一个代码中,它工作得非常好。在第二个中,我得到了上面的错误。
【问题讨论】: