OceanBase logo

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Resources

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS

OceanBase Cloud

OceanBase Database

Tools

Connectors and Middleware

QUICK START

OceanBase Cloud

OceanBase Database

BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Company

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

International - English
中国站 - 简体中文
日本 - 日本語
Sign In
Start on Cloud

A unified distributed database ready for your transactional, analytical, and AI workloads.

DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS
OceanBase CloudOceanBase Database
ToolsConnectors and Middleware
QUICK START
OceanBase CloudOceanBase Database
BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

Start on Cloud
编组
All Products
    • Databases
    • iconOceanBase Database
    • iconOceanBase Cloud
    • iconOceanBase Tugraph
    • iconInteractive Tutorials
    • iconOceanBase Best Practices
    • Tools
    • iconOceanBase Cloud Platform
    • iconOceanBase Migration Service
    • iconOceanBase Developer Center
    • iconOceanBase Migration Assessment
    • iconOceanBase Admin Tool
    • iconOceanBase Loader and Dumper
    • iconOceanBase Deployer
    • iconKubernetes operator for OceanBase
    • iconOceanBase Diagnostic Tool
    • iconOceanBase Binlog Service
    • Connectors and Middleware
    • iconOceanBase Database Proxy
    • iconEmbedded SQL in C for OceanBase
    • iconOceanBase Call Interface
    • iconOceanBase Connector/C
    • iconOceanBase Connector/J
    • iconOceanBase Connector/ODBC
    • iconOceanBase Connector/NET
icon

OceanBase Database

SQL - V3.1.4Community Edition

  • Get Started
    • Overview
    • Quick Start
    • Hands on for OceanBase SQL
      • Before You Start
      • Basic Operations
    • Build Applications and Connect to OceanBase
      • Connect a Python application to OceanBase Database
      • Connect an ODBC application to OceanBase Database (using Unix ODBC)
      • Connect a Java application to OceanBase Database
      • Connect a Golang application to OceanBase Database (using Go-SQL-Driver/MySQL)
      • Connect a C application to OceanBase Database
    • Experience OceanBase Advanced Features
      • Experience Scalable OLTP
        • Run the TPC-C benchmark test in OceanBase Database
        • Experience the hot row update capability of OceanBase Database
      • Experience operational OLAP
      • Experience parallel import and data compression
      • Experience the multi-tenant feature
    • FAQ
    • Glossary

Download PDF

Overview Quick Start Before You Start Basic Operations Connect a Python application to OceanBase Database Connect an ODBC application to OceanBase Database (using Unix ODBC) Connect a Java application to OceanBase Database Connect a Golang application to OceanBase Database (using Go-SQL-Driver/MySQL) Connect a C application to OceanBase Database Experience operational OLAP Experience parallel import and data compression Experience the multi-tenant feature FAQ Glossary
OceanBase logo

The Unified Distributed Database for the AI Era.

Follow Us
Products
OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
Resources
DocsBlogLive DemosTraining & Certification
Company
About OceanBaseTrust CenterLegalPartnerContact Us
Follow Us

© OceanBase 2026. All rights reserved

Cloud Service AgreementPrivacy PolicySecurity
Contact Us
Document Feedback
  1. Documentation Center
  2. OceanBase Database
  3. SQL
  4. V3.1.4
iconOceanBase Database
SQL - V 3.1.4Community Edition
SQL
KV
  • V 4.4.2
  • V 4.3.5
  • V 4.3.3
  • V 4.3.1
  • V 4.3.0
  • V 4.2.5
  • V 4.2.2
  • V 4.2.1
  • V 4.2.0
  • V 4.1.0
  • V 4.0.0
  • V 3.1.4 and earlier
Community Edition
  • V 3.1.4

Connect a Python application to OceanBase Database

Last Updated:2023-07-21 09:10:58  Updated
share
What is on this page
Python3.x series (using PyMySQL)
Install PyMySQL
Use PyMySQL
Python2.x series (using MySQL-python)
Install MySQL-python
Use MySQL-python

folded

share

Connect a Python application to OceanBase Database

This topic provides a code example on how to connect a Python application to OceanBase Database.

Python3.x series (using PyMySQL)

PyMySQL is a library for connecting to the MySQL server in Python3.x.
PyMySQL follows the Python Database API v2.0 specification and includes a pure-Python MySQL client library.
For more information about PyMySQL, see the official documentation and API reference documentation.

Install PyMySQL

Go to the PyMySQL page to download and install PyMySQL.
There are two methods of installing PyMySQL:

  • Use the command line

    python3 -m pip install PyMySQL
    
  • Compile from source code

    git clone https://github.com/PyMySQL/PyMySQL
    cd PyMySQL/
    python3 setup.py install
    

Use PyMySQL

Run the following command to set the connect object:

conn = pymysql.connect(
    host="localhost", 
    port=2881,user="root", 
    passwd="", 
    db="testdb"
)

Example:

#!/usr/bin/python3

import pymysql

conn = pymysql.connect(host="localhost", port=2881,
                       user="root", passwd="", db="testdb")

try:
    with conn.cursor() as cur:
        cur.execute('SELECT * FROM cities')
        ans = cur.fetchall()
        print(ans)

finally:
    conn.close()

Python2.x series (using MySQL-python)

MySQL-python is a library for connecting to the MySQL server in Python2.x.
MySQL-python is installed to connect and operate OceanBase Database using MySQLdb. MySQLdb is an interface for a Python application to connect to the MySQL database. It implements the Python database API specification V2.0 and is based on the MySQL C API.
For more information about MySQL-python, see the official documentation and GitHub documentation.

Install MySQL-python

First, ensure that you have a Python2.x environment on your computer, and then use pip to install MySQL-python.

pip install MySQL-python

Use MySQL-python

Run the following command to set the connect object:

conn= MySQLdb.connect(
    host='127.0.0.1',
    port = 2881,
    user='root',
    passwd='',
    db ='testdb'
)    

Example:

#!/usr/bin/python2

import MySQLdb

conn= MySQLdb.connect(
    host='127.0.0.1',
    port = 2881,
    user='root',
    passwd='',
    db ='testdb'
)

try:
    cur = conn.cursor()
    cur.execute('SELECT * from cities')
    ans = cur.fetchall()
    print(ans)
    
finally:
    conn.close()

Previous topic

Basic Operations
Last

Next topic

Connect an ODBC application to OceanBase Database (using Unix ODBC)
Next
What is on this page
Python3.x series (using PyMySQL)
Install PyMySQL
Use PyMySQL
Python2.x series (using MySQL-python)
Install MySQL-python
Use MySQL-python