How to access the mdb database through Python scripts in Linux

Source: Internet
Author: User
Tags mdb database

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.