--fields-terminated-by=name
Fields in the output file are terminated by the given
--lines-terminated-by=name
Lines in the output file are terminated by the given
name应包含以下内容之一:
`--fields-terminated-by`
\t或"\""
`--fields-enclosed-by=name`
Fields in the output file are enclosed by the given
SELECT customer_id, firstname, surname INTO OUTFILE '/exportdata/customers.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM customers;
import os
import subprocess
import pymysql.cursors
def get_table_names(cursor: pymysql.cursors.Cursor, database_name) -> list[str]:
"""Returns a list of all the table names in the database"""
with cursor:
cursor.execute(f"SHOW TABLES FROM {database_name};")
tables = cursor.fetchall()
tables = [table[0] for table in tables]
return tables
def save_clean_data(traget_directory: str, mysql_user: str, my_sql_password: str, database_name: str, host: str, cursor: pymysql.cursors.Cursor) -> None:
"""Saves the each table in the database to a csv file"""
os.makedirs(traget_directory, exist_ok=True)
expresion = r"s/\"/\"\"/g;s/'/\'/;s/\t/\",\"/g;s/^/\"/;s/$/\"/;s/\n//g"
for table_name in get_table_names(cursor, database_name):
file_path = os.path.join(traget_directory, f'{table_name}.csv')
if not os.path.exists(file_path):
dump_command = (
f'mysql -B -u {mysql_user} -p{my_sql_password} {database_name} -h {host}'
f' -e "SELECT * FROM {table_name};" | sed "{expresion}" > {file_path}'
)
subprocess.call(dump_command, shell=True)
8条答案
按热度按时间hiz5n14c1#
首先,我可以给予你一个表的答案:
所有这些
INTO OUTFILE
或--tab=tmpfile
(和-T/path/to/directory
)答案的问题在于,它需要在与MySQL服务器相同的服务器上运行**mysqldump*,并具有这些访问权限。我的解决方案是简单地使用
mysql
(notmysqldump
)和-B
参数,用-e
内联SELECT语句,然后用sed
处理ASCII输出,最后得到包含标题字段行的CSV:范例:
“id”,“login”,“password”,“folder”,“email”“8”,“mariana”,“xxxxxxxxxx”,“mariana”,”3”,“squaredesign”,“xxxxxxxxxxxxx”,“squaredesign”,”email protected(https://stackoverflow.com/cdn-cgi/l/email-protection)““4”,“miedziak”,“xxxxxxxx”,“miedziak”,”email protected(https://stackoverflow.com/cdn-cgi/l/email-protection)““5”,“Sarko”,“xxxxxxx”,“Sarko”,“6”,“Logitrans波兰”,“xxxxxxxxxx”,“LogitransPoland ",“7”,“Amos”,“xxxxxxxxxxxxxxxx”,“Amos”,”9”,“Annabelle”,“xxxxxxxxxxxxxx”,“Annabelle”,”11”,“Brandfathers and Sons”,“xxxxxxxxxxxxxxx”,“BrandfathersAndSons”,”“12”,“Imagine Group”,“xxxxxxxxxxxx”,“ImagineGroup”,“13”,”EduSquare.pl“,“xxxxxxxxxxxxx”,“EduSquare.pl”,”101”,“tmp”,“xxxxxxxxxxxxxxxxx”,"_",”email protected(https://stackoverflow.com/cdn-cgi/l/email-protection)“
在这个一行程序的末尾添加一个
> outfile.csv
,以获取该表的CSV文件。接下来,获取一个包含所有表的列表
从那里,只需再做一个循环,例如,在Bash shell中迭代这些表:
在
do
和; done
之间插入我在上面的第1部分中编写的长命令,但将表名替换为$tb
。nhjlsmyf2#
此命令将在 /path/to/directory table_name.sql 和 table_name.txt 中创建两个文件。
SQL文件将包含表创建模式,txt文件将包含mytable表的记录,字段由逗号分隔。
gkl3eglg3#
mysqldump
有CSV格式选项:name
应包含以下内容之一:\t
或"\""
和
--lines-terminated-by
\r
\n
\r\n
当然,你应该mysqldump每个表单独。
我建议您将所有表名收集到一个文本文件中。然后,遍历运行mysqldump的所有表。下面是一个脚本,它将一次转储和gzip 10个表:
8xiog9wr4#
如果你使用的是MySQL或MariaDB,最简单和高性能的方式转储单表的CSV是-
现在可以使用其他技术对多个表重复此命令。请在此处查看更多详细信息:
j2qf4p5b5#
这对我来说很有效:
或者,如果只想转储特定的表:
我转储到
/var/lib/mysql-files/
以避免此错误:mysqldump:Got error:1290:MySQL服务器正在使用--secure-file-priv选项运行,因此在执行“SELECT INTO OUTFILE”时无法执行此语句
lfapxunr6#
看起来其他人也有这个问题,现在是there is a simple Python script,用于将mysqldump的输出转换为CSV文件。
dgenwo3n7#
您也可以使用dbForge Studio for MySQL中的数据导出工具来执行此操作。
它将允许您选择部分或所有表并将其导出为CSV格式。
tzcvj98z8#
下面是一个Python解决方案: