This topic describes how to connect Python applications to OceanBase Database by using OceanBase Connector/J and provides sample code.
Prerequisites
The basic database development environment is set.
Java Development Kit (JDK) 8 is installed.
The Python environment on the PC is version 3.6.8.
The installation package of OceanBase Connector/J is obtained from OceanBase Technical Support.
Procedure
Install JayDeBeApi. We recommend that you use the PIP installation method. For more information, see Install JayDeBeApi with PIP.
Place the JAR package of OceanBase Connector/J in the local path.
Establish a connection to OceanBase Database. Enter parameter values in the corresponding Python code snippet. If the correct result is returned, the database is connected.
Enter the IP address and port number in the
urlfield.Enter the class path in the
driverfield.Enter the JAR package installation path in the
jarFilefield.
#!/usr/bin/env python3.6 # -*- coding: UTF-8 -*- encoding = "utf8" import jaydebeapi def ob_test(): url = 'jdbc:oceanbase://host:port/database' user = '**u***' password = '**p***' 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 OceanBase Connector/J, see OceanBase Connector/J.