NotImplementedError:executemany仅针对简单的INSERT语句实现

时间:2017-05-18 09:02:00

标签: python-3.x pandas sqlalchemy vertica

我尝试使用sqlalchemy

通过pandas附加我的vertica(SQL类型)表
import pandas as pd
import sqlalchemy as sa

创建引擎到vertica:

def get_engine(base):
    engine = sa.create_engine("{sys}+{dri}://{user}:" + \
                               "{password}@{host}:{port}/{database}".format(**login[base]))
    return engine
engine = get_engine('vertica')

为了清楚起见,一个简单的查询:

table = '***'

sql =\
'''
select *
from public.{table}
'''.format(table=table)

connection = engine.connect()
data = pd.read_sql(sql, connection)
connection.close()

数据不为空:

print(len(data))
569955

并尝试写入同一张表:

fields = list(data.columns)
connection = engine.connect()
data.to_sql(table, connection, schema='public', index=False, if_exists='append', chunksize=30000,
            dtype={fields[0]:sa.types.Integer,
            fields[1]:sa.types.VARCHAR,
            fields[2]:sa.types.Integer,
            fields[3]:sa.types.Integer,
            fields[4]:sa.types.Integer,
            fields[5]:sa.types.VARCHAR,
            fields[6]:sa.types.VARCHAR,
            fields[7]:sa.types.VARCHAR,
            fields[8]:sa.types.VARCHAR,
            fields[9]:sa.types.VARCHAR,
            fields[10]:sa.types.VARCHAR,
            fields[11]:sa.types.VARCHAR,
            fields[12]:sa.types.DateTime
           })
connection.close()

并犯了这个错误:

...
\Anaconda3\lib\site-packages\sqlalchemy\engine\default.py in do_executemany(self, cursor, statement, parameters, context)
    465 
    466     def do_executemany(self, cursor, statement, parameters, context=None):
--> 467         cursor.executemany(statement, parameters)
    468 
    469     def do_execute(self, cursor, statement, parameters, context=None):

\Anaconda3\lib\site-packages\vertica_python\vertica\cursor.py in executemany(self, operation, seq_of_parameters)
    153         else:
    154             raise NotImplementedError(
--> 155                 "executemany is implemented for simple INSERT statements only")
    156 
    157     def fetchone(self):

NotImplementedError: executemany is implemented for simple INSERT statements only

1 个答案:

答案 0 :(得分:1)

当我尝试使用sqlalchemy将数据写入vertica时,出现了相同的错误。就我而言,问题是列名。似乎无法写出包含特殊字符的列名。我可以通过从pandas的列名称中删除所有的'_','%'和空格字符来解决错误,然后使用df.to_sql()将其写入vertica。