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)
Related
I have a Python program which is trying to connect to a MySQL 8.31 database. This is the code I am using.
connection_string = "DRIVER={ODBC Driver 17 for SQL Server};SERVER=localhost;DATABASE=%s;UID=root;PWD=password" % db_name
connection_url = URL.create("mssql+pyodbc", query={"odbc_connect": connection_string})
engine = create_engine(connection_url)
pd.read_sql('select * from apps_list', engine)
I get this exception
sqlalchemy.exc.OperationalError: (pyodbc.OperationalError) ('08001',
'[08001] [Microsoft][ODBC Driver 17 for SQL Server]Named Pipes
Provider: Could not open a connection to SQL Server 1. (2)
(SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL
Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 17
for SQL Server]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:
https://sqlalche.me/e/14/e3q8)
I have researched this exception and most people recommend using SQL Server Configuration manager which I do not seem to have on my system.
Not in Computer Management
Not in C:\Windows\SysWOW64
Others have said to ensure Named Pipes are allowed which they are
And finally I've ensured Ports 3306 and 1433 are open
Yet I still get this exception.
For the record I am able to connect using this code
mydb = pymysql.connect(
host="localhost",
database=db_name,
user="root",
password="****",
autocommit=True
)
mycursor = mydb.cursor()
engine = create_engine('mysql+pymysql://****#localhost/%s' % db_name )
However this gives me a warning and tells me to use SQLAlchemy:
userwarning: pandas only support SQLAlchemy
connectable(engine/connection) ordatabase string URI or sqlite3 DBAPI2
connectionother DBAPI2 objects are not tested, please consider using
SQLAlchemy warnings.warn(
I am slightly confused at this because I am using SQLAlchemy to create the connection on this line
engine = create_engine('mysql+pymysql://****#localhost/%s' % db_name )
Is it fine the way I am currently connecting? Please let me know how I should connect.
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)')
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?
I have searched the web all day tried many solutions but none works. I can manually connect to this server but not with Python I get this 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()). (5); [08001]
[Microsoft][ODBC SQL Server Driver]Invalid connection string attribute
(0)')
I have tried the following:
A)
cnx = pyodbc.connect(Driver='{SQL Server}',
SERVER=self.DBserver,
DATABASE=xyz,
username=self.DBusername,
password=self.DBpassword,
PORT = 1433)
B)
cnx = pyodbc.connect('''Driver={SQL Server};
SERVER=tcp:<self.DBserver>;
PORT=self.DBPort; DATABASE=xyz;
UID=self.DBusername;
PWD=self.DBpassword''')
C)
cnx = = pyodbc.connect(Driver='{SQL Server}',
SERVER=self.DBserver,
DATABASE=xyz,
UID=self.DBusername,
PWD=self.DBpassword)
The issue was with firewall. it is working now
I'm guessing you need something more like this:
cnx = pyodbc.connect('DRIVER={SQL Server};SERVER='+self.DBserver+';DATABASE=xyz;UID='+self.DBusername+';PWD='+self.DBpassword)
I get the same error when trying to connect to Azure SQL Server with pyodbc (4.0.30) when connected via VPN and running in Windows Subsystem for Linux:
with pyodbc.connect(db_connect_string) as conn:
OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: Error code 0x274C (10060) (SQLDriverConnect)')
The Python code is running in WSL (5.10.16.3-microsoft-standard-WSL2) / Ubuntu 20.04.3, openssl version shows: OpenSSL 1.1.1k 25 Mar 2021
I could successfully connect with the same credentials via:
MS Data Studio with VPN
same Python/pyodbc code in Powershell with VPN
same Python/pyodbc code in WSL without VPN (plus adding a firewall rule)
My guess is there must be some interaction between OpenSSL and our] VPN, possibly related to these issues.
https://github.com/mkleehammer/pyodbc/issues/853
https://github.com/microsoft/msphpsql/issues/1112
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')