'how to get intergers from pyodbc's fetch?
I am trying to use the update statement, but my database won't accept (int, ) only int.
import pyodbc
connect_data = (
r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};
r'DBQ=C:\filename.accdb;'
)
cnxn = pyodbc.connect(connect_data)
crsr = cnxn.cursor()
crsr.execute('select ID from WeatherAnalisis')
weathertable = crsr.fetchmany(5)
for rowcount in weathertable:
timeCET = timeCET + timeincrement
print (rowcount)
sql = "UPDATE WeatherAnalisis SET Time_UTC = ? WHERE ID = ?"
crsr.execute(sql, timeCET, rowcount)
cnxn.commit()
>>>>
(1, )
crsr.execute(sql, timeCET, rowcount)
pyodbc.Error: ('HY004', '[HY004] [Microsoft][ODBC Microsoft Access Driver]Invalid SQL data type (67) (SQLBindParameter)')
This seems to be because the rowcount is in the (int, ) format instead of int
now my question is: How do I get it to give me just the interger?
Solution 1:[1]
.fetchmamy() returns a list of pyodbc Row objects. A Row object behaves like a tuple, so if it only contains one column you can extract it as a scalar value like so:
for row in weathertable:
rowcount = row[0]
Solution 2:[2]
I figured something out myself
rowiercount = str(rowcount)
rowiercount.replace("(", "").replace(")", "").replace(" ", "").replace(",", ""))
It most likely isn't effectient, but it seems to do the job I will not yet mark this as solved because if someone else has this issue to, there probably is a better way to do this.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gord Thompson |
| Solution 2 | Blazing Blast |
