我有兴趣将mysql select语句导出到samba共享上的文件中。一切正常,直到我尝试更新文件。例如,我有mysql导出到outfile peachtree.csv,当数据发生变化时,我希望它用新数据覆盖旧文件,但是,我收到错误:ERROR 1086(HY000):File'/ srv / samba /share/peachtree.csv'已经存在。是否有一个选项或开关可以放入语句中强制或将其写入现有文件?
SELECT * FROM peachtree,
INTO OUTFILE '/srv/samba/share/peachtree.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
答案 0 :(得分:5)
你做不到。这是一项安全功能。否则,你可能会 选择/ etc / password或/ etc / shells等文件。
从手册:
SELECT的SELECT ... INTO OUTFILE'file_name'形式写入 选定的行到文件。该文件是在服务器主机上创建的,所以 您必须具有FILE权限才能使用此语法。 file_name不能 是一个现有的文件,其中包括防止文件等 / etc / passwd和数据库表被销毁。
答案 1 :(得分:0)
此解决方案使用动态SQL并写入Windows文件系统。
Select Now() Into @Now; # get DateTime
Set @Now = Replace(@Now, ':', '-'); # format DateTime for the filesystem
# set Destination filename using formatted DateTime
Set @FilNam = Concat("'", 'd:/Folder/qrySelectToFile ', @Now, '.TXT', "'");
#Select @FilNam; # debug
# build query to execute, one clause at a time, for encoding purposes
Set @Sel = 'Select * From vewViewName Into OutFile ' ; # Select From Into
Set @FldsTrm = ' Fields Terminated By ","' ; # Fields Terminated
Set @FldsEnc = Concat(" Enclosed By '", Char(34 Using utf8), "'") ; # Enclosed. 'Using' clause is to avoid a blob of 34 spaces.
Set @LinsTrm = " Lines Terminated By '\\r\\n'" ; # Lines Terminated
Set @Qry = Concat(@Sel, @FilNam, @FldsTrm, @FldsEnc, @LinsTrm, ';'); # all together now
Prepare Stmt From @Qry; # Prepare
Execute Stmt ; # Execute
Deallocate Prepare Stmt ; # Done