If I try to retrieve a value from a column which typed as UUID I get a Python error: TypeError("a bytes-like object is required, not 'str'")
when I call fetchall()
on the CursorObject
.
Code that raises the error:
import sqlalchemy as sa
def getScheduleData(engine: sa.engine, scheduleID: str) -> dict:
rows = []
metadata = sa.MetaData()
table = sa.Table('schedules', metadata, autoload_with=engine)
stmt = sa.select(table.c['userID']).where(table.c["ID"] == scheduleID)
print(stmt)
try:
with engine.connect() as conn:
result = conn.execute(stmt)
rows = result.fetchall()
print(rows)
except Exception as e:
print(e)
The greater issue is that i want to retrieve all the column values (so a whole row) from the DB because there are several columns which are typed as UUID in the schedules
table:
import sqlalchemy as sa
def getScheduleData(engine: sa.engine, scheduleID: str) -> dict:
rows = []
metadata = sa.MetaData()
table = sa.Table('schedules', metadata, autoload_with=engine)
//all column values
stmt = sa.select(table).where(table.c["ID"] == scheduleID)
print(stmt)
try:
with engine.connect() as conn:
result = conn.execute(stmt)
rows = result.fetchall()
print(rows)
except Exception as e:
print(e)
I found a solution if I only want to retrieve specific values, in this way there is no TypeError:
import sqlalchemy as sa
def getScheduleData(engine: sa.engine, scheduleID: str) -> dict:
rows = []
metadata = sa.MetaData()
table = sa.Table('schedules', metadata, autoload_with=engine)
//cast the value to string in the statement
stmt = sa.select(sa.cast(table.c['userID'], sa.String(36))).where(table.c["ID"] == scheduleID)
print(stmt)
try:
with engine.connect() as conn:
result = conn.execute(stmt)
rows = result.fetchall()
print(rows)
except Exception as e:
print(e)
But how can I achieve this taking the second scenario into consideration where I want all the rows and there can be several UUID typed values?