Connecting to SQL server using credentials in Python - 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;')

Related

Escape # char in psycopg SQLalchemy [duplicate]

This works:
db = pyodbc.connect('driver={SQL Server Native Client 11.0}; server=172.30.0.194; database=db;uid=someuser; pwd=fancy#password')
This doesn't
cn_string = "mssql+pyodbc://someuser:"fancy&password"#172.30.0.194/db?driver=SQL+Server+Native+Client+11.0"
return create_engine(cn_string)
This doesn't either:
driver = "SQL Server Native Client 11.0"
server = "192.30.0.194"
database = "EPM_Dashboard"
uid = "someuser"
pwd = "fancy#password"
params = f'DRIVER={{{driver}}};SERVER={server};DATABASE={database};UID={uid};PWD={{{pwd}}};'
connection_string = 'mssql+pyodbc:///?odbc_connect=%s' % urllib.parse.quote_plus(params)
return create_engine(connection_string)
I get something like:
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. (53)
which would be more believable if the pyodbc item failed.
Here's another failure:
return create_engine(urllib.parse.quote_plus('driver={SQL Server Native Client 11.0}; server=172.30.0.194; database=EPM_Dashboard;uid=someuser; pwd=fancy#password'))
I'm sure there's a tricky character somewhere I'm missing.
Here are some resources
https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-databases
Special character in SQL password
SqlAlchemy equivalent of pyodbc connect string using FreeTDS
If you need to construct a connection URI that may have "funny characters" in it then you can use engine.URL.create() to build it for you:
import sqlalchemy as sa
connection_uri = sa.engine.URL.create(
"mssql+pyodbc",
username="someuser",
password="fancy#password",
host="192.30.0.194",
database="EPM_Dashboard",
query={"driver": "SQL Server Native Client 11.0"},
)
print(connection_uri)
# mssql+pyodbc://someuser:fancy%40password#192.30.0.194/EPM_Dashboard?driver=SQL+Server+Native+Client+11.0

I can't connect to SQL Server database from Python

I need to connect to a SQL server database from Python. In this case, I am a non-administrator Windows user.
This is the code that I used:
import pyodbc
conn = pyodbc.connect('Driver={SQL Server};'
'Server=XXXXXXXX;'
'Database=XXX;'
'Trusted_Connection=yes;'
'port = 1433;')
But this error message appear:
Traceback (most recent call last):
File "<ipython-input-1-c018c40360dc>", line 3, in <module>
conn = pyodbc.connect('Driver={SQL Server};'
ProgrammingError: ('42000', '[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open database "XXX" requested by login. Login failed. (4060) (SQLDriverConnect); [42000 ] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0);[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open database "XXX" requested by login . Login failed. (4060); [42000] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0)')
This is the Conection Section in my SQL Server Manangement Studio:
The image description:
Server Type: Database Engine
Server Name: XXXXX
Authentication: Windows Authentication
Username: SERVERNAME / username
Password: (empty)
What am i making wrong?

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)

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?

Writing data to a SQL Server

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')

Categories