4

我有兴趣将 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';
4

2 回答 2

5

你不能。这是一项安全功能。否则,您也许可以选择进入 /etc/password 或 /etc/shells 等文件。

从手册:

SELECT ... INTO OUTFILE 'file_name' 形式的 SELECT 将选定的行写入文件。该文件是在服务器主机上创建的,因此您必须具有 FILE 权限才能使用此语法。file_name 不能是现有文件,这可以防止诸如 /etc/passwd 和数据库表之类的文件被破坏。

http://dev.mysql.com/doc/refman/5.0/en/select.html

于 2013-07-04T18:03:54.013 回答
-1

此解决方案使用动态 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

MySQL INTO OUTFILE 的其他答案会覆盖现有文件吗?

于 2019-07-02T19:23:46.863 回答