用Python将MySQL查询结果复制到tempfile

用Python将MySQL查询结果复制到tempfile,python,mysql,postgresql,temporary-files,Python,Mysql,Postgresql,Temporary Files,我对SQL世界有点陌生,但我遵循了一个名为的教程。问题是,我正在处理一个大数据集,类似于教程中的示例,我需要一种更快的方法来执行查询并将其转换为数据帧。在那里,他们使用这个功能: def read_sql_tmpfile(query, db_engine): with tempfile.TemporaryFile() as tmpfile: copy_sql = "COPY ({query}) TO STDOUT WITH CSV {head}".format(

我对SQL世界有点陌生,但我遵循了一个名为的教程。问题是,我正在处理一个大数据集,类似于教程中的示例,我需要一种更快的方法来执行查询并将其转换为数据帧。在那里,他们使用这个功能:

def read_sql_tmpfile(query, db_engine):
    with tempfile.TemporaryFile() as tmpfile:
        copy_sql = "COPY ({query}) TO STDOUT WITH CSV {head}".format(
           query=query, head="HEADER"
        )
        conn = db_engine.raw_connection()
        cur = conn.cursor()
        cur.copy_expert(copy_sql, tmpfile)  # I want to replicate this
        tmpfile.seek(0)
        df = pandas.read_csv(tmpfile)
        return df
我试着复制它,就像这样:

def read_sql_tmpfile(query, connection):
    with tempfile.TemporaryFile() as tmpfile:
        copy_sql = "COPY ({query}) TO STDOUT WITH CSV {head}".format(
           query=query, head="HEADER"
        )

        cur = connection.cursor()
        cur.copy_expert(copy_sql, tmpfile)
        tmpfile.seek(0)
        df = pandas.read_csv(tmpfile)
        return df

问题是,
cursor.copy\u expert
来自PostgreSQL的
psycopg2
库,我找不到一种方法来使用
pymysql
做同样的事情。有没有办法做到这一点?我该怎么办?感谢

他们已经提到,这是postgres特有的功能,非常感谢@waynetech,特别是第二个链接正是我需要阅读的内容。他们已经提到,这是postgres特有的功能,非常感谢@waynetech,特别是第二个链接正是我需要阅读的内容