如何df.to在Azure中使用SQL Server执行www.example.com _sql

dwbf0jvd  于 2023-01-31  发布在  SQL Server
关注(0)|答案(1)|浏览(145)

我可以df.to在我的SQL Server本地示例上执行www.example.com _slq。当我尝试使用Python和Azure SQL Server执行相同的df.to_sll时,我会卡住。我认为它基本上是这样完成的。

import urllib.parse
params = urllib.parse.quote_plus(
    'Driver=%s;' % '{ODBC Driver 17 for SQL Server}' +
    'Server=%s,1433;' % 'ryan-server.database.windows.net' +
    'Database=%s;' % 'ryan_sql_db' +
    'Uid=%s;' % 'UN'  +
    'Pwd={%s};' % 'PW' +
    'Encrypt=no;' +
    'TrustServerCertificate=no;'
    )

from sqlalchemy.engine import create_engine
conn_str = 'mssql+pyodbc:///?odbc_connect=' + params
engine = create_engine(conn_str)
        
connection = engine.connect()
connection

all_data.to_sql('health', engine, if_exists='append', chunksize=100000, method=None,index=False)

这给了我这个错误。

OperationalError: (pyodbc.OperationalError) ('08S01', '[08S01] [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.\r\n (10060) (SQLExecDirectW); [08S01] [Microsoft][ODBC Driver 17 for SQL Server]Communication link failure (10060)')
[SQL: INSERT INTO health ([0], [Facility_BU_ID], [Code_Type], [Code], [Description], [UB_Revenue_Code], [UB_Revenue_Description], [Gross_Charge], [Cash_Charge], [Min_Negotiated_Rate], [Max_Negotiated_Rate], etc., etc., etc.

我发现这个链接今天:

https://learn.microsoft.com/en-us/sql/machine-learning/data-exploration/python-dataframe-sql-server?view=sql-server-ver15

我也试过做类似的事,像这样。

import pyodbc
import pandas as pd
df = all_data
# server = 'myserver,port' # to specify an alternate port
server = 'ryan-server.database.windows.net'
database = 'ryan_sql_db' 
username = 'UN' 
password = 'PW' 
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
# Insert Dataframe into SQL Server:
for index, row in df.iterrows():
     cursor.execute(all_data.to_sql('health', cnxn, if_exists='append', chunksize=100000, method=None,index=False))
cnxn.commit()
cursor.close()

当我运行它的时候,我得到了这个错误。

DatabaseError: Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': ('42S02', "[42S02] [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'sqlite_master'. (208) (SQLExecDirectW); [42S02] [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared. (8180)")

我真正希望的是df.to_sql,而不是Insert Into。我在Spyder工作,试图将数据从我的本地机器发送到云。

g2ieeal7

g2ieeal71#

我读了下面的两个链接,并得到了它的工作。

基本上,你需要在本地机器上打开命令窗口,输入“ipconfig”,获取两个IP地址,然后将它们输入Azure中的SQL Server。

EXECUTE sp_set_database_firewall_rule 
N'health', 
'192.0.1.1', 
'192.0.0.5';

最后,在SQL Server中运行下面的小脚本,以确认更改是否正确。

USE [ryan_sql_db]
GO
SELECT * FROM sys.database_firewall_rules
ORDER BY modify_date DESC

相关问题