Mari0 Mari0 - 6 months ago 54
SQL Question

Function sequence error (0) (SQLFetch) - SQL, Pyodbc

for _item in file_list:
for col in cursor.execute("select column1,column2 from tbl"):
if _item[0]==col.column1:
#I also tried using a different cursor, still doesn't work
var1 = cursor.execute("select column2 from tbl where column1=?", _item[0])
for i in var1: var2 = i[0]
cursor3.execute("insert into tbl(column2,column1,column3,column4) values (?,?,?,?)", (var1, _item[0],_item[1],_item[2]))
except IOError as error:
print error

What i am trying to do is compare the values in a list and the ones from column1, if they match, get the value from column2 on the same row, then add a new line with the value from column2 but different values for the remaining ones.

But unfortunately it doesen't work the way i did it, when running the code above python throws the error:

Error: ('HY010', '[HY010] [Microsoft][ODBC Driver Manager] Function sequence error (0) (SQLFetch)')

In line:

for columnrow in cursor.execute("select column1,column2 from tbl")


You need to retrieve the results of the query using, for example, .fetchall()

So instead of that line you could do:

for columnrow in cursor.execute("select column1,column2 from tbl").fetchall():