I am using pyodbc
to connect to a database and extract certain data from it.
Here is my code:
JavaScript
x
24
24
1
con = pyodbc.connect("driver={SQL Server};server= MyServer;database= MyDatabase;trusted_connection=true")
2
3
cursor = con.cursor()
4
5
SQL_command = """
6
SELECT RowID = ISNULL
7
(
8
(
9
SELECT TOP 1 RowID
10
FROM [MyDatabase].[admin].[MyTable]
11
WHERE [queue] = ? and processed IS NULL
12
)
13
,-1
14
)
15
"""
16
17
cursor.execute(SQL_command, queueNumber)
18
19
cursor.commit()
20
21
con.commit()
22
23
result_set = cursor.fetchall()
24
And I got following error after I run above code:
pyodbc.Error: (‘HY010’, ‘[HY010] [Microsoft][ODBC SQL Server Driver]Function sequence error (0) (SQLFetch)’)
May I know what caused such problem, and how can I fix it?
Thanks.
Advertisement
Answer
I believe your problem is the strange commit
statements. You only need to commit
when inserting or updating records not selecting.
JavaScript
1
3
1
cursor.execute(SQL_command, queueNumber)
2
result_set = cursor.fetchall()
3
Also, in the future when using commit,
both cursor.commit
and con.commit
do the same thing, you only need one.
Finally, I’d get used to calling execute with the second arguement as a tuple:
JavaScript
1
2
1
cursor.execute(SQL_command, (queueNumber,))
2
The way you have it works for pyodbc
but is not DB API standard.