1. 程式人生 > >Python 邊做邊學 8.3 工具類--資料庫工具(DbUtil)

Python 邊做邊學 8.3 工具類--資料庫工具(DbUtil)

資料持久化

採集到的資料需要儲存起來,這是個demo專案,選用什麼方式做資料持久化並不是十分重要,重要的是把資料存起來;

之前專案一直在用mysql,所以此處也使用mysql做資料儲存;

搜尋“python3 操作mysql”

搜尋“python3 操作mysql”,找到一篇比較靠譜的:

文章開頭“python3.x 使用pymysql操作MySQL,python2.x使用mysqldb操作mysql”告訴我們“python3.x 使用pymysql操作MySQL”;用pycharm下載“pymysql”包之後,就可以使用了;

“pymysql”操作資料庫與其他程式語言類似:
建立連線
獲取遊標
執行語句
提交結果
關閉遊標
關閉連結

DbUtil.py程式碼

import pymysql

from lufaxin.csdn.util import CfgUtil

__host = CfgUtil.get_db("host")
__user = CfgUtil.get_db("user")
__passwd = CfgUtil.get_db("passwd")
__db = CfgUtil.get_db("db")
__port = int(CfgUtil.get_db("port"))
__charset = CfgUtil.get_db("charset")


def execute(sql_str)
:
if sql_str is None: raise Exception("引數不能為空:sql_str") if len(sql_str) == 0: raise Exception("引數不能為空:sql_str") try: conn = pymysql.connect(host=__host, user=__user, passwd=__passwd, db=__db, port=__port, charset=__charset) cur = conn.cursor() # 獲取一個遊標
cur.execute(sql_str) data = cur.fetchall() conn.commit() cur.close() # 關閉遊標 conn.close() # 釋放資料庫資源 return data except Exception as e: raise e # 插入資料,返回資料主鍵 def execute_insert(insert_str, data): if insert_str is None: raise Exception("引數不能為空:sql_str") if len(insert_str) == 0: raise Exception("引數不能為空:sql_str") try: conn = pymysql.connect(host=__host, user=__user, passwd=__passwd, db=__db, port=__port, charset=__charset) cur = conn.cursor() # 獲取一個遊標 cur.execute(insert_str, data) data = cur.fetchall() # last_id = cur.lastrowid last_id = conn.insert_id() conn.commit() cur.close() # 關閉遊標 conn.close() # 釋放資料庫資源 return last_id except Exception as e: raise e # 更新資料,返回更新條數 def execute_update(update_str, data): if update_str is None: raise Exception("引數不能為空:update_str") if len(update_str) == 0: raise Exception("引數不能為空:update_str") try: conn = pymysql.connect(host=__host, user=__user, passwd=__passwd, db=__db, port=__port, charset=__charset) cur = conn.cursor() # 獲取一個遊標 count = cur.execute(update_str, data) conn.commit() cur.close() # 關閉遊標 conn.close() # 釋放資料庫資源 return count except Exception as e: raise e # 執行帶引數的查詢,返回查詢結果 def execute_select(select_str, data): if select_str is None: raise Exception("引數不能為空:sql_str") if len(select_str) == 0: raise Exception("引數不能為空:sql_str") try: conn = pymysql.connect(host=__host, user=__user, passwd=__passwd, db=__db, port=__port, charset=__charset) cur = conn.cursor() # 獲取一個遊標 cur.execute(select_str, data) data = cur.fetchall() conn.commit() cur.close() # 關閉遊標 conn.close() # 釋放資料庫資源 return data except Exception as e: raise e # 執行帶引數的刪除 def execute_delete(select_str, data): if select_str is None: raise Exception("引數不能為空:sql_str") if len(select_str) == 0: raise Exception("引數不能為空:sql_str") try: conn = pymysql.connect(host=__host, user=__user, passwd=__passwd, db=__db, port=__port, charset=__charset) cur = conn.cursor() # 獲取一個遊標 cur.execute(select_str, data) data = cur.fetchall() conn.commit() cur.close() # 關閉遊標 conn.close() # 釋放資料庫資源 return data except Exception as e: raise e

資料庫連結

資料庫連線資訊來自配置檔案:

__host = CfgUtil.get_db("host")
__user = CfgUtil.get_db("user")
__passwd = CfgUtil.get_db("passwd")
__db = CfgUtil.get_db("db")
__port = int(CfgUtil.get_db("port"))
__charset = CfgUtil.get_db("charset")

建立連線

通過“pymysql.connect()”方法建立資料庫連線:

conn = pymysql.connect(host=__host, user=__user, passwd=__passwd, db=__db, port=__port, charset=__charset)

獲取遊標

cur = conn.cursor()  # 獲取一個遊標

執行語句

cur.execute(sql_str)
data = cur.fetchall()

提交結果

conn.commit()

關閉連線,遊標

cur.close()  # 關閉遊標
conn.close()  # 釋放資料庫資源

引數傳遞

如上面定義的“execute”方法所示,可以只傳遞一個引數,那就是要執行的sql語句;這種方式執行簡單操作可以,但執行復雜插入,更新等操作會比較複雜;所以有了下面幾個方法,用來執行不同的操作(增刪改查),不再一一介紹;

呼叫方法

from lufaxin.csdn.util import DbUtil

......

sql = "select " + cols_str + " from " + table_name + " where " + t_pk + " = " + str(pk)
sel_data = DbUtil.execute(sql)

......

sql = "delete from " + table_name + " where " + pk_name + " = " + str(pk)
del_data = DbUtil.execute(sql)

其他方法類似,構建各表資料庫操作基礎類的的時候,再詳細描述各方法的用法;

以上就是我們用到的資料庫操作工具類;

以上內容僅供練習,學習使用;