通过PYODBC检索行

2024-09-27 00:21:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码现在有些问题。一方面,我有一些可以工作的代码,但我认为我可以改进。然而,我试图改进代码的尝试是…灾难性的。这是我的原始代码:

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)

上面的代码是有效的,但我被告知要对其进行进一步完善。具体来说,我有两个独立的例程来生成输出。我被告知要合并,但结果并不理想:

^{pr2}$

上面的代码是我试图改进它,但没有得到好的结果。你知道我为什么会犯这样的错误吗

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'

在第一个代码中,它工作得非常好。在第二个例子中,我得到了上面的错误。在


Tags: 代码idremotecustomersystemcursorlistrow
1条回答
网友
1楼 · 发布于 2024-09-27 00:21:02

通过以下更改修复:

customer_list = []
hibiscus_cursor.execute(hibiscus_full_SQLstring, hibiscus_parameters)
coxcomb_cursor.execute(coxcomb_full_SQLstring, coxcomb_parameters)
row = hibiscus_cursor.fetchone()
row2 = coxcomb_cursor.fetchone()
resultCounter = 0
while (row or row2) and resultCounter < remote_system_max_results:
    if hibiscus_cursor.fetchone() is not None:
        customer_list.append(str(row[0]))
        row = hibiscus_cursor.fetchone()
    if coxcomb_cursor.fetchone() is not None:
        customer_list.append(str(row2[0]))
        row2 = coxcomb_cursor.fetchone()
    resultCounter += 1

代码效率低下。可能没用。在

相关问题 更多 >

    热门问题