开发者

Escaping special characters in filepaths using SQLAlchemy

开发者 https://www.devze.com 2023-03-21 03:15 出处:网络
I\'m storing filenames and filepaths in MySQL.Retrieving them from the database using LIKE expressions requires that I escape all allowed filename chars that collide with MySQL special chars.I\'m happ

I'm storing filenames and filepaths in MySQL. Retrieving them from the database using LIKE expressions requires that I escape all allowed filename chars that collide with MySQL special chars. I'm happy to simply use Python's string.replace() method, but was wondering if there was a more standard or built-in method of sanitizing filepaths with SQLAlchemy or dealing with filepaths in MySQL in general.

I need the solution to be OS-agnostic and established. It does not need to be implemented in SA开发者_如何学Python. I'll accept any procedure for encoding that works; failing that, I need a list of all chars that need to be escaped and a smart choice of an escape char.


SQLAlchemy do sanitize for you if you will use regular queries. Maybe the problem that you use like clause. Like require addition escape for such symbols: _%. Thus you will need replace methods if you want to quote like expression.


Why do you need to escape the file paths? As far as you are not manually writing select / insert queries, SQLAlchemy will take care of the escaping when it generates the query internally.

The file paths can be inserted as they are into the database.


As I know there isn’t what you are looking for in SQLAlchemy. Just go basestring.replace() method by yourself.


You don't need do anything SQLAlchemy will do it for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消