I'm trying to connect to an oracle database with Pyodbc:
pyodbc.connect('{Microsoft ODBC for Oracle};Server=serverxzy.com:1234;Uid=myusername;Pwd=pass123')
I get the following error message:
pyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
Any suggestions how to fix it would be appreciated. I'm specifically interested in getting pyodbc to work and NOT cx_Oracle.
You have to use the proprietary library for Oracle, cx_Oracle, and you must have the Oracle client and SDK installed.
Once this is all set up you can simply:
import cx_Oracle
conn_str = 'USER/PASS#HOSTNAME:PORT/ALIAS'
conn = cx_Oracle.connect(conn_str)
Then you can create a cursor with the conn object:
c = conn.cursor()
And then you can execute SQL:
c.execute(SQL)
Consider specifying the DRIVER in connection string:
pyodbc.connect('DRIVER={Microsoft ODBC for Oracle};Server=serverxzy.com:1234;
Uid=myusername;Pwd=pass123')
Related
I have already looked up a lot of some what similar questions on StackOverflow regarding the connectivity of MSSQL with pyodbc, but none of their solutions helped.
I'm tring to connect a MSSQL database which is lying on a VM server, and I'm trying to access it from my local system. Following is the code:
import pyodbc
server = '172.xxx.xxx.xxx,1443'
database = 'sample_db'
username = 'SA'
password = 'xxxxx'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+password+';Trusted_Connection=yes')
cursor = cnxn.cursor()
cursor.execute("SELECT name FROM sys.databases;")
results = cursor.fetchall()
print(results)
The error I'm getting is as follows:
pyodbc.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
If there's any other alternative to pyodbc that works better with MSSQL, please do mention that too.
Any help is appreciated, thanks.
Edit:
Upon #GordThompson suggestion I checked the driver in pyodbc.drivers() and found that my system only has 'SQL Server' driver so I changed the Driver to SQL Server. The present code looks like this:
import pyodbc
server = '172.xxx.xxx.xxx,1443'
database = 'sample_db'
username = 'SA'
password = 'xxxxx'
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+password+';Trusted_Connection=yes')
cursor = cnxn.cursor()
cursor.execute("SELECT name FROM sys.databases;")
results = cursor.fetchall()
print(results)
But now i'm getting a totally different error, still not sure what it is
Error:
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (17) (SQLDriverConnect); [08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()). (53)')
Its important to not mention the Trusted_Connection parameter when logging-in using UID and PWD. Hence, when I removed the Trusted_Connection parameter, it was able to establish the connection successfully.
So the connection string that later worked for me just had these,
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+password)
cursor = cnxn.cursor()
[ Since #GordThompson didn't post it as an answer, I'm positing it here and closing it. Thanks #GordThompson ]
I have been running Python script using my desktop at work that successfully connects to a remote desktop server and outputs data in SQL via pyodbc.connect.
I am looking to migrate this code to a separate remote desktop PC recently installed at work and I get the following error:
InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
The code I am using is:
cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
"Server=AUBAMTRAS01-DEV;"
"Database=ForwardTrading;"
"Username=xxxxxxxx;"
"Password=yyyyyyyy;"
"Trusted_Connection=yes;")
cursor = cnxn.cursor()
cursor.execute('SELECT distinct(Commodity) from ForwardCurvesOilAndGas')
for row in cursor:
print('row = %r' % (row,))
Data source name not found and no default driver specified
This means you need to install the SQLDriverConnect driver on your second machine.
SOLVED - Driver required was 'SQL Server' not 'SQL Server Native Client 11.0'
I am writing a script that needs to make a connection to a microsoft access .accdb file and extract data from it.
Here is a snippet of what I am doing?
import pypyodbc
pypyodbc.lowercase = False
conn = pypyodbc.connect(
r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};' +
r'DBQ=file.accdb;'
)
cur = conn.cursor()
I get the following error:
raise Error(state,err_text)
pypyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified')
I have installed the drivers for ODBC as instructed in https://learn.microsoft.com/en-us/sql/connect/python/pyodbc/step-1-configure-development-environment-for-pyodbc-python-development
But the error persists, any idea what should I do or where I am going wrong?
I want to write a python dataframe (df_EVENT5_13) to SQL Server version 17 using SQL Alchemy. In order to do this I have to make an "engine" connection.
Let's say hypothetically below are the credentials I have for SQL Server:
Driver = {SQL Server}
Server = ABCD14GPBVTSI88\ABCDRD22,4421
Database = DB4
uid = abcde
pwd = 12345678
Below is the code I'm trying to run and the corresponding error:
import sqlalchemy as sa
import pyodbc
engine = sa.create_engine('mssql+pyodbc://abcde:12345678#ABCD14GPBVTSI88\ABCDRD22,4421/DB4')
df_EVENT5_13.to_sql("MOD_test", engine)
Below is the error I get:
DBAPIError: (pyodbc.Error) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
I've read all the faq pages from the python odbc library as well as other examples and managed to connect to the DSN, using the following code:
cnxn = pyodbc.connect("DSN=DSNNAME")
cursor = cnxn.cursor()
cursor.tables()
rows = cursor.fetchall()
for row in rows:
print row.table_name
but for everything else I keep getting this error:
Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
I know that I can pull up my data using Microsoft Access by going through the following steps: Creating a new database, clicking the external data tab, Click More and select ODBC database, use the Link to the data source by creating a linked table, in the Select data source window choosing Machine Data source and select NAME2 which has a System type, press okay and choose the table acr.Table_one_hh, then select the fields in the table that I want to look at like City, State, Country, Region, etc. When I hover over the table name it shows the DSN name, Description, Trusted Connection = Yes, APP, Database name and the table name.
I've attempted two methods, first
cnxn = pyodbc.connect('DRIVER={SQL Server Native Client 10.0};SERVER=mycomputername;DATABASE=mydatabase;Trusted_Connection=yes;')
cursor = cnxn.cursor()
which gives an error:
Error: ('08001', '[08001] [Microsoft][SQL Server Native Client 10.0]Named Pipes Provider: Could not open a connection to SQL Server [2]. (2) (SQLDriverConnect); [HYT00] [Microsoft][SQL Server Native Client 10.0]Login timeout expired (0); [08001] [Microsoft][SQL Server Native Client 10.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (2)')
I tried
cnxn = pyodbc.connect("DSN=DSNNAME, DATABASE=mydatabase")
cursor = cnxn.cursor()
cursor.execute("""SELECT 1 AS "test column1" from acr.Table_one_hh""")
cursor.tables()
rows = cursor.fetchall()
for row in rows:
print row.table_name
which gave an error
Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
I managed to solve my issue. My code did not really change.
cnxn = pyodbc.connect("DSN=BCTHEAT")
cursor = cnxn.cursor()
cursor.execute("select * from acr.Table_one_hh")
row = cursor.fetchall()
then I wrote the results into a csv file.