【问题标题】:MySQL Export into outfile and overwriteMySQL导出到outfile并覆盖
【发布时间】:2013-01-02 13:12:56
【问题描述】:

我有兴趣将 mysql select 语句导出到 samba 共享上的文件中。在我尝试更新文件之前一切正常。例如,我将 mysql 导出到 outfile peachtree.csv 并且当数据更改时,我希望它用新数据覆盖旧文件,但是,我收到错误:错误 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';

【问题讨论】:

标签: mysql export overwrite


【解决方案1】:

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

来自手册:

SELECT ... INTO OUTFILE 'file_name' 形式的 SELECT 写入 选定的行到文件。该文件是在服务器主机上创建的,因此 您必须具有 FILE 权限才能使用此语法。文件名不能 是现有文件,除其他外,它会阻止文件,例如 /etc/passwd 和数据库表不会被破坏。

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

【讨论】:

    【解决方案2】:

    此解决方案使用动态 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 override existing file?的其他答案

    【讨论】:

      猜你喜欢
      • 2010-10-31
      • 2012-02-26
      • 2014-06-20
      • 2015-06-15
      • 2012-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多