首页 > 数据库 >Python操作数据库及应用

Python操作数据库及应用

时间:2022-10-21 14:24:15浏览次数:55  
标签:insert Python 数据库 cursor conn 应用 query self def

 

Utils.DBHelper 

import cx_Oracle


class OracleHandler(object):

    def __init__(self, name):
        if name == 'PULSE':
            self.user = 'user'
            self.password = 'password'
            self.host = web_mes

        else:
            raise Exception('Can not recognise the user name, please contact to SW administrator')
        self._conn = cx_Oracle.connect(self.user, self.password, self.host)
        self.cursor = self._conn.cursor()

    def get_version(self):
        return self._conn.version

    def query_all(self, query):
        self.cursor.execute(query)
        return self.cursor.fetchall()

    def query_one(self, query):
        self.cursor.execute(query)
        return self.cursor.fetchone()

    def insert_many(self, sql_query, insert_param):
        if len(insert_param) == 0:
            raise Exception('Insert parameters cannot be null')
        self.cursor.prepare(sql_query)
        result = self.cursor.executemany(None, insert_param)
        self.commit()
        return result

    def insert_one(self, sql_query, insert_param):
        if len(insert_param) == 0:
            raise Exception('Insert parameters cannot be null')
        self.cursor.prepare(sql_query)
        result = self.cursor.execute(None, insert_param)
        self.commit()
        return result

    def delete_data(self, sql_query):
        self.cursor.execute(sql_query)
        self.commit()

    def commit(self):
        self._conn.commit()

    def __del__(self):
        if hasattr(self, 'cursor'):
            self.cursor.close()
        if hasattr(self, '_conn'):
            self._conn.close()


web_mes = """
        (DESCRIPTION =
            (ADDRESS_LIST =
            (ADDRESS = (PROTOCOL = TCP)(HOST = 10.xxx.xxx.xxx)(PORT = 1521))
            )
            (CONNECT_DATA =
            (SERVICE_NAME = prdinfo)
            )
        )
        """

 

应用

def try_get_exist_data(y, m,plant_code):
    conn = Utils.DBHelper.OracleHandler("PULSE")
    query = f'select * from t_xxx where year = {y} and month = {m} ' \
            f'and plant = {plant_code}'.format(y=y, m=m, plant_code=plant_code)
    try:
        result = conn.query_one(query)
    except Exception as e:
        log.error('An error occurred while get exist data, Error: ' + e.args[0])
    del conn
    if result is None:
        log.info('Data not exists')
        return False
    else:
        log.info('Data exists')
        return True

 

Query_One,Query_Many,Insert_One,Inser_Many 按情况使用即可

 

标签:insert,Python,数据库,cursor,conn,应用,query,self,def
From: https://www.cnblogs.com/kykstyle/p/16813265.html

相关文章

  • 枚举实际应用
    packagecom.msb.enum05;/***开发人:liu*日期:13:43:13*描述:IntelliJIDEA*版本:1.0*/publicclassPerson{//属性privateintage;pr......
  • Python操作SAP
    一些基础的操作SAP的CodeOpenSAP#OpenSAPdefopen_sap():try:Application(backend="win32").start(sap_path,timeout=60)log.info("OpenS......
  • 一位同学的Python大作业【分析当当网书籍价格、出版社、电子书版本占比数据】
    目录​​前言​​​​本次案例实现目标​​​​最基本思路流程:<通用>​​​​一.数据来源分析​​​​二.代码实现步骤过程:代码实现基本四大步骤​​​​代码实现​​......
  • 017 Python 流程控制之 if 判断
    博客配套视频链接:​​https://space.bilibili.com/383551518?spm_id_from=333.1007.0.0​​b站直接看配套github链接:​​https://github.com/nickchen121/shui-water......
  • Linux常用启动python和jar的后台运行命令
    1>通过端口查看是否启动并获取PIDlsof-i:端口号(例如,7777、4444、10000)2>关闭端口kill-9PID号(例如,kill-97777)3>启动imei.pynohuppython-uimei.py>/dev/null......
  • scrapy爬虫简单使用&python执行cmd命令程序
    1.安装pipinstallscrapy2.scrapy简单运行以及架构1.项目创建以及运行创建项目aaa@localhostpyspace%scrapystartprojectdemo1NewScrapyproject'demo1',using......
  • python 连库定时生成excel文件并转成可执行文件
    importscheduleimportpandasaspdimportpymysql,xlwtfromdatetimeimportdatetimefromtimeimportstrftimedt=datetime.now()#print(dt)defexport_excel(dt):......
  • Python打包成exe,包含cx_oracle的情况
    当Python程序应用了cx_oracle包,打包成功之后,exe文件不能正常运行。需要按照以下步骤执行1,执行pyinstaller-Fmain.py,执行之后生成一个main.spec文件2,按照下图修改spec文......
  • 为什么 Python 会如此火爆?
    python是编程语言,属于高级语言,python之所以火爆,主要是爬虫的应用,也就是你可以来做自己的大数据,用python来进行搜索数据!第二python应用在人工智能上,效果较好!第三:它属于胶水......
  • Python | 用Python制作送给女票的生日礼物
    HappyBirthday程序视频地址:​​https://www.bilibili.com/video/BV1R7411C7A1​​代码地址:​​https://github.com/borninfreedom/HappyBirthday​​截图: ......