在SQL Express中追加到现有表不起作用

时间:2019-01-24 20:28:43

标签: python sql-server python-3.x sqlalchemy pyodbc

您好,我有以下代码用于在SQL Server中附加数据表

def push_to_sql(sql_table_name, dataframe, sql_conn):
    try:
        dataframe.to_sql(con=sql_conn, name=sql_table_name, if_exists='append', index=False,
                     chunksize=200)
        logger.info("Pushed to the dataframe")
    except Exception as ex:
        logger.error(ex)
        logger.warning("Please check SQL connection string or give a valid table name")

及以下是连接字符串格式

def create_sql_engine(login, password, server_name, db_name, driver_name):
    connection_string = 'mssql+pyodbc://' + login + ':' + password + '@' + server_name + '/' + db_name + '?' + 'driver=' + driver_name
    engine = None
    try:
        engine = create_engine(connection_string)
        logger.info("Connected to the database")
    except Exception as ex:
        logger.error(ex)
        logger.warning("Please check the Sql Authentication Parameters")
    return engine

最近我尝试使用此代码将数据推送到SQL Express(我将参数更改为SQL Express), 脚本运行得很好,但是我看不到表格中附加的新数据 (日志文件中也没有错误)。我具有读取和写入SQL Express数据的所有访问权限。有什么我想念的吗?请帮助我。

*****编辑*****

login = 'default'
passw = 'somepassword'
server = 'server.ip\hostname'
db_name = 'TestDB'
driver = 'SQL Server Native Client 11.0'

engine = create_sql_engine(login, passw, server, db_name, driver)

if engine is None:
    logger.warning("Cannot insert data as connection was not established")
else:
    if df_from_file is not None:
        push_to_sql('sample_table', df_from_file, engine)
    else:
        logger.warning("Dataframe is empty or File doesn't have valid encoding ")

这是示例用例,因为我无法透露连接字符串的任何详细信息,我已经验证了n是否选中了它。

0 个答案:

没有答案