How to access the mdb database through Python scripts in Linux
This article mainly introduces how to access the mdb database through Python scripts in Linux. This article provides an example of a debian-based Linux system. For more information, see
If you connect to the mdb database in linux, the default drive of mdb cannot identify non-windows paths, so you cannot use the conventional connection method.
?
1 |
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.
?
1 2 3 4 5 6 7 8 |
#-*-Coding: UTF-8 -*- Import pyodbc Conn = pyodbc. connect ('dsn = test '); Cursor = conn. cursor () 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.