This article mainly introduces the method of accessing the MDB database through Python script in Linux, this article is based on the Linux system of the Debian Department, the friend who needs it can refer to the
In the Linux system to connect the MDB database, directly connected, the MDB default driver does not recognize the non-Windows path, so you can not use the normal connection method
|
Driver={microsoft Access DRIVER (*.mdb)};D Bq=c:dirfile.mdb |
Installation
Here we need to use some libraries to implement the third party driver
We need to install these packages: Mdbtools, UNIXODBC, LIBMDBODBC
If you are not able to find Libmdbodbc in a deb-enabled system, add the following path to the list of software sources
Deb Http://ftp.de.debian.org/debian Squeeze Main
LIBMDBODBC can be installed after the source is updated
Configuration
After installing the required packages, you need to do some configuration to support the LIBMDBODBC driver
The code is as follows:
/etc/odbcinst.ini
[MDBTOOLSODBC]
Description = MDB Tools ODBC
Driver =/usr/lib/libmdbodbc.so.0
Setup =
FileUsage =
CPTimeout =
Cpreuse =
/etc/odbc.ini or ~/.odbc.ini.
The code is as follows:
[Test]
Description = Microsoft Access Try DB
Driver = Mdbtoolsodbc
Database =/path/to/mdb/file/test.mdb
Servername = localhost
Username =
Password =
Port = 5432
Code
Once the data source is configured, it can be used in any application that supports ODBC access, as an example of PYODBC
|
#-*-coding:utf-8-*- Import Pyodbc conn = Pyodbc.connect (' dsn=test '); cursor = Conn.cursor () cursor.execute (' select * from ' province ') for row in Cursor.fetchall (): Print row. Name |
Note: If the operation name of the Chinese table or field, you need to include it in double quotes, otherwise there will be errors, of course, the table name into Chinese so two of the practice, or not recommended use of the
Pyodbc is a very good library, the API is also very useful, but not good for Chinese support, it does not default to Unicode processing data, so for Chinese-related applications, coding problems everywhere, had to slowly toss.