Python Forum

Full Version: connection string issue
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi I am new to programming
Have be using python and trying things out.
Now trying to connect to my mssql 2016 database

The issue is he use of the \ character in the server name

Server=DesktopBne\sqlexpress

The other issue is the use of ! in the password
Password=sql4python!123!

Tried using the r option at front of string and various " and ' to get the character as not a line continuation

What is the syntax to get the character as the \ ?

Racone
racone Wrote:The issue is he use of the \ character in the server name
Can you post the actual python code that you tried, so that we can see what the issue actually is?
import pyodbc
conn = pyodbc.connect('Driver={SQL Server};'
'Server=DESKTOP-BNIP8AB\\SQLEXPRESS;'
'Database=TestDemoe;'
'Trusted_Connection=yes;')

>>> conn = pyodbc.connect("Driver={SQL Server};"
"Server=DESKTOP-BNIP8AB\\SQLEXPRESS;"
"Database=Jiwa2010;"
"Trusted_Connection=yes;")
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
conn = pyodbc.connect("Driver={SQL Server};"
pyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open database "Jiwa2010" requested by the login. The login failed. (4060) (SQLDriverConnect); [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open database "Jiwa2010" requested by the login. The login failed. (4060)')
>>>