Python:无法从SQL转换列表

Python:无法从SQL转换列表,python,Python,您好,我正在学习Python,现在我想从SQL导入一些数据。不幸的是,我有一个我无法解决的问题。我收到以下错误消息: cursor.executeselect*自matchinfo,其中matchid='%i'% Matchid类型错误:%i格式:需要数字,而不是pyodbc.Row 我想问题是列表数据没有格式化为整数,但是当我尝试执行诸如Matchid[0]=intMatchid[0]之类的操作,然后打印出Matchid[0]时,它也不起作用。因此,我不确定如何解决这个问题。代码如下,请提前感

您好,我正在学习Python,现在我想从SQL导入一些数据。不幸的是,我有一个我无法解决的问题。我收到以下错误消息:

cursor.executeselect*自matchinfo,其中matchid='%i'% Matchid类型错误:%i格式:需要数字,而不是pyodbc.Row

我想问题是列表数据没有格式化为整数,但是当我尝试执行诸如Matchid[0]=intMatchid[0]之类的操作,然后打印出Matchid[0]时,它也不起作用。因此,我不确定如何解决这个问题。代码如下,请提前感谢

import pyodbc

cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=DESKTOP-    
FCDHA0J\SQLEXPRESS;DATABASE=Data')

cursor = cnxn.cursor()

cursor.execute("select matchid from matchinfo where matchid > 1 order by dato asc")

Matchinfo = cursor.fetchall()


for Matchid in Matchinfo:

    print(Matchinfo[0])

    cursor.execute("select * from matchinfo WHERE matchid = '%i'" % Matchid)
您正在尝试将Pyodbc.Row强制转换为整数。要解决此问题,只需进行以下更改:

cursor.execute("select * from matchinfo WHERE matchid = %s", (Matchid[0],))
为什么你需要fetchall,你需要离线工作还是什么

只要这样做:

cursor.execute("select matchid from matchinfo where matchid > 1 order by dato asc")

for Matchid in Matchinfo:
    match_id = Matchid[0]
    cursor.execute("select * from matchinfo WHERE matchid = '%i'" % match_id)
如果表很大,您将获得一些性能,因为您一次检索一个结果。另一方面,您正在将所有结果一次提取到内存中。

您正在尝试将Pyodbc.Row转换为整数。要解决此问题,只需进行以下更改:

cursor.execute("select * from matchinfo WHERE matchid = %s", (Matchid[0],))
为什么你需要fetchall,你需要离线工作还是什么

只要这样做:

cursor.execute("select matchid from matchinfo where matchid > 1 order by dato asc")

for Matchid in Matchinfo:
    match_id = Matchid[0]
    cursor.execute("select * from matchinfo WHERE matchid = '%i'" % match_id)
如果表很大,您将获得一些性能,因为您一次检索一个结果。另一方面,您可以一次将所有结果提取到内存中