pythonsqlalchemy

How do I do a "starts with" query using SQLAlchemy?


I am learning to use SQLAlchemy to connect to a MySQL database. I want to pull records from the DB that start with a given string. I know that for simple equality all I need to do is this

queryRes = ses.query(Table).filter(Table.fullFilePath == filePath).all()
result = []

How do I do something like this?

queryRes = ses.query(Table).filter(Table.fullFilePath.startsWith(filePath)).all()
result = []

Maybe the query would look like this?

q = ses.query(Table).filter(Table.fullFilePath.like('path%')).all()

Solution

  • This is the pure SQL:

    SELECT * FROM table WHERE field LIKE "string%"
    

    The SQL alchemy is:

    q = ses.query(Table).filter(Table.fullFilePath.like('path%')).all()