简体   繁体   中英

Python - how to get rid of escape sequences in sql query execution

I am creating a sql query in python of the sort:

select lastupdatedatetime from auth_principal_entity where lastupdateddatetime < '02-05-16 03:46:51:527000000 PM'

When it is executed, there are escape sequences that are being added which doesn't return me answers.

Although when we print it in stdout, it looks perfect, but for python's understanding it has escape sequences which I don't want in the execution command

'select lastupdatedatetime from auth_principal_entity where lastupdateddatetime < \\'02-05-16 03:50:14:388000000 PM\\''

The escape sequences won't cause any problem in the cursor.execute(query) The real issue lies in the date that is being sent as a string is being used to compare and return values from db which are in date-object format.

so something like this should work.

query = "SELECT LASTUPDATEDDATETIME FROM AUTH_PRINCIPAL_ENTITY WHERE LASTUPDATEDDATETIME < to_date('03-May-16', 'dd-mon-yy')"

Or

date_ = datetime.datetime.now().strftime('%d-%b-%y')
query = "SELECT LASTUPDATEDDATETIME FROM AUTH_PRINCIPAL_ENTITY WHERE LASTUPDATEDDATETIME < to_date('{}', 'dd-mon-yy')".format(date_)

Try that. Should work for you :-)

For me, I wrap my SQL statements in triple quotes so it doesn't run into these issues when I execute:

query = """
    select lastupdatedatetime from auth_principal_entity where lastupdateddatetime < '02-05-16 03:46:51:527000000 PM'
    """

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM