pymssql InterfaceError: Connection is closed, but only in a loop(!)
pymssql InterfaceError: Connection is closed, but only in a loop(!)
我正在开发一个 ETL(提取、转换、加载)工具来自动化一些办公室工作。每个员工都有一个单独的数据集;抓取一个网站(将员工 ID 编号作为输入变量)给了我一个数据集。
主要函数如下所示:
for emp_number in all_emp_numbers:
#scrape html from website:
web_element_scraper.main_function(str(emp_number))
time.sleep(5)
# take scraped html from one html file, clean it, and place the cleaned html in
# another file:
web_element_cleaner.main_function('results_cache.html','detagged_markup.html')
time.sleep(5)
# convert html table into a set of nested lists:
table_values = table_converter.desired_table
time.sleep(3)
# upload nested lists (i.e., a "list of lists" into MS SQL Server 2008 database:
db_upload.main_function(str(emp_number))
time.sleep(5)
print("ETL process completed.")
已导入所有相关模块等
这是我的问题开始的地方: 如果我 运行 这个脚本用于任何单个员工编号,脚本执行正常,并且值出现在 MS SQL 服务器数据库 table 如预期。
但是,如果我有多个员工编号并且脚本必须多次 运行,第一次迭代(看似)按预期工作 - 值已成功上传到 table- 但是下一次迭代,无论是哪个员工号,都不会!
基于一些调试和测试,我发现问题仅限于 SQL 服务器连接 - 即,对于后续迭代,我收到此错误:
pymssql.InterfaceError: Connection is closed.
我真的不知道如何解决这个问题,特别是因为连接在第一次迭代中工作正常。
编辑添加: 这是 db_upload.py 模块:
import pymssql
import credentials_page
import table_converter
import time
db_connect = pymssql.connect(
server = credentials_page.database_server,
user = credentials_page.database_username,
password = credentials_page.database_password,
database = credentials_page.database_name
)
def main_function(emp_id):
my_cursor = db_connect.cursor()
data_table = table_converter.desired_table
# adjust date format:
for data_row in data_table:
data_row[0] = data_row[0][:26]
data_row.append(emp_id)
for individual_line in data_table:
my_cursor.execute("INSERT INTO [db_table_name] VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)", tuple(individual_line))
db_connect.commit()
time.sleep(3)
db_connect.close()
time.sleep(2)
print("Data uploaded.")
您不能提交多条记录的原因是因为在您的函数中您正在调用 .close() 函数,该函数会关闭连接并要求您 运行 在函数内部进行连接,方式你现在已经构建了它,当你导入函数时它只被调用一次。
查看pymssql中的相关文档here
我正在开发一个 ETL(提取、转换、加载)工具来自动化一些办公室工作。每个员工都有一个单独的数据集;抓取一个网站(将员工 ID 编号作为输入变量)给了我一个数据集。
主要函数如下所示:
for emp_number in all_emp_numbers:
#scrape html from website:
web_element_scraper.main_function(str(emp_number))
time.sleep(5)
# take scraped html from one html file, clean it, and place the cleaned html in
# another file:
web_element_cleaner.main_function('results_cache.html','detagged_markup.html')
time.sleep(5)
# convert html table into a set of nested lists:
table_values = table_converter.desired_table
time.sleep(3)
# upload nested lists (i.e., a "list of lists" into MS SQL Server 2008 database:
db_upload.main_function(str(emp_number))
time.sleep(5)
print("ETL process completed.")
已导入所有相关模块等
这是我的问题开始的地方: 如果我 运行 这个脚本用于任何单个员工编号,脚本执行正常,并且值出现在 MS SQL 服务器数据库 table 如预期。
但是,如果我有多个员工编号并且脚本必须多次 运行,第一次迭代(看似)按预期工作 - 值已成功上传到 table- 但是下一次迭代,无论是哪个员工号,都不会!
基于一些调试和测试,我发现问题仅限于 SQL 服务器连接 - 即,对于后续迭代,我收到此错误:
pymssql.InterfaceError: Connection is closed.
我真的不知道如何解决这个问题,特别是因为连接在第一次迭代中工作正常。
编辑添加: 这是 db_upload.py 模块:
import pymssql
import credentials_page
import table_converter
import time
db_connect = pymssql.connect(
server = credentials_page.database_server,
user = credentials_page.database_username,
password = credentials_page.database_password,
database = credentials_page.database_name
)
def main_function(emp_id):
my_cursor = db_connect.cursor()
data_table = table_converter.desired_table
# adjust date format:
for data_row in data_table:
data_row[0] = data_row[0][:26]
data_row.append(emp_id)
for individual_line in data_table:
my_cursor.execute("INSERT INTO [db_table_name] VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)", tuple(individual_line))
db_connect.commit()
time.sleep(3)
db_connect.close()
time.sleep(2)
print("Data uploaded.")
您不能提交多条记录的原因是因为在您的函数中您正在调用 .close() 函数,该函数会关闭连接并要求您 运行 在函数内部进行连接,方式你现在已经构建了它,当你导入函数时它只被调用一次。
查看pymssql中的相关文档here