Solved!!!
I am trying to connect with a local SQL Server database using the Python framework 'pyodbc' in an Ubuntu machine. But I get over and over the same error.
As background I have to add that I first createn locally a database with the 'sqlcmd' application and it works good in the terminal. I can access by this way.
sqlcmd -S 127.0.0.1 -U SA
But when I try access by Python scrip and I get the error.
Here is my code:
import pyodbc
details = {
'server' : '127.0.0.1',
'database' : 'testDB',
'username' : 'SA',
'password' : 'password'
}
connect_string = 'DRIVER={{ODBC Driver 17 for SQL Server}};SERVER={server};PORT=1443; DATABASE={database};UID={username};PWD={password})'.format(**details)
connection = pyodbc.connect(connect_string)
print(connection)
This is a simple code to star working with the framework, however I got the next error output in jupyter notebook:
InterfaceError: ('28000', "[28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'SA'. (18456) (SQLDriverConnect)")
Solved!!
driver = "ODBC Driver 17 for SQL Server"
server = "127.0.0.1"
database = "testDB"
username = "sa"
password = "pwd"
print ("'DRIVER={" + str (driver) + "};SERVER=" + str (server) + ";DATABASE=" + str (database) + ";UID=" + str (username) + ";PWD=" + str (password) + "'")
import pyodbc
conn = pyodbc.connect ('DRIVER={ODBC Driver 17 for SQL Server};SERVER=127.0.0.1;DATABASE=testDB;UID=sa;PWD=pwd')
getcon = conn.cursor ()
print ("Connected to Database!")
Now with this syntax works!!