Writing data to a SQL Server - python

So I am trying to write a data frame to Microsoft SQL Server using the pandas to_sql function.
I have created an engine using
engine = sqlalchemy.create_engine(
'mssql:///Server/Database?driver=SQL Server Native Client 11.0'
)
con = engine.connect()
switchers.to_sql('check',engine)
The error I am getting is as follows :
OperationalError: (pyodbc.OperationalError) ('08001', '[08001] [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [2]. (2) (SQLDriverConnect); [08001] [Microsoft][SQL Server Native Client 11.0]Login timeout expired (0); [08001] [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (2)') (Background on this error at: http://sqlalche.me/e/e3q8)
Any idea what I should be looking for?

Your connection string should be:
engine =
sqlalchemy.create_engine('mssql+pyodbc://Server/Database?driver=SQL+Server+Native+Client+11.0')

Related

SQL Alchemy create_engine connection string won't work

I'm trying to write to MSSQL using SQL Alchemy but can't get the connection to work. I've got
engine = create_engine('mssql+pyodbc://<Username>#localhost:5432/<DBName>?driver=SQL+Server+Native+Client+11.0')
df.to_sql('<TableName>', con=engine, if_exists='append')
This gives:
OperationalError: (pyodbc.OperationalError) ('08001', '[08001] [Microsoft][SQL Server Native Client 11.0]TCP Provider: No connection could be made because the target machine actively refused it.\r\n (10061) (SQLDriverConnect); [08001] [Microsoft][SQL Server Native Client 11.0]Login timeout expired (0); [08001] [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (10061)')
(Background on this error at: http://sqlalche.me/e/13/e3q8)
I've looked at the error documentation and tried several different things with the connection string but nothing seems to work. Reading from the same database using pyodbc works fine:
con = pyodbc.connect('Driver={SQL Server};'
'Server=<Server>;'
'Database=<DBName>;'
'Trusted_Connection=yes;')
df = pd.read_sql_query('select * from <DBName>.dbo.<TableName>', con)

Timing out while trying to connect to sql in Azure Managed Instance from Azure VM

I am trying to access tables in SQL database in Azure Managed Instance (with IP: xxxx.database.windows.net) from a python script in Azure VM machine but I am getting the Operational Error below. I have tried with 2 different ways below.
Error:
OperationalError: ('08001', '[08001] [Microsoft][SQL Server Native Client 11.0]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) (SQLDriverConnect); [08001] [Microsoft][SQL Server Native Client 11.0]Login timeout expired (0); [08001] [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (10060)')
1st way with connectionString:
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine("mssql+pyodbc://<username>:<password>#<server>/<database>?driver=SQL+Server+Native+Client+11.0")
query = "select * from table"
df=pd.read_sql(query,engine)
2nd way with connectionString:
import pyodbc
server = 'xxx.database.windows.net'
database = 'database'
username = 'username'
password = 'password'
driver= '{SQL Server Native Client 11.0}'
with pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password) as conn:
with conn.cursor() as cursor:
cursor.execute("SELECT TOP 3 name, collation_name FROM sys.databases")
row = cursor.fetchone()
while row:
print (str(row[0]) + " " + str(row[1]))
row = cursor.fetchone()
Besides, I have also tried to change the driver to drivers below, still no luck.
{ODBC Driver 11 for SQL Server}
{ODBC Driver 13 for SQL Server}
{ODBC Driver 17 for SQL Server}
{SQL Server Native Client 11.0}
Interesting part is, if I try the connect with the same connection string from on-premise machine which is not Azure VM (ex: my local machine or other servers I can RDP to), I can access the database. But when I try on a Azure VM machine, it is timing out. Do you have any ideas how to fix this problem?
Thank you for inputs.
So in the end we foud out that firewall caused this problem. We need to check firewall rules first.

connecting python to sql server using pyodbc

How to connect python to SQL Server using pyodbc until now I wrote the below script but the system crash and display error:
I tried to connect the script to the localhost sql server and i had a successful connection but when I tried on the server on the same network it did crash
code:
import pyodbc
import pandas as pd
conn = pyodbc.connect(
ENGINE='sql_server.pyodbc',
driver='SQL Server',
NAME='testDB',
SERVER ='WIN-CMUH9TBNL53',
DSN='pythonDSN',
PORT='1433',
UID='test',
PWD="test",
)
cursor = conn.cursor()
sql_query=pd.read_sql_query('select * from testDB.dbo.t1',conn)
print(sql_query)
print(type(sql_query))
error:
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (17) (SQLDriverConnect); [08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()). (53); [08001] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0)')

Error on using pyodbc for connecting to a remote sql server

I'm using pyodbc for being able to connect from my local machine to a remote server which has installed sql server 2012.
My code looks like:
conn = pyodbc.connect('Driver={SQL Server Native Client 11.0};'
'Server=myremoteserver;'
'Database=mydb;'
'Username=myusername;'
'Password=mypass;'
)
cursor = conn.cursor()
cursor.execute('SELECT * FROM mydb.mytable')
for row in cursor:
print(row)
I got the following error:
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][SQL Server Native Client 11.0]TCP Provider: An existing connection was forcibly closed by the remote host.\r\n (10054) (SQLDriverConnect); [08001] [Microsoft][SQL Server Native Client 11.0]Client unable to establish connection (10054); [08001] [Microsoft][SQL Server Native Client 11.0]Invalid connection string attribute (0)')
I even tried with the port in connection string, both ways, but the same error encountered.
conn = pyodbc.connect('Driver={SQL Server Native Client 11.0};'
'Server=myremoteserver;'
'Port=1433;'
'Database=mydb;'
'Username=myusername;'
'Password=mypass;'
)
conn = pyodbc.connect('Driver={SQL Server Native Client 11.0};'
'Server=myremoteserver,1443;'
'Database=mydb;'
'Username=myusername;'
'Password=mypass;'
)
A connection between the remote server and my local machine could be made within SSMS, and also using the file explorer.
Do you have any suggestion regarding how could be solved?

Connecting to SQL server using credentials in Python

I ran into this issue where I couldn't connect to SQL server using the credentials .
I can connect when using trusted connection = yes .
Am I doing something wrong here ? Should something be added or concidered ?
conn = pyodbc.connect('Driver={SQL Server Native Client 11.0};'
'Server=1070010-01;'
'Database=test_DB;'
'Uid =sa;'
'Pwd =SDTK-1111;'
)
I have also tried replacing Uid and Pwd with username and password .
I have also tried adding trusted_connection = no
In all the above cases , I get this error :
conn = pyodbc.connect('Driver={SQL Server Native Client 11.0};'
pyodbc.InterfaceError: ('28000', "[28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user ''. (18456) (SQLDriverConnect); [28000] [Microsoft][SQL Server Native Client 11.0]Invalid connection string attribute (0); [28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user ''. (18456); [28000] [Microsoft][SQL Server Native Client 11.0]Invalid connection string attribute (0)")
try this , string was not well formatted
conn = pyodbc.connect('Driver={SQL Server Native Client 11.0}; Server=1070010-01; uid=sa; pwd=SDTK-1111; Database = test_DB; Trusted_Connection=No;')

Categories