1

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!!

Salvador Vigo
  • 397
  • 4
  • 16
  • What do the SQL Logs say? Can you see the connection attempt, and if so what is the error? Also, I *really* hope you aren't giving your application `sysadmin` access. – Thom A Feb 18 '19 at 16:29
  • It is not the real sysadmin access. And I don't know where exactly find the Logs for the connection attempt, since I am running a jupyter notebook and the only error I got is the one in my question. What I did was, first create a database locally from terminal. Now I am trying to access and work with this database from a Python script, but it is not working. I am missing something but I don't now where – Salvador Vigo Feb 18 '19 at 16:57
  • The SQL Server logs will be on the SQL Server. And that error message typically means that the password is wrong. How have you verified that the password your application uses is correct? – Brian Feb 18 '19 at 17:22
  • I solve it. The syntax was wrong somehow. I wrote the same with different syntax and now it works.. – Salvador Vigo Feb 19 '19 at 10:41

0 Answers0