Reputation: 21
when using mysql select statements in python,there is a ValueError pointing that 'Y' (0x59)
Upvotes: 1
Views: 1451
Reputation: 56
For anyone running into the problem that OPs pre- and post-change (point 3) commands are the same:
For me, the solution is to escape the percentage signs using another percentage sign:
DATE_FORMAT(CREATE_TIME, '%Y-%m-%d')
Becomes:
DATE_FORMAT(CREATE_TIME, '%%Y-%%m-%%d')
Excecuting SQL statement in MariaDB with SQLAlchemy engine (through pandas).
Upvotes: 1
Reputation: 21
The reasons for this, and the solutions are as follows
(1) Rrror message: ValueError: unsupported format character 'Y' (0x59) at index 146
(2) Cause: because the sql executed by python has a writing method similar to DATE_FORMAT(CREATE_TIME, '%Y-%m-%d'). Where %Y conflicts with python's parameter %s
(3) Solution: change DATE_FORMAT(CREATE_TIME, '%Y-%m-%d') to DATE_FORMAT(CREATE_TIME, '%Y-%m-%d').
(4) Some friends commented that if the SQL is put into the string and then put into the execution, you need to add another layer: DATE_FORMAT(CREATE_TIME, '%%%%Y-%%%%m-%%%%d')
Upvotes: 1