Python訪問OceanBase資料庫方法
操作步驟
1、安裝jaydebeapi,操作方法:pip install jaydebeapi
2、將OceanBase Connector/J 的JAR安裝包放入本地路徑中,我使用的是oceanbase-client-2.2.7.jar,阿里提供的oceanbase-client-1.1.10.jar下載地址(https://oceanbase-aliyun-docs.oss-cn-hangzhou.aliyuncs.com/downloads/obclient/oceanbase-client-1.1.10.jar?spm=a2c4g.11186623.0.0.27aa4d5bho7Of4&file=oceanbase-client-1.1.10.jar)
3、測試程式如下:
import jaydebeapi
url = 'jdbc:oceanbase://host:port/database' #ob資料庫網址
user = 'userxxx@租戶#叢集' #使用者名稱,如配置有租戶和叢集,需要在此加上(很關鍵)
password = 'passxxx' #密碼
driver = 'com.alipay.oceanbase.jdbc.Driver'
jarFile = './oceanbase-client-2.2.7.jar'
sqlStr = 'select * from test_python' #SQL語句
conn = jaydebeapi.connect(driver, url, [user, password], jarFile)
curs = conn.cursor()
curs.execute(sqlStr)
result = curs.fetchall()
print(result)
curs.close()
conn.close()