This topic describes how to connect Python applications to OceanBase Database by using OceanBase Connector/J and provides sample code.
Prerequisites
A basic database development environment is deployed.
You have installed or upgraded to JDK 8 on your computer.
The Python environment on your computer is version 3.6.8.
You have obtained the installation package of the OceanBase Connector/J driver from OceanBase Technical Support.
Procedure
Install JayDeBeApi. We recommend that you install JayDeBeApi with pip. For more information, see Install JayDeBeApi with pip.
Place the JAR installation package of OceanBase Connection/J in a local directory.
Establish a connection with OceanBase Database. Enter the corresponding parameter information in the Python code as follows. If valid running results are returned, the connection to the database is established.
In the
urlfield, enter the corresponding IP address and port number.In the
driverfield, enter the classpath.In the
jarFilefield, enter the path of the JAR installation package.
#!/usr/bin/env python3.6 # -*- coding: UTF-8 -*- encoding = "utf8" import jaydebeapi def ob_test(): url = 'jdbc:oceanbase://host:port/database' user = '**u***' password = '**1***' driver = 'com.alipay.oceanbase.jdbc.Driver' jarFile = './oceanbase-client-2.2.3.jar' sqlStr = 'select * from test_python' # conn=jaydebeapi.connect('oracle.jdbc.driver.OracleDriver','jdbc:oracle:thin:@10.0.0.0:1521/orcl',['hwf_model','hwf_model'],'E:/pycharm/lib/ojdbc14.jar') conn = jaydebeapi.connect(driver, url, [user, password], jarFile) curs = conn.cursor() curs.execute(sqlStr) result = curs.fetchall() print(result) curs.close() conn.close() ob_test()
For more information about how to use OceanBase Connector/J, see OceanBase Connector/J.