繁体   English   中英

如何在 Debian 上将 MYSQLdb 表导出为 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 '';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM