This article mainly introduces how to access the mdb database through Python scripts in Linux. This example is based on the debian Linux system. If you need a friend, refer to connect to the mdb database in linux, if you connect directly, the default mdb driver cannot identify non-windows paths, so you cannot use conventional connections.
Method
DRIVER={Microsoft Access Driver (*.mdb)};DBQ=c:\\dir\\file.mdb
Install
Here we need to use some libraries to implement third-party drivers.
We need to install these packages: mdbtools, unixODBC, and libmdbodbc
If libmdbodbc cannot be found in a deb-supported system, add the following path to the software source list.
Deb http://ftp.de.debian.org/debian squeeze main
After updating the source, you can install libmdbodbc.
Configuration
After the required package is installed, some configuration is required 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
After the data source is configured, it can be used in any application that supports odbc access. Here, pyodbc is used as an example.
#-*-Coding: UTF-8-*-import pyodbcconn = pyodbc. connect ('dsn = test'); cursor = conn.cursor(your cursor.exe cute ('select * from "Province" ') for row in cursor. fetchall (): print row. name
Note: If the operation name is a Chinese table or field, you need to include it in double quotation marks. Otherwise, an error will occur. Of course, the method of making the table name Chinese is as follows, it is not recommended.
Pyodbc is a very good library, and the api is also very useful, but it does not support Chinese, it does not use unicode to process data by default, so for Chinese-related applications, coding problems are everywhere, so I had to wait for a while.