How to use executemany in pyodbc to run multiple SELECT queries

一个人想着一个人 提交于 2021-02-17 02:37:38

问题


Im using PYODBC to query an SQL DB multiple times based on the values of a pandas dataframe column (seen below as a list of values, since I used the ToList() function to turn the column into a list.

#the connection string
cnxn = pyodbc.connect(driver='{SQL Server}', server = 'NameOfTheServer',autocommit = True,uid ='name',pwd ='password')

cursor = cnxn.cursor()
params = ['3122145', '523532236']
sql = ("""
    SELECT  SO.column
    FROM    table AS SO
    WHERE SO.column = ?
    """)
cursor.executemany(sql, params)
row = cursor.fetchone()

the executemany function throws an error even though I'm using a list: TypeError: ('Params must be in a list, tuple, or Row', 'HY000')


回答1:


.executemany is not intended to be used with SELECT statements. If we try, we only get the last row back:

cnxn = pyodbc.connect(connection_string, autocommit=True)
crsr = cnxn.cursor()
crsr.execute("CREATE TABLE #tmp (id int primary key, txt varchar(10))")
crsr.execute(
    "INSERT INTO #tmp (id,txt) "
    "VALUES (1,'one'),(2,'two'),(3,'three'),(4,'four'),(5,'five')"
)
print(crsr.execute("SELECT * FROM #tmp").fetchall())
"""console output:
[(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four'), (5, 'five')]
"""
sql = "SELECT * FROM #tmp WHERE id = ?"
list_of_tuples = [(1,), (3,), (5,)]
crsr.executemany(sql, list_of_tuples)
print(crsr.fetchall())
"""console output:
[(5, 'five')]
"""
try:
    crsr.nextset()
    print(crsr.fetchall())
except pyodbc.ProgrammingError as pe:
    print(pe)
    """console output:
    No results.  Previous SQL was not a query.
    """

Instead, we need to build a string of parameter placeholders and use it in an IN clause, like this:

tuple_of_scalars = tuple(x[0] for x in list_of_tuples)
sql = f"SELECT * FROM #tmp WHERE id IN ({','.join('?' * len(tuple_of_scalars))})"
print(sql)
"""console output:
SELECT * FROM #tmp WHERE id IN (?,?,?)
"""
crsr.execute(sql, tuple_of_scalars)
print(crsr.fetchall())
"""console output:
[(1, 'one'), (3, 'three'), (5, 'five')]
"""


来源:https://stackoverflow.com/questions/64087692/how-to-use-executemany-in-pyodbc-to-run-multiple-select-queries

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!