Error connecting to SQL Server using pyodbc

Asked

Viewed 120 times

0

I am trying to connect to SQL Server database using pyodbc lib in Python, but I am encountering error right from the start.

Follow the code and error below.

Script:

import pyodbc

conn = pyodbc.connect('Driver = {SQL SERVER Native Client 11.0};'
'Server = DESKTOP-3TFNKAR;'
'Database = Aula1;'
'Trusted_connection = yes;')

cursor = conn.cursor()

cursor.execute('''

CREATE TABLE Professor (
    Nome varchar(100),
    Idade varchar(10),
    Email varchar(100)
)

''')

conn.commit()

Error:

conn = pyodbc.connect('Driver = {SQL SERVER Native Client 11.0};'
pyodbc.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Nome da fonte de dados n�o encontrado e nenhum driver padr�o especificado (0) (SQLDriverConnect)')

I made the modification of 'Driver = {SQL Server}; for 'Driver = {SQL SERVER Native Client 11.0}; as an attempt, because from what I understood my driver is this.

Anyway, I’m very beginner, so explanations are also welcome.

Gift!!

1 answer

2


Use string without spaces, and also confirm that you have the driver installed:

pyodbc.connect('DRIVER={SQL Native Client};SERVER=DESKTOP-3TFNKAR;DATABASE=Aula1;Trusted_connection=yes');

For a better view:

conn = pyodbc.connect('Driver={SQL SERVER Native Client};
                       Server=DESKTOP-3TFNKAR;
                       Database=Aula1;
                       Trusted_connection = yes;');

If you want to test with ODBC driver, can change driver to DRIVER={ODBC Driver 17 for SQL Server}.

Note that, ODBC sometimes requires registering the name of the data source using the "ODBC Data Source Administrator" on Windows

  • Decided to change the driver to what you suggested. Thank you so much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.