使用SSMS从Python输出表上载数据并将其保存在数据库中
我一直在微软SQL Server管理工作室(MicrosoftSQLServerManagementStudio)附近修修补补,试图了解它是如何工作的。现在,我正试图制作一个程序,使用Python将数据更新到SQL server中,并每天更新数据,因为我从一个站点(每天更新)获取数据。它每天都会创建单独的数据库,而且几乎会自动创建 到目前为止,我已经编写了一个python代码,它解析页面并从页面中删除表格数据并输出表格(在.csv文件中,因为我不想丢失数据)。此外,已经与数据库建立了连接,studio服务器运行python代码使用SSMS从Python输出表上载数据并将其保存在数据库中,python,sql-server,python-3.x,pandas,Python,Sql Server,Python 3.x,Pandas,我一直在微软SQL Server管理工作室(MicrosoftSQLServerManagementStudio)附近修修补补,试图了解它是如何工作的。现在,我正试图制作一个程序,使用Python将数据更新到SQL server中,并每天更新数据,因为我从一个站点(每天更新)获取数据。它每天都会创建单独的数据库,而且几乎会自动创建 到目前为止,我已经编写了一个python代码,它解析页面并从页面中删除表格数据并输出表格(在.csv文件中,因为我不想丢失数据)。此外,已经与数据库建立了连接,stu
import pandas as pd
from bs4 import BeautifulSoup
import requests
from pandas import DataFrame
url = 'https://www.pgcb.org.bd/PGCB/?a=pages/hourly_generation_loadshed_display.php' # url name
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser') # will get the data from the url
tables = soup.find_all('table')
table = tables[1]
def get_data ():
headers = table.find_all('th') # tables had the tag th
columns = [ td.text for td in headers ]
df = pd.DataFrame()
rows = table.find_all('tr')
for row in rows:
tds = row.find_all('td')
data = [ td.text for td in tds ]
temp_df = pd.DataFrame([data])
df = df.append(temp_df, sort=True).reset_index(drop=True)
df = df.dropna(axis=1,how='all')
df = df.dropna(axis=0,how='all')
df.columns = columns
df = df[:-1]
return df
if __name__ == "__main__":
dataresponse = get_data()
export_csv = dataresponse.to_csv (r'D:\Work\data\test2.csv', index = None, header = True)
print (dataresponse.to_string())
上面的代码显示了python代码,该代码解析页面中的表,提取细节并将其保存在文件中
结果是:
Date Time Generation Demand Shortage Loadshed Remark
1 14-10-2019 07:00:00 7340.8 7341 0 0
2 14-10-2019 06:00:00 7648.2 7648 0 0
3 14-10-2019 05:00:00 7968 7968 0 0
4 14-10-2019 04:00:00 8077 8077 0 0
5 14-10-2019 03:00:00 8346 8346 0 0
6 14-10-2019 02:00:00 8577 8577 0 0
7 14-10-2019 01:00:00 9065 9065 0 0
8 13-10-2019 24:00:00 9490 9490 0 0
9 13-10-2019 23:00:00 9778 9778 0 0
10 13-10-2019 22:00:00 10216 10216 0 0
11 13-10-2019 21:00:00 10666 10666 0 0
12 13-10-2019 20:00:00 10897 10897 0 0
13 13-10-2019 19:30:00 11128 11128 0 0
14 13-10-2019 19:00:00 11219 11219 0 0 Evening Peak
15 13-10-2019 18:00:00 10330 10330 0 0
16 13-10-2019 17:00:00 8748 8748 0 0
17 13-10-2019 16:00:00 8938.9 8939 0 0
18 13-10-2019 15:00:00 9248.9 9249 0 0
19 13-10-2019 14:00:00 9077.1 9077 0 0
20 13-10-2019 13:00:00 9133.8 9134 0 0
21 13-10-2019 12:00:00 8904 8904 0 0 Day Peak
22 13-10-2019 11:00:00 8669 8669 0 0
23 13-10-2019 10:00:00 8495.3 8495 0 0
24 13-10-2019 09:00:00 8167.7 8168 0 0
25 13-10-2019 08:00:00 7684.1 7684 0 0
26 13-10-2019 07:00:00 7508.5 7509 0 0
[Finished in 4.2s]
然后我建立了与服务器的连接,这仍然会产生一些问题(但我正在处理)
我需要在SQL server中获得结果并以表格格式输出,这些结果将保存在数据库中(每天都有单独的数据库)。您是在尝试从SQL server运行python代码,还是仅从python更新SQL?您已经有了一个库可以执行后者,只需搜索一个如何从python执行更新的示例。我正在尝试从SQL server运行python代码,因为这将从SQL server更新数据库。没有理由将整个内容放在SQL server中,您可以非常轻松(可能更简单)地从外部更新SQL。这就是数据库的全部概念,允许应用程序更新数据。
import pyodbc
import time
retry_flag = True
retry_count = 0
while retry_flag and retry_count < 10:
# throw an exception, which will try to connect (if failed) after every 1 sec
try:
conn = pyodbc.connect ('DRIVER = {SQL Server};'#Driver Name
'server = ####;' #Server Name
'database = ;' #database Name
'uid = ###;' # Username
'pwd = ###;') #Password
retry_flag = False
cur = connection.cursor ()
except:
print ("Please retry after 1 sec")
retry_count += 1
time.sleep (1)
EXEC sp_execute_external_script @language = N'Python',
@script = N'