Connection to Linked Access Database Using pyodbc - python-3.x

I am trying to get data from a Microsoft Access Database. The issue is the number of constraints I'm under:
I have to use 64 bit Python
The access database is made up of linked tables to a different database
The other database requires a 32 bit Oracle driver
Therefore, I have the Access Database stored locally and am trying to connect to that using PyODBC.
I've tried looking around and messing with the connection string but this problem seems pretty unique.
This is currently a modified version of what I have:
import pyodbc
dbPATH = r'C:\path\to\database.accdb'
UID = 'username'
PWD = 'username'
driver = r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
credentials = r'DBQ=%s;UID=%s;PWD=%s'%(dbPATH, UID, PWD)
conn_str = driver + credentials
connection = pyodbc.connect(conn_str)
cursor = connection.cursor()
cursor.execute("select * from [table_name];")
for row in cursor.fetchone():
print(row)
This is the error I typically get:
pyodbc.Error: ('HY000', "[HY000] [Microsoft][ODBC Microsoft Access Driver] ODBC--connection to '{Oracle in OraClient11g_home1_32bit}' failed. (-2001) (SQLExecDirectW)")
Which is surprising since this is the driver and connection that the access database uses to connect to the other data source.
I have tried setting
pyodbc.pooling = False
but that did not change anything.

You have two constraints that are mutually exclusive:
I have to use 64 bit Python
... and ...
The other database requires a 32 bit Oracle driver
64-bit processes cannot use 32-bit ODBC drivers; they are simply not compatible. If the rest of your stack (Oracle ODBC driver, Microsoft Office/Access application(s)) is 32-bit then you will need to use a 32-bit version of Python if you want to work with the linked tables as you've described.
Additional Note: Your statement that "The other database requires a 32 bit Oracle driver" is dubious. Client-server databases like Oracle, SQL Server, etc., don't particularly care if they receive requests from a 32-bit client or a 64-bit client. There may be differences in the details on the client side, but that's for the ODBC Driver (and/or the ODBC Driver Manager) on the client to figure out.

Related

IBM AS400 File not Found error while the Table Object is still there

I'm relatively new to working with remote IBM databases. I am connecting to a remote IBM AS400 database using an ODBC Python3 connector in an Anaconda virtual environment on Windows 10. My connection that successfully works is:
import pyodbc
connection = pyodbc.connect(
Driver='{iSeries Access ODBC Driver}',
System='<host>',
database='<database>',
uid='<username>',
pwd='<password>')
c1 = connection.cursor()
print('Connection established')
After connecting I run this command to see the list of tables:
c1.execute("select table_name from sysibm.sqltables")
And I see all the tables that I would need to query. But then when I try to query the contents of a specific table using:
c1.execute("select * from <database>.<table> LIMIT 100")
I get an error:
ProgrammingError: ('42S02', '[42S02] [IBM][System i Access ODBC Driver][DB2 for i5/OS]SQL0204 - <table> der Art *FILE in <database> nicht gefunden. (-204) (SQLExecDirectW)')
(It's in German, it means table of type *FILE in database not found)
(And I'm not using angle brackets, it's just for demonstration)
But a software like DBeaver returns valid data for both of them, for the tables list query and the specific table query. It's only python that gives the error.
Can anyone point out what could I be doing wrong? Or what can I run to pinpoint the problem?
<database>.<table> is not correct.
On Db2 you have
database
-schema
--table
you should be using <schema>.<table> as the database has been specified in the connection string.
<database>.<schema>.<table> is supported when connecting to a remote database from the "local" one...but only for basic selects. Db2 for IBM i doesn't support for example joining between a "local" and remote table.

Informatica workflow(MS SQL SERVER data source and target) fails error RR_4036"Data Source name not found and no default driver specified ."

I have windows 64bit and a Informatica client 32bit installed on it, so I added a DSN entry using the ODBC administrator for 32bit with driver native client "sql server" to connect to a Mssql 2008 R2 database on another pc. I added an entry in system DSN tab because informatica service is using System account. No problems when I have to import source and target in informatica using that ODBC DSN 32 bit "TARGET_DB_sciolto" (see odbc.ini below) entry. I build up my mapping and in the workflow session property than I change the connection from mssql server to ODBC.
The worflow is failing with same RR_4036 error.I think that the only thing I still have to try is to modify my ODBC.ini file since I read that I must add "enablequotedidentifiers=1" so I opened the file expecting to see a lot of entries but I only see following few lines and don't know what to do and would really appreciate if someone would help:
[ODBC 32 bit Data Sources]
test_infa=SQL Server Native Client 10.0 (32 bit)
TARGET_DB_sciolto=SQL Server (32 bit)
Target_DB=SQL Server (32 bit)
[test_infa]
Driver32=C:\WINDOWS\SysWOW64\sqlncli10.dll
[TARGET_DB_sciolto]
Driver32=C:\WINDOWS\system32\SQLSRV32.dll
[Target_DB]
Driver32=C:\WINDOWS\system32\SQLSRV32.dll
You need to set it up using lots of parameters. Please make sure you add the connection info in below format. Pls mention host,port,database at least. also make sure informatica machine has access to the datbase.
[TARGET_DB_sciolto]
Driver32=C:\WINDOWS\system32\SQLSRV32.dll
Description=DataDirect 7.1 SQL Server Wire Protocol
AlternateServers=
AlwaysReportTriggerResults=0
AnsiNPW=1
ApplicationName=
ApplicationUsingThreads=1
AuthenticationMethod=1
BulkBinaryThreshold=32
BulkCharacterThreshold=-1
BulkLoadBatchSize=1024
BulkLoadOptions=2
ConnectionReset=0
ConnectionRetryCount=0
ConnectionRetryDelay=3
Database=<database_name>
EnableBulkLoad=0
EnableQuotedIdentifiers=0
EncryptionMethod=0
FailoverGranularity=0
FailoverMode=0
FailoverPreconnect=0
FetchTSWTZasTimestamp=0
FetchTWFSasTime=1
GSSClient=native
HostName=<SQL_Server_host>
HostNameInCertificate=
InitializationString=
Language=
LoadBalanceTimeout=0
LoadBalancing=0
LoginTimeout=15
LogonID=
MaxPoolSize=100
MinPoolSize=0
PacketSize=-1
Password=
Pooling=0
PortNumber=<SQL_Server_server_port>
QueryTimeout=0
ReportCodePageConversionErrors=0
SnapshotSerializable=0
TrustStore=
TrustStorePassword=
ValidateServerCertificate=1
WorkStationID=
XML Describe Type=-10

How to setup ODBC connection via command line for Teradata driver?

I am trying to setup a Teradata ODBC User DSN via below command line code.
%WINDIR%\System32\odbcconf.exe CONFIGDSN "Teradata" "DSN=Test|Description=Test|SERVER=ServerName|Trusted_Connection=Yes|Database=Controltub|UID=<LoginId>"
%WINDIR%\SysWOW64\odbcconf.exe CONFIGDSN "Teradata" "DSN=Test|Description=Test|SERVER=ServerName|Trusted_Connection=Tes|Database=Controltub|UID=<LoginId>"
This code works fine for "SQL Server" driver. However when I try to make changes accordingly for Teradata ODBC creation, it fails without any error.
I get blank in text fields for server name/IP field & user id in ODBC window.
The following worked for me with the Teradata 16.20 Driver on Windows 7:
%WINDIR%\System32\odbcconf.exe CONFIGDSN “Teradata Database ODBC Driver 16.20” “DSN=Test|Description=Test|DBCName={server}|DefaultDatabase=DBC|MechanismName=TD2|Username={user}”
Once you create the DSN, you can go into the Registry under Computer\HKEY_CURRENT_USER\Software\ODBC\ODBC.INI{DSN Name} and try the other property (key) names to customize the DSN creation further. What I provide should get you a basic DSN to connect to Teradata.
Replace TD2 with LDAP if your environment uses LDAP authentication.

Cannot create direct connection to Oracle Server (Excel)

Hi I'm attempting to use some of the SQL code I've written in SQL developer through a direct database connection via Excel. Unfortunately the error message I receive whenever I try is:
"Attempt to load Oracle client libraries threw BadImageFormatException. This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed."
I've painstakingly installed the Oracle 64bit client on my PC, so that at least, should be working. Unfortunately I've spent the best part of two days looking up solutions on the internet with no success. Does anyone know how I can force Excel to use my 64bit client or avoid this error in another way?
Update:
I believe despite my best efforts the problem is somewhere within my TNSNAMES file since I can't connect using SQL Plus either. Here's a redacted version of my Connection string. The ommitted details are correct since they are the same as I'm using for SQL Developer. Am I missing something?
DB1 =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = xxx.xxx.xxx.xxx)(PORT = 1521))
)
(CONNECT_DATA = (SID = SID1)(SERVER = DEDICATED)
)
)
DB2 =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = xxx.xxx.xxx.xxx)(PORT = 1521))
)
(CONNECT_DATA = (SID = SID2)(SERVER = DEDICATED)
)
)
Obviously, it's something small in your machine settings or configuration.
I would suggest you will look at your machine's PATH environment variable. It may refer to an older version of Oracle Server or the Oracle Client Components.
Make sure you know what you are using to access Oracle from Excel, JDBC or ODBC?
It's always the little details which drive us crazy.
Did you see any of these answers?
SQL Server 2012 connectivity to Oracle
Getting MS Office 2013 (32 bit) Excel and Access to connect to Oracle database when both 32 and 64 clients are installed
and I found this link, too complicated to copy everything to the answer:
Connect To Oracle Via ODBC Driver
And you can try connecting to Oracle with ODBC using ODAC or Oracle client, download ODAC112030_x64.zip from Oracle's website. Here's a link on how to install it SETTING UP AN ORACLE ODBC DRIVER AND DATA SOURCE

Drivers to connect to an Oracle Database using VBA

I'm trying to connect to an Oracle Database from an Excel application and whithout a DNS. I found on a website that it's possible to use ADO, so this is why I tried to do. I'm new to this so I juste copied what I found on this website.
Here is my code so far :
Sub ADOtest()
Dim connection As New ADODB.connection
connection.ConnectionString = "UID = user1; PWD= my_pwd; DRIVER = {Microsoft ODBC for Oracle; Server= localhost; Database= orcl.my_domain;"
connection.Open
End sub
When I run this code, I get an error saying that the driver was not found.
The problem is that I have no idea of what I have to do with the driver (how to install it and configure it). Plus, I don't know which one I should use : I've read that there is a driver from Microsoft, another one from Oracle and also I've seen something about providers like msdaora.
The program will be used by many users, so I would like to choose the solution that is the lightest (not much to install on computers).
Thank you !
For COM based ADO (ADODB) you can use the OLE DB Providers.
One is from Oracle, called "Oracle Provider for OLE DB". You can download it from 32-bit Oracle Data Access Components (ODAC) and NuGet Downloads (assuming your Excel is 32-bit). The connection string would be
"Provider=OraOLEDB.Oracle;Data Source=orcl;User ID=myUsername;Password=myPassword"
The other one is from Microsoft. Please note, this provider is deprecated, you should not use it for new projects. Usually it should be available on your Windows. Be aware, like the provider from Oracle it also requires an Oracle Client to be installed on the PC! The connection string would be
"Provider=MSDAORA;Data Source=orcl;User ID=myUsername;Password=myPassword"
The data source is usually defined in tnsnames.ora file or at a LDAP server, for example:
orcl.my_domain =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(Host = localhost)(Port = 1521))
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)
If you don't have such entry you can put everything into the connection string, e.g.
"Provider=OraOLEDB.Oracle;Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=localhost)(Port=1521))(CONNECT_DATA=(SERVICE_NAME=orcl)));User ID=myUsername;Password=myPassword"
Perhaps you have to enclose the data source value by double-quotes ("), I am not sure.
So, in any case you would have to install an Oracle Client at all PC's.
Where is your database server hosted? In your question you say Server=localhost;, this would be quite unlikely, i.e. it is in contradiction to The program will be used by many users. I doubt everybody has an Oracle Database server installed on his local host.

Resources