将数据导出为 .sql 格式。如何逃脱?
我正在编写一个导出工具,将输入的 json 数据转换为 sql 语句。
该工具(并且不应该)知道数据库连接,它应该只输出一个可与其他工具一起使用的 .sql 来进行实际导入。
大多数 mysqli->* 和 PDO 相关函数都依赖于开放连接(以确定字符集等内容)。解决这个问题有什么好的方法吗?
I'm writing an export tool that converts input json data to sql statements.
This tool is (and should not) be aware of database connections, it should just output a .sql that can be used with other tools do the actual import.
Most of the mysqli->* and PDO-related functions rely on an open connection (to determine things like the characterset). What's a good way to go about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
MySQL 函数需要连接才能转义字符串的原因是所有
mysql_real_escape_string()
的作用是调用 MySQL 的内置转义函数。但是,如果您阅读其手册页,您会发现它们确实列出了转义的字符:
您不想使用
addslashes() 因为这只转义了几个字符,并且不会提供安全的解决方案,但是您应该能够重新实现由以下方式完成的转义。
mysql_real_escape_string()
使用给定的字符列表,通过简单调用strtr()
或类似的:The reason the MySQL functions require a connection in order to escape the string is that all
mysql_real_escape_string()
does is make a call to MySQL's built-in escaping function.However, if you read the manual page for it, you'll see that they do list the characters which are escaped:
You don't want to use
addslashes()
since that only escapes a few characters, and would not provide a secure solution. But you should be able to re-implement the escaping done bymysql_real_escape_string()
using the list of characters given, with a simple call tostrtr()
or similar:@斯特夫戈瑟林:
mysql_real_escape_string 确实需要连接。
我会选择阻力最小的路线。意味着使用系统调用来执行 mysqldumper >临时文件
@stefgosselin:
mysql_real_escape_string did require a connection.
I would go the line with least resistance. Means use a systemcall to exec mysqldumper > tmpfile
只是一个想法,您的应用程序是否可以生成类似的 sql
并将一组 sql 参数作为数组传递
,然后让应用程序中执行数据库工作的部分在此时进行转义
just a thought, is it possible for you app to generate and sql like
and pass a set of paramaters for the sql as an array
and then have the part of your app that does do database work do the escaping at that point
对于转义,
mysql_real_escape_string
函数是此任务的通常选择,只不过它确实需要连接。另一种选择是addslashes
。我会检查一个根据所需参数(字符集、删除表等)定制的 mysqldump 文件,并将其作为起点。
For escaping,
mysql_real_escape_string
function is the usual choice for this task except it does need a connection. The other alternative would beaddslashes
.I would check out a mysqldump file tailored with the needed paramaters (character sets, drop tables, etc ..) and take it from there as a starting point.