0
To summarize the connection process, I’m trying to create a function to connect the data bunch, but I’m not able to make the connection as expected:
def conectar_com_banco(usuario):
if usuario in 'illuminati':
username = 'illuminati'
password = 'fnord'
elif usuario in 'fascist_firewall':
username = 'tor'
password = 'onion'
else:
print('usuario_nao_encontrado')
import pyodbc
cnxn = pyodbc.connect('DRIVER = SQL Server; SERVER = conspiracy; DATABASE = illuminati; UID='+username+';PWD='+password)
cnxn.cursor()
return(cursor)
cursor=conectar_com_banco('illu')
cursor.execute("SELECT @@version;")
row = cursor.fetchone()
print(row)
When I run this code, I get the following reply:
Traceback (most recent call last):
File "<ipython-input-4-d96fceb45081>", line 18, in <module>
cursor=conectar_com_banco('illu')
File "<ipython-input-4-d96fceb45081>", line 14, in conectar_com_banco
cnxn = pyodbc.connect('DRIVER = SQL Server; SERVER = conspiracy; DATABASE = illuminati; UID='+username+';PWD='+password)
InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Nome da fonte de dados não encontrado e nenhum driver padrão especificado (0) (SQLDriverConnect)')
When the right answer would be the version of my database as occurs in the reference: https://docs.microsoft.com/pt-br/sql/connect/python/pyodbc/step-3-proof-of-concept-connecting-to-sql-using-pyodbc?view=sql-server-2017 , I used as an example to make the connection simple.
Resolution:
The error occurred in:
cnxn = pyodbc.connect('DRIVER = SQL Server; SERVER = conspiracy; DATABASE = illuminati; UID='+username+';PWD='+password)
So I rewrote it as follows:
def conectar_com_banco(usuario):
if usuario in 'illuminati':
server = 'conspiracy'
database = 'illuminati'
username = 'illuminati'
password = 'fnord'
elif usuario in 'fascist_firewall':
server = 'conspiracy'
database = 'illuminati'
username = 'tor'
password = 'onion'
else:
print('funcao_nao_encontrado')
import pyodbc
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cur=cnxn.cursor()
return(cur)
cursor=conectar_com_banco('illu')
cursor.execute("SELECT @@version;")
row = cursor.fetchone()
print(row)
And returned to me the expected:
('Microsoft SQL Server 2005 - 9.00.5000.00 (X64) \n\tDec 10 2010 10:38:40 \n\tCopyright (c) 1988-2005 Microsoft Corporation\n\tEnterprise Edition (64-bit) on Windows NT 6.1 (Build 7601: Service Pack 1)\n', )
I haven’t figured out the syntax error I must have made.
PS.: Both {ODBC Driver 13 for SQL Server} and {SQL Server} worked.
What was your intention in doing
if usuario in 'illuminati'
?– Woss
Be able to declare:
usuario='illu'
andusuario in 'illuminati'
and soOut[3]: True
.– Márcio Mocellin
If you enter the
else
, what the values ofusername
andpassword
?– Woss
The function has several
elif
before theelse
, but I removed it to simplify it.– Márcio Mocellin
Okay, let’s try to simplify. If you go into
else
you only display a message and continue the execution of the function; in that case there will be no variablesusername
andpassword
. Wouldn’t it be the case to stop the execution if you enter theelse
? Also, what is going on in your program? Is there an error? Which one? Or if there is an unexpected behavior, what is it and what would be expected? Please answer all this directly in the question by entering [Dit].– Woss