Sqlalchemy query data, sort according to the data in the field (Chinese) to Pinyin (ascending order)

1.sqlalchemy query data, sort according to the data in the field (Chinese) to Pinyin (ascending order)
2. It is known that there are func.convert and sql.func.CONVERT methods in sqlalchemy, and the import method is

.
from sqlalchemy import func
from sqlalchemy import sql

here"s how I implement it

self.db.query(Table).order_by(
            sql.func.CONVERT(sql.literal_column("VARCHAR(8)"), 
            Table.department_name, sql.literal_column("8"))).all()

result hint:
ProgrammingError: (_ mysql_exceptions.ProgrammingError) (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near "VARCHAR (8), Table.department_name, 8)

I don"t know how to implement it.


there is a referenced link https://stackoverflow.com/que., but it does not solve the problem


self.db.query(Table).order_by(
            sql.func.CONVERT(sql.literal_column('department_name as gbk'))
            ).all()

that should do it. Sql.literal_column is essentially a method that passes arbitrary code into a method that goes back and forth to func.

Menu