我想将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?
答案 0 :(得分:1)
我建议您尝试将您保存在肯定具有写入权限的目录中,例如/ 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 '';