Unable to connect to Access database from Linux - python

i'm trying to connect to access database on my ubuntu 18 but i cant
self.con = pyodbc.connect(
r'Driver={Microsoft Access Driver (*.accdb)};'
r'DBQ=C:\Users\Derar\PycharmProjects\ULMS\ulms.accdb;PWD=v7WC$=3ZJ5pX?h?TM54S')
self.cmd = self.con.cursor()
I Get This Error
''`r'DBQ=C:\Users\Derar\PycharmProjects\ULMS\ulms.accdb;PWD=v7WC$=3ZJ5pX?h?TM54S')
pyodbc.InterfaceError: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnect)')`

It looks like your trying to run code written for Windows on Ubuntu.
It looks like the path name is not a POSIX style path.
You will need to configure linux odbc properly - or just install python on Windows and run your code there...
As far as I know there is no free linux ODBC driver for accdb files. You would likely have to convert the database, use commercial software, or access the DB from Windows.

See my answer here:
Connect to MS Access in Python
I believe it's applicable to your scenario.

Related

Connecting pyodbc to obscure proprietary ODBC connection that seems to use drivers from the 90s?

We're trying to use pyodbc to connect to an ODBC connection that's correctly configured through Win10's OBDBC Data Source Administrator (32-bit), but the driver is "SPAN Open ODBC", a string we ironically can't find anywhere on the internet.
pyodbc throws "pyodbc.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (Sified (0) (SQLDriverConnect)')", and when we check [print(x) for x in pyodbc.drivers()], "SPAN Open ODBC" is indeed absent.
We've found the DLL the Win ODBC Administrator uses at C:\Windows\SysWOW64\dhodbc.dll, but don't know how / if we should pass this to pyodbc. The company Win10 ODBC Administrator lists for it is "Dharma Systems", and the only docs we can find are from 1999...
I know this isn't much to work with but we're grasping at straws here. Do we have any options?

How should I configure the a connection to MS Azure cloud database using pyodbc?

I'm setting up a python script to connect to an instance of the azure cloud database, and seeing a difference in the connection when I use IDLE vs when I try to execute the connection from a script. For example, when I do the following in IDLE:
>>> import pyodbc
>>> conn = pyodbc.connect('DRIVER={SQL Server};SERVER=<my.database.host.info>,1433', user='<my_username#mydatabase', password='password', database='database')
I'm able connect successfully and execute a basic query against one of the tables in the database.
However when I use the same connection string (copied from the IDLE console) in a script, I get the following error:
pyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
Usually, the issue is raised because the 32/64 bit version of python environment and odbc driver are mismatched.
You may check the python version and odbc driver version you used to run the script. You have to manage to change both of them need to be 32-bit (or both 64-bit) in order for this to work.
You can get the specified version of pyodbc extension from http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyodbc.

Using SQL Server Native Client With Pyodbc on Linux

I'm trying to use Python on a RedHat machine to connect to a SQL Server database using the Microsoft provided driver. I know the driver is working because I can connect using the sqlcmd. The following in Python yields an error. It seems that pyodbc can't find the driver. Anybody know how to fix this?
conStr = 'Driver={SQL Server Native Client 11.0};Server='+server+';Database='+db+';
UID='+u+';PWD='+pw+';'
cnxn = pyodbc.connect(conStr)
pyodbc.Error: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnect)')
I found that on Linux I needed to remove the braces around the driver name.
Some more info on the Driver keyword can be gleaned from here:
http://msdn.microsoft.com/en-us/library/hh568455
Actually, You can use pymssql to connect SQLServer in python.
It's easy to use, similar with MySQL-python lib.
http://code.google.com/p/pymssql/
Since pyodbc is going through unixODBC, can you confirm odbc.ini and odbcinst.ini are correctly set up?

Connecting to MS Access 2007 (.accdb) database using pyodbc

I am on Win7 x64, using Python 2.7.1 x64. I am porting an application I created in VC++ to Python for educational purpouses.
The original application has no problem connecting to the MS Access 2007 format DB file by using the following connection string:
OleDbConnection^ conn = gcnew OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=|DataDirectory|DB.accdb");
Now, when I try to connect to the same DB file (put in C:\ this time) in Python using pyodbc and the following conenction string:
conn = pyodbc.connect("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\DB.accdb;")
, and no matter whether I keep the OLEDB provider or I use the Provider=MSDASQL; as mentioned here (MS mentions it's not availiable for 64bit), I keep getting the following error:
pyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnectW)')
What might cause this problem?
ADD:
I have looked into pyodbc docs more closely and tried conn = pyodbc.connect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=c:\\DB.accdb;") - the same error. This is really weird, since the pyodbc.dataSources() shows that I have this provider.
ADD2:
I tried win32com.client usage such as here in order to connect by using OLE DB - no success. Seems that it's impossible, nothing works.
Try to use something like the following instead of using the same string as the one for OLeDb:
"Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\DB.accdb;"
You may not be able to talk to the driver directly from your x64 Python application: Access 2007 and its ACE driver are 32 bits only.
Instead, get the ACE x64 driver for Access 2010, but be careful that if you already have Access or the ACE driver 32bit installed, it won't work.
I would stick to the 32bit versions of Python and of the ACE driver if you expect your app to be run on other systems: it is not recommended to mix x64 and x86 versions of Office tools and drivers, you'll probably end up with lots of issues if you do.
If the issue is not with the 32/64bit mix, then maybe this question has the answer you seek.

pyodbc and mySQL

I am unable to connect to mySQl db using pyodbc.
Here is a snippet of my script:
import pyodbc
import csv
cnxn = pyodbc.connect("DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost;DATABASE=mydb; UID=root; PASSWORD=thatwouldbetelling;")
crsr = cnxn.cursor()
with open('C:\\skunkworks\\archive\\data\\myfile.csv','r') as myfile:
rows = csv.reader(myfile, delimiter=',', quotechar='"')
for row in rows:
insert_str = 'INSERT into raw_data VALUES(something, something)'
print insert_str
#crsr.execute(insert_str)
cnxn.commit()
myfile.close()
I get this error at the pyodbc.connect() line:
pyodbc.Error: ('IM002', '[IM002]
[Microsoft][ODBC Driver Manager] Data
source name not found and no default
driver specified (0)
(SQLDriverConnectW)')
I have another question regarding this error (and Python scripts in general). When I run this as a script, it fails silently (I was expecting a stack trace). I have to type each line in manually to find where the error occured.
I am being a bit lazy for now (no exception handling) - is this normal behaviour of a Python script without exception handling to fail silently?
[Edit]
I am not using mysqldb because I am already using pyodbc to extract my data from another source (MS Access). Ok, not a good reason - but I am already grappling with pyodbc and I dont really fancy having to wrestle with another library/module/package(whatever its called in Python) for a "one off" job. I just want to move my data of from various data sources in the Windows environment to mySQl on Linux. once on Linux, I'll be back on terra firma.
That is the entire 'script' right there. I just saved the code above into a file with a .py extension, and I run python myscript.py at the command line. I am running this on my XP machine
I had this same mistake so I went over all the version I was using for the connection. This is what I found out:
For Python 2.7 32 bits:
- pyodbc must be 32bits
- the DB Driver must be 32bits. (Microsoft Access should be 32 bits too)
For those who use the 64 bits version. You should check that everything is 64 bits too.
In my case I was trying to connecto to an Oracle DB and Microsoft Access DB so I had to make the following components match the architechture version:
pyodbc (MS Access)
python
cx_Oracle (Oracle dialect for SQLalchemy)
Oracle instantclient basic (Oracle. Do not forget to create the environment variable)
py2exe (Making the excecutable app)
Is that your driver name right?
You can check your driver name in
Windows -> Control panel -> System and security -> Administrative tools -> ODBC Data Sources -> Driver tab
then copy the river name to the first parameter
like
cnxn = pyodbc.connect("DRIVER={MySQL ODBC 5.3 ANSI Driver}; SERVER=localhost;DATABASE=books; UID=root; PASSWORD=password;")
And my problem solved
or you may not install the driver and the step is simple.
MySQLdb (or oursql) and pyodbc both have the same interface (DB-API 2), only you don't have to deal with ODBC's issues if you use the former. I strongly recommend you consider using MySQLdb (or oursql) instead.
First, According to the official docs, if you want to connect without creating a DSN, you need to specify OPTION=3 in the connection string:
ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=test;USER=venu;PASSWORD=venu;OPTION=3;"
If that fails to work, I'd further troubleshoot by creating a DSN.
Second, no Python should not be failing silently. If that is the case when you run your script, there is something else amiss.
only need install mysql-connector-odbc-3.51.28-win32.msi.
and pyodbc-2.1.7.win32-py2.7.exe.
of course, you have ready installed MySQL and python 2.7.
example:
import pyodbc
cndBase = pyodbc.connect("DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost; PORT=3306;DATABASE=nameDBase; UID=root; PASSWORD=12345;")
ptdBase = cndBase.cursor()
query_str = 'SELECT* FROM nameTabla;'
rows = ptdBase.execute(query_str)
for rw in rows:
print list(rw)`enter code here`
I was getting the same error. It seemed the driver i was using to make the connection was not the driver installed in my system.
Type ODBC on windows run and select ODBC Data Source(32/64) based on where you have installed the driver.
From there click on System DSN and click add. From there you can see the MySQL driver installed in your system. Use the ANSI driver in your code where you are making the connection.

Categories

Resources