python3 OceanBase 数据库连接

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: 风过无言花易落
# @Date  : 2022/08/01 17:00
# @Desc  : OceanBase 数据库连接(商业版本,驱动需要有企业合作下载)

import jaydebeapi
class OceanBase(object):
    def __init__(self, url, user, password):
        driver = 'com.alipay.oceanbase.jdbc.Driver'
        jarFile = 'I:\Account\\tydic_vop_scripts\\resource_num_iccid_imsi/oceanbase-client-2.2.7.3.jar' # 驱动jar包 官网可以下载
        self.connection = jaydebeapi.connect(driver, url, [user, password], jarFile)

    def fetch_one(self, sql):
        """
        查询单条
        :param sql:
        :return:
        """
        try:
            conn = self.connection
            cursor = conn.cursor()
            cursor.execute(sql)
            result = cursor.fetchone()

        except Exception as e:

            cursor.close()
            conn.close()
            print('execute sql {0} is error:{1}'.format(sql, e))
        return result

    def fetch_all(self, sql):
        """
        查询全部
        :param sql:
        :return:
        """
        try:
            conn = self.connection
            cursor = conn.cursor()
            cursor.execute(sql)
            result = cursor.fetchall()
        except Exception as e:

            cursor.close()
            conn.close()
            print('execute sql {0} is error:{1}'.format(sql, e))

        return result

    def InsertSql(self, sql):
        """
        插入+更新+删除
        :param sql:
        :return:
        """
        try:
            conn = self.connection
            cursor = conn.cursor()
            cursor.execute(sql)
            conn.commit()
        except Exception as e:
            cursor.close()
            conn.close()
            print('execute sql {0} is error:{1}'.format(sql, e))
        return True

    def __del__(self):
        print('关闭数据库连接')
        self.connection.close()

if __name__ == "__main__":
    url = 'jdbc:oceanbase://10.10.10.255:12345/database'
    user = '租户里的用户名@租户名#集群名'
    password = '111111111111'
    sql = 'select 1 from dual'
    ob = OceanBase(url,user,password)
    print(ob.fetch_one(sql))

你可能感兴趣的:(数据库,sql,java)