Python使用sqlalchemy實(shí)現(xiàn)連接數(shù)據(jù)庫(kù)的幫助類
python使用 sqlalchemy連接數(shù)據(jù)庫(kù)幫助類
實(shí)現(xiàn)代碼
import mysql.connector class MySqlHelper(object): """操作數(shù)據(jù)庫(kù)幫助類""" def __init__(self): #self.host = "localhost" #self.user = "root" #self.password = "xinshiyun@123" #self.database = "deliverunion_callcenter" self.host = "192.168.60.156" self.user = "root" self.password = "root" self.database = "deliverunion_callcenter" try: self.mydb = mysql.connector.connect(host=self.host, user=self.user, passwd=self.password, database=self.database, connect_timeout=10) #database=self.database, #auth_plugin='mysql_native_password') self.mycursor = self.mydb.cursor() except Exception as e: print('MySql Error : %d %s' % (e.args[0],e.args[1])) #不帶參數(shù)的查詢 def select(self,mysql): try: self.mycursor.execute(mysql) values = self.mycursor.fetchall() return values except Exception as e: print ('select Error : %d %s' % (e.args[0],e.args[1])) return [] finally: self.mycursor.close() self.mydb.close() #帶參數(shù)的查詢 def select2(self,mysql,na): try: self.mycursor.execute(mysql,na) values = self.mycursor.fetchall() return values except Exception as e: print ('select2 Error : %d %s' % (e.args[0],e.args[1])) return [] finally: self.mycursor.close() self.mydb.close() #更新 def Update(self,mysql,na): try: self.mycursor.execute(mysql,na) self.mydb.commit() row = self.mycursor.rowcount if row > 0: return True else: return False except Exception as e: print ('Update Error : %d %s' % (e.args[0],e.args[1])) return False finally: self.mycursor.close() self.mydb.close() #插入數(shù)據(jù) def Insert(self,mysql,na): try: self.mycursor.execute(mysql,na) self.mydb.commit() row = self.mycursor.rowcount if row > 0: return True else: return False except Exception as e: print('Insert Error : %d %s' % (e.args[0],e.args[1])) return False finally: self.mycursor.close() self.mydb.close()
使用數(shù)據(jù):
from DAL import MySqlHelper from Entity import TaskPoolEntity import datetime class TaskPoolDAL(object): """操作數(shù)據(jù)庫(kù)t_du_guiji_task""" sqlHelper = MySqlHelper.MySqlHelper() #查詢所有的任務(wù) def selectTasks(): TaskPoolDAL.sqlHelper = MySqlHelper.MySqlHelper() sql='select * from t_du_guiji_task' values= TaskPoolDAL.sqlHelper.select(sql) data=[] for item in values: selectTask = TaskPoolEntity.TaskPoolEntity(item) data.append(selectTask) return data
以上就是Python使用sqlalchemy實(shí)現(xiàn)連接數(shù)據(jù)庫(kù)的幫助類的詳細(xì)內(nèi)容,更多關(guān)于Python sqlalchemy連接數(shù)據(jù)庫(kù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- 分析解決Python中sqlalchemy數(shù)據(jù)庫(kù)連接池QueuePool異常
- 3個(gè)Python?SQLAlchemy數(shù)據(jù)庫(kù)操作功能詳解
- Python使用SQLAlchemy模塊實(shí)現(xiàn)操作數(shù)據(jù)庫(kù)
- Python?SQLAlchemy與數(shù)據(jù)庫(kù)交互操作完整指南
- Python中使用sqlalchemy操作數(shù)據(jù)庫(kù)的問(wèn)題總結(jié)
- Python中SQLAlchemy庫(kù)的使用方法分析
- Python使用SQLAlchemy進(jìn)行復(fù)雜查詢的操作代碼
- Python如何使用sqlalchemy實(shí)現(xiàn)動(dòng)態(tài)sql
- python SQLAlchemy 數(shù)據(jù)庫(kù)連接池的實(shí)現(xiàn)
相關(guān)文章
用Python中的字典來(lái)處理索引統(tǒng)計(jì)的方法
這篇文章主要介紹了用Python中的字典來(lái)處理索引統(tǒng)計(jì)的方法,字典的使用是Python學(xué)習(xí)當(dāng)中的基礎(chǔ)知識(shí),本文則是相關(guān)的一個(gè)小實(shí)踐,需要的朋友可以參考下2015-05-05Python?創(chuàng)建或讀取?Excel?文件的操作代碼
Excel是一種常用的電子表格軟件,廣泛應(yīng)用于金融、商業(yè)和教育等領(lǐng)域,本文介紹Python?創(chuàng)建或讀取?Excel?文件的操作代碼,感興趣的朋友一起看看吧2023-09-09Pycharm項(xiàng)目代碼同步到Gitee的圖文步驟
本文主要介紹了Pycharm項(xiàng)目代碼同步到Gitee的圖文步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02如何解決import torchvision報(bào)錯(cuò)問(wèn)題 DLL:找不到模塊
這篇文章主要介紹了如何解決import torchvision報(bào)錯(cuò)問(wèn)題 DLL:找不到模塊,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01Python使用文件鎖實(shí)現(xiàn)進(jìn)程間同步功能【基于fcntl模塊】
這篇文章主要介紹了Python使用文件鎖實(shí)現(xiàn)進(jìn)程間同步功能,結(jié)合實(shí)例形式分析了Python基于fcntl模塊文件鎖功能實(shí)現(xiàn)進(jìn)程間同步的相關(guān)操作技巧,需要的朋友可以參考下2017-10-10Python實(shí)現(xiàn)的當(dāng)前時(shí)間多加一天、一小時(shí)、一分鐘操作示例
這篇文章主要介紹了Python實(shí)現(xiàn)的當(dāng)前時(shí)間多加一天、一小時(shí)、一分鐘操作,結(jié)合實(shí)例形式分析了Python基于datetime模塊進(jìn)行日期時(shí)間操作相關(guān)使用技巧,需要的朋友可以參考下2018-05-05