python:封装连接数据库方法
生活随笔
收集整理的這篇文章主要介紹了
python:封装连接数据库方法
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?
config.py
# 數(shù)據(jù)庫(kù)測(cè)試環(huán)境 name = '***' password = '******' host_port_sid = '10.**.*.**:1521/bidbuat'?
OracleOperation.py
import cx_Oracle import configclass OracleOperation(object):# 執(zhí)行下面的execute_sql方法時(shí)會(huì)自動(dòng)執(zhí)行該初始化方法進(jìn)行連接數(shù)據(jù)庫(kù)def __init__(self):# 建立連接self.conn = cx_Oracle.connect(config.name, config.password, config.host_port_sid)# 創(chuàng)建游標(biāo)self.cursor = self.conn.cursor()def execute_sql(self, sql):"""執(zhí)行sql語(yǔ)句,并commit提交:param sql:需要執(zhí)行的sql語(yǔ)句:return:"""self.cursor.execute(sql)self.conn.commit()def get_data(self):"""獲得查詢數(shù)據(jù):return: 返回查到的數(shù)據(jù)"""data = self.cursor.fetchall()return datadef close_oracle(self):# 關(guān)閉游標(biāo) self.cursor.close()# 關(guān)閉數(shù)據(jù)庫(kù)連接self.conn.close()?
轉(zhuǎn)載于:https://www.cnblogs.com/gcgc/p/11526803.html
總結(jié)
以上是生活随笔為你收集整理的python:封装连接数据库方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 梦到自己装死什么意思啊
- 下一篇: python:使用Djangorestf