首页 > 解决方案 > SQL Alchemy,pymssql,Pandas 0.24.2 to_sql 试图在表已经存在时创建表

问题描述

我正在尝试使用 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

错误

(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:

标签: pythonsql-serverpandassqlalchemypymssql

解决方案


如果我理解正确,您正在尝试将 pandas 数据帧上传到已经存在的 SQL 表中。然后你只需要创建一个与 sql alchemy 的连接并将你的数据写入表中:

 import pyodbc
 import sqlalchemy
 import urllib
 from sqlalchemy.pool import NullPool
 serverName = 'Server_Name'
 dataBase = 'Database_Name'
 conn_str = urllib.parse.quote_plus(
            r'DRIVER={SQL Server};SERVER=' + serverName + r';DATABASE=' + dataBase + r';TRUSTED_CONNECTION=yes')
 conn = 'mssql+pyodbc:///?odbc_connect={}'.format(conn_str) #IF you are using MS Sql Server Studio
 engine = sqlalchemy.create_engine(conn, poolclass=NullPool)
 connection = engine.connect()
 sql_table.to_sql('Your_Table_Name', engine, schema='Your_Schema_Name', if_exists='append', index=False,
                  chunksize=200)
 connection.close()
 

推荐阅读