使用 SQLAlchemy 转义文件路径中的特殊字符
我将文件名和文件路径存储在 MySQL 中。使用 LIKE 表达式从数据库中检索它们要求我转义所有允许与 MySQL 特殊字符冲突的文件名字符。我很高兴简单地使用 Python 的 string.replace() 方法,但想知道是否有更标准或内置的方法来使用 SQLAlchemy 清理文件路径或处理 MySQL 中的文件路径。
我需要一个与操作系统无关且已建立的解决方案。它不需要在 SA 中实现。我会接受任何有效的编码程序;如果做不到这一点,我需要一个需要转义的所有字符的列表以及一个转义字符的明智选择。
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. 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您将使用常规查询,SQLAlchemy 会为您进行清理。也许是你使用like子句的问题。就像需要对此类符号进行附加转义一样:_%。因此,如果您想引用类似的表达式,则需要替换方法。
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.
为什么需要转义文件路径?只要您不手动编写选择/插入查询,SQLAlchemy 将在内部生成查询时处理转义。
文件路径可以按原样插入数据库中。
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.
据我所知,SQLAlchemy 中没有您想要的东西。自己去
basestring.replace()
方法就可以了。As I know there isn’t what you are looking for in SQLAlchemy. Just go
basestring.replace()
method by yourself.您不需要做任何事情,SQLAlchemy 会为您做的。
You don't need do anything SQLAlchemy will do it for you.