簡體   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