我正在尝试使用 Pandas 和 Sql Alchemy。这基本上就是我想做的事情。如果我删除该表,它会创建它,但我希望它追加而不需要进行表重命名。我尝试过更新和更改所有库的版本。我很茫然。如果我一开始没有表,它会创建它,然后我再次运行代码,它就会崩溃。错误消息只是说该表已经存在,我知道,这就是为什么我告诉它追加。另外,在加载之前,我正在使用 PYMSSQL 读取数据,并且它可以很好地读取数据帧
Python命令
def writeDFtoSSDatabase(tgtDefiniton,df):
try:
if int(tgtDefiniton.loadBatchSize) > 0:
batchSize = int(tgtDefiniton.loadBatchSize)
else:
batchSize = 1000
#Domain error using SQL Alchemy
logging.debug("Writting Dataframe to SQL Server database")
#hardcoded type beccause that is only type for now
with createDBConnection(tgtDefiniton.tgtDatabaseServer
,tgtDefiniton.tgtDatabaseDatabase
,tgtDefiniton.tgtDatabaseUser
,tgtDefiniton.tgtDatabasePassword,tgtDefiniton.tgtDataType).connect().execution_options(schema_translate_map={
None: tgtDefiniton.tgtDatabaseSchema}) as conn:
logging.debug("Writting DF to Database table {0}".format(tgtDefiniton.tgtDatabaseTable))
logging.debug("ifTableExists: {0}.".format(tgtDefiniton.ifTableExists))
if tgtDefiniton.ifTableExists == "append":
logging.debug('Appending Data')
df.to_sql(tgtDefiniton.tgtDatabaseTable,con=conn,if_exists='append',chunksize = batchSize,index=False)
elif tgtDefiniton.ifTableExists == "replace":
logging.debug('Replacing Table and Data')
df.to_sql(tgtDefiniton.tgtDatabaseTable,con=conn,if_exists='replace',chunksize = batchSize,index=False)
else:
df.to_sql(tgtDefiniton.tgtDatabaseTable,con=conn,if_exists='fail',index=False)
logging.debug("Data wrote to database")
except Exception as e:
logging.error(e)
raise
Error
(Background on this error at: http://sqlalche.me/e/e3q8)
2021-08-30 13:31:42 ERROR (pymssql.OperationalError) (2714, b"There is already an object
named 'test' in the database.DB-Lib error message 20018, severity 16:\nGeneral SQL Server
error: Check messages from the SQL Server\n")
编辑:
日志条目
2021-08-30 13:31:36 DEBUG Writting Dataframe to SQL Server database
2021-08-30 13:31:36 DEBUG create_engine(mssql+pymssql://REST OF CONNECTION INFO
2021-08-30 13:31:36 DEBUG DB Engine Created
2021-08-30 13:31:36 DEBUG Writting DF to Database table test
2021-08-30 13:31:36 DEBUG ifTableExists: append.
2021-08-30 13:31:36 DEBUG Appending Data
2021-08-30 13:31:42 ERROR (pymssql.OperationalError) (2714, b"There is already an object named 'test' in the database.DB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\n")
[SQL: