MySQL INTO OUTFILE是否覆盖现有文件? [英] MySQL INTO OUTFILE override existing file?
问题描述
我写了一个大的sql脚本来创建CSV文件.我想每天晚上打电话给cronjob,以创建一个新的CSV文件并在网站上提供该文件.
I've written a big sql script that creates a CSV file. I want to call a cronjob every night to create a fresh CSV file and have it available on the website.
例如,我将文件存储在'/home/sites/example.com/www/files/backup.csv'
Say for example I'm store my file in '/home/sites/example.com/www/files/backup.csv'
我的SQL是
SELECT * INTO OUTFILE '/home/sites/example.com/www/files/backup.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM ( ....
当文件已经存在时,MySQL给我一个错误
MySQL gives me an error when the file already exists
文件'/home/sites/example.com/www/files/backup.csv'已经存在
File '/home/sites/example.com/www/files/backup.csv' already exists
有没有办法使MySQL覆盖文件?
Is there a way to make MySQL overwrite the file?
我可以让PHP检测文件是否存在,并在再次创建它之前将其删除,但是如果我可以直接在MySQL中进行操作,它将更加简洁.
I could have PHP detect if the file exists and delete it before creating it again but it would be more succinct if I can do it directly in MySQL.
推荐答案
否,无法覆盖它.从文档:
file_name不能是现有文件,这尤其可以防止破坏/etc/passwd和数据库表之类的文件.
file_name cannot be an existing file, which among other things prevents files such as /etc/passwd and database tables from being destroyed.
最好每天晚上使用不同的文件名,因为进行多次备份意味着您可以从存在超过一天的问题中恢复.然后,您可以维护一个始终指向最新的完整csv的符号链接.
It might be a better idea to use a different filename each night, as having multiple backups means you can recover from problems that have existed for more than a day. You could then maintain a symlink that always points at the latest complete csv.
这篇关于MySQL INTO OUTFILE是否覆盖现有文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!