I am trying to get a subset of a table from my database. The database is a MySql database.
Python code:
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, VARCHAR, DATETIME, INT, TEXT, TIMESTAMP
from datetime import datetime
from sqlalchemy.orm import sessionmaker
Base = declarative_base()
class TrackablesTable(Base):
__tablename__ = 'Trackables'
trackableId = Column(INT, primary_key=True) #autogenerate
productID = Column(TEXT)
createdOn = Column(TIMESTAMP) #autogenerate
urlTitle = Column(TEXT)
humanTitle = Column(TEXT)
userId = Column(VARCHAR(45))
def __repr__(self):
return "<MyTable(%s)>" % (self.asin)
@staticmethod
def getTrackableByProductId(productID, session):
trackable = session.query(TrackablesTable).filter_by(productID=productID)
return trackable
Note the method at the bottom. I was expecting this method to get me all the rows in the "Trackables" table with a "productID" column with the value of the productID variable. Instead, it seems to be returning a query which is malformed.
The query it returns is below:
SELECT "Trackables"."trackableId" AS "Trackables_trackableId", "Trackables"."productID" AS "Trackables_productID", "Trackables"."createdOn" AS "Trackables_createdOn", "Trackables"."urlTitle" AS "Trackables_urlTitle", "Trackables"."humanTitle" AS "Trackables_humanTitle", "Trackables"."userId" AS "Trackables_userId"
FROM "Trackables"
WHERE "Trackables"."productID" = :productID_1
MySQL workbench is telling me the query is malformed. Further, the value in the query of productID (":productID_1") is not the actual value of the variable referenced in the code.
:productID_1is not a value, it's a placeholder that will be bound to a parameter passed to theexecutefunction. This is how you avoid Little Bobby Tables eating your database. If you turn up the debugging, you can see the value it passes to theexecuteand make sure it's the actualproductIDvalue.