import pypyodbc mySQLServer = "PCC\SQLEXPRESS myDatabase = "northwind" connection = pypyodbc.connect('Driver=(SQL Server);' 'Server=' + mySQLServer + ';' 'Database=' + myDatabase + ';') cursor = connection.cursor() mySQLQuery = (""" SELECT CompanyName, ContactName, Country FROM [Northwind].[dbo].[Customers] WHERE country = 'USA' """) cursor.execute(mySQLQuery) results = cursor.fetchall() print(results) connection.close() 

Errors:

 Traceback (most recent call last): File "B:/Program Files/pyCharm/new1/SQLServer.py", line 10, in <module> 'Database=' + myDatabase + ';') File "B:\Program Files\python\lib\site-packages\pypyodbc.py", line 2454, in __init__ self.connect(connectString, autocommit, ansi, timeout, unicode_results, readonly) File "B:\Program Files\python\lib\site-packages\pypyodbc.py", line 2507, in connect check_success(self, ret) File "B:\Program Files\python\lib\site-packages\pypyodbc.py", line 1009, in check_success ctrl_err(SQL_HANDLE_DBC, ODBC_obj.dbc_h, ret, ODBC_obj.ansi) File "B:\Program Files\python\lib\site-packages\pypyodbc.py", line 985, in ctrl_err raise Error(state,err_text) pypyodbc.Error: ('IM002', '[IM002] [Microsoft][Диспетчер драйверов ODBC] Источник данных не найден и не указан драйвер, используемый по умолчанию') 

I heard that for my version of Python, you need to insert the driver into odbcad32.exe in SysWOW64 , but how to do it step by step? (for example, which tab to put and so on, unless of course the rest of the code is correct)

    1 answer 1

    try to get rid of brackets in mySQLQuery first

     import pypyodbc mySQLServer = "PCC\SQLEXPRESS myDatabase = "northwind" connection = pypyodbc.connect('Driver=(SQL Server);' 'Server=' + mySQLServer + ';' 'Database=' + myDatabase + ';') cursor = connection.cursor() mySQLQuery = """ SELECT CompanyName, ContactName, Country FROM [Northwind].[dbo].[Customers] WHERE country = 'USA' """ cursor.execute(mySQLQuery) results = cursor.fetchall() print(results) connection.close() 

    example for Vertica

     import pypyodbc ds=pypyodbc.dataSources() db = pypyodbc.connect("DSN=HPVertica") cursor = db.cursor() sql="SELECT count(*) from WAREHOUSE.ALL_EVENTS" cursor.execute(sql) rows = cursor.fetchall() for row in rows: print (row) 
    • mySQLQuery = ("" "SELECT CompanyName, ContactName, Country FROM dbo.Customers WHERE country = 'USA'" "") Got rid of the brackets. But what about errors? Their number has not changed - Alex Flow
    • plus did not immediately notice that you forgot to close the quotes mySQLServer = "PCC \ SQLEXPRESS" - Nikolay Baranenko
    • Yes, for some reason it has been erased here, but it is in the original code. But thanks anyway. - Alex Flow
    • And how to work with the driver? How to connect it in Windows and in the code? - Alex Flow
    • use windows administration and register DSN there and then refer to it already in the code - Nikolay Baranenko