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