如何在 Debian 上将 MYSQLdb table 导出为 CSV?

How to export MYSQLdb table to CSV on Debian?

我想将 MYSQLdb 表导出为 .csv 格式。

我试过这个:

connection = MySQLdb.connect(host='localhost',
    user='***',
    passwd='***',
    db='database1',
    use_unicode=True,
    charset="utf8")
cursor = connection.cursor()
query = """ select * 
from example_table1
into outfile 'MYFOLDER'
fields terminated by ';'
enclosed by '"'
lines terminated by '';
"""
cursor.execute(query)
connection.commit()
cursor.close()

我收到此错误消息:

Traceback (most recent call last):
  File "mysql_export_to_csv.py", line 46, in <module>
    cursor.execute(query)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 226, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorvalue
_mysql_exceptions.InternalError: (1, 'Can\'t create/write to file \'/usr/src/Python-2.7.13/output.csv\' (Errcode: 30 "Read-only file system")')

这段代码有什么问题?为什么我无法将其导出为 .csv?

我建议尝试将您的文件保存在您肯定具有写入权限的目录中,例如 /tmp/

像这样:

connection = MySQLdb.connect(host='localhost',
    user='***',
    passwd='***',
    db='database1',
    use_unicode=True,
    charset="utf8")
cursor = connection.cursor()
query = """ select * 
from example_table1
into outfile '/tmp/myfile.csv'
fields terminated by ';'
enclosed by '"'
lines terminated by '';