Get data from database via ODBC with Python (Access)

Introduction

The article of the ancestor is enough, but for myself. Also, I remember that the data came from Sports Data Analysis Competition or Access, so I hope it helps the participants.

Articles of ancestors

https://qiita.com/taro373/items/619e3385087279f72279

My example

environment

--Windows 10 1909 64-bit Edition --Access 365 2002 32-bit Edition

Preparation

Download and install "Microsoft Access Database Engine 2010 Redistributable Components" https://www.microsoft.com/ja-jp/download/details.aspx?id=13255

Database

A database consisting only of a table named T_1 with the following contents

ID field1
1 Test value

Source

test.py


import pandas as pd
import pyodbc

#List of ODBC drivers in your PC
print(pyodbc.drivers())
print()

#Character string used for DB connection
#In the string "{}"And"\, So it's better to use a raw string
conn_str = (
    r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; ' +
    r'DBQ=(Drive name):\(Folder name)\ ~ \file name.accdb;'
)

#Use with statement to safely open and close DB
with pyodbc.connect(conn_str) as conn:

    #When using DataFrame
    print('*When using DataFrame')
    df = pd.io.sql.read_sql(r'select * from T_1', conn)
    print(df)
    print()

    print('*When using the cursor')
    with conn.cursor() as cur:
        #About each table
        for table_info in cur.tables(tableType='TABLE'):
            #Display contents with select statement
            #If you want to use both raw strings and f notation, "rf''To
            cur.execute(rf'select * from {table_info.table_name}')
            for row in cur.fetchall():
                print(row)

Execution result

['SQL Server', 'SQL Server Native Client 11.0', 'Microsoft Access Driver (*.mdb, *.accdb)', 'Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)', 'Microsoft Access dBASE Driver (*.dbf, *.ndx, *.mdx)', 'Microsoft Access Text Driver (*.txt, *.csv)']

*When using DataFrame
   ID field1
0 1 Test value

*When using the cursor
(1, 'Test value')

in conclusion

that's all.

Recommended Posts

Get data from database via ODBC with Python (Access)
[Note] Get data from PostgreSQL with Python
Get Youtube data with python
ODBC access to SQL Server from Linux with Python
Get data from Quandl in Python
[Python] Get economic data with DataReader
Get data from analytics API with Google API Client for python
Receive textual data from mysql with python
Get data from Cloudant with Bluemix flask
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
Manipulating kintone data with Python & C Data ODBC Driver from AWS Lambda
Extract database tables with CSV [ODBC connection from R and python]
Data integration from Python app on Linux to Amazon Redshift with ODBC
Data integration from Python app on Windows to Amazon Redshift with ODBC
Get time series data from k-db.com in Python
Get stock price data with Quandl API [Python]
Extract data from a web page with Python
Data analysis with python 2
Access bitcoind from python
Data analysis with Python
Get corporate number at once via gbizinfo with python
How to scrape image data from flickr with python
Get schedule from Garoon SOAP API with Python + Zeep
Get data from GPS module at 10Hz in Python
Send data from Python to Processing via socket communication
Interact with Python on Android from PC via adb
Get mail from Gmail and label it with Python3
[Linux] [Python] [Pandas] Load Microsoft Access database (* .mdb) with Pandas
Get structural data from CHEMBLID
Get country code with python
Sample data created with python
Access Oracle DB from Python
Get thread ID with python
Get started with Python! ~ ② Grammar ~
Access Google Drive with Python
Get stock price with Python
Get home directory with python
Get keyboard events with python
With skype, notify with skype from python!
Get Alembic information with Python
Read json data with python
Check! Get sensor data via Bluetooth with Raspberry Pi ~ Preparation
Get rid of dirty data with Python and regular expressions
Xpath summary when extracting data from websites with Python Scrapy
Get additional data to LDAP with python (Writer and Reader)
Get message from first offset with kafka consumer in python
[Introduction to Python] How to get data with the listdir function
Get Leap Motion data in Python.
Get PowerShell commands from malware dynamic analysis site with BeautifulSoup + Python
Try IAM Database Authentication from Python
Get started with Python! ~ ① Environment construction ~
Call C from Python with DragonFFI
Link to get started with python
Using Rstan from Python with PypeR
Get reviews with python googlemap api
Install Python from source with Ansible
Create folders from '01' to '12' with python
Get the weather with Python requests
Get web screen capture with python
Get the weather with Python requests 2
Get one column from DataFrame with DataFrame