Python ';SQLTable';对象没有属性';插入#u语句';

Python ';SQLTable';对象没有属性';插入#u语句';,python,sql,pandas,sqlalchemy,amazon-redshift,Python,Sql,Pandas,Sqlalchemy,Amazon Redshift,我正在工作中安装一台新电脑,在我的另一台电脑上安装了anaconda和其他各种软件包之后,我试图在我的另一台电脑上运行一些工作正常的代码 然而,当尝试使用SQLalchemy导入到redshift时,我遇到了一个新错误,我无法通过google找到任何信息: “SQLTable”对象没有属性“insert\u statement” 这似乎是padas.io.sql的一些问题,但我不知道是什么 以下是代码块: import io from pandas.io.sql import SQLTable

我正在工作中安装一台新电脑,在我的另一台电脑上安装了anaconda和其他各种软件包之后,我试图在我的另一台电脑上运行一些工作正常的代码

然而,当尝试使用SQLalchemy导入到redshift时,我遇到了一个新错误,我无法通过google找到任何信息:

“SQLTable”对象没有属性“insert\u statement”

这似乎是padas.io.sql的一些问题,但我不知道是什么

以下是代码块:

import io
from pandas.io.sql import SQLTable

def _execute_insert(self, conn, keys, data_iter):
    print("Using monkey-patched _execute_insert")
    data = [dict((k, v) for k, v in zip(keys, row)) for row in data_iter]
    conn.execute(self.insert_statement().values(data))

SQLTable._execute_insert = _execute_insert

import pandas as pd
from sqlalchemy import create_engine
from sqlalchemy import text

dbschema='xref'
engine = create_engine('not_showing_you_this_part',
                              connect_args={'options': '-csearch_path={}'.format(dbschema)})
# test
from sqlalchemy import event, create_engine
@event.listens_for(engine, 'before_cursor_execute')
def receive_before_cursor_execute(conn, cursor, statement, params, context, executemany):
    if executemany:
        cursor.fast_executemany = True
        cursor.commit()
# end test
api_start_time = time.time()
print('starting SQL query')
# change yh to the dataframe you want to upload
# under name = : enter in the name of the table you want to create or append to
df.to_sql(name='computer_test', con = engine, if_exists = 'append',index=False)
print('sql insert took: ' + str((time.time() - api_start_time)) + ' seconds')

供参考,monkey patch零件来自:

图像中的完全错误


我一直在寻找答案,结果发现一位男士在评论部分回答了你的问题

我使用了一个非常类似的代码来连接和插入到Redshift。 我犯的错误是使用下面的 conn.execute(self.insert_语句().值(数据))

将上述代码替换为以下代码:

conn.execute(self.table.insert().values(data))

欢迎在评论中回答。

您从中获取猴子补丁的问题有答案。查看接受答案的最后一个代码片段,特别是:
conn.execute(self.table.insert().values(data))