Python学习之GUI--SQL数据库连接
1 前言
在進(jìn)行SQL數(shù)據(jù)庫連接之前,我們需要先了解一下pymssql庫。有關(guān)pymssql的解釋推薦大家去官網(wǎng)學(xué)習(xí)。網(wǎng)上有關(guān)這方面的知識(shí)大部分是實(shí)例為主,不利于學(xué)習(xí)。另一方面,經(jīng)過他人咀嚼過的知識(shí)也總是差那么點(diǎn)味道。
2 pyssql簡(jiǎn)介
A simple database interface for Python that builds on top of FreeTDS to provide a Python DB-API (PEP-249) interface to Microsoft SQL Server.
大意就是pymssql的一個(gè)python鏈接SQL數(shù)據(jù)庫的接口包,至于其中提到的FreeTDS,如果你是Linux是需要安裝這個(gè)包的,當(dāng)然如果你是Windows系統(tǒng)就可以直接跳過這個(gè)問題了
FreeTDS is required. On some platforms, we provide a pre-compiled FreeTDS to make installing easier, but you may want to install FreeTDS before doing pip install pymssql if you run into problems or need features or bug fixes in a newer version of FreeTDS. You can build FreeTDS from source if you want the latest. If you’re okay with the latest version that your package manager provides, then you can use your package manager of choice to install FreeTDS. E.g.
Generally,you will want to install pymssql with:
pip install pymssql3 SQL數(shù)據(jù)庫連接
先上代碼
def __GetConnect(self):"""得到連接信息返回: conn.cursor()"""if not self.db:raise(NameError,"沒有設(shè)置數(shù)據(jù)庫信息")self.conn = pymssql.connect(host=self.host,user=self.user,password=self.pwd,database=self.db,charset="utf8")#打開游標(biāo)cur = self.conn.cursor()if not cur:raise(NameError,"連接數(shù)據(jù)庫失敗")else:return cur第一步: 通過connect完成對(duì)數(shù)據(jù)庫的連接
conn = pymssql.connect(host,user,password,database,charset="utf8")| server | 數(shù)據(jù)庫服務(wù)器名稱或IP |
| user | 用戶名 |
| password | 密碼 |
| database | 數(shù)據(jù)庫名稱 |
| charset | 數(shù)據(jù)的編碼格式,可以省略 |
截圖示例:
第二步: 創(chuàng)建一個(gè)游標(biāo)
到這了數(shù)據(jù)庫就算連接成功了,下面介紹如何對(duì)數(shù)據(jù)進(jìn)行操作
4 SQL查詢操作
輸入SQL查詢語句–>執(zhí)行sql語句–>獲取查詢結(jié)果–>關(guān)閉數(shù)據(jù)庫連接–>返回查詢結(jié)果
def ExecQuery(self,sql):"""執(zhí)行查詢語句返回的是一個(gè)包含tuple的list,list的元素是記錄行,tuple的元素是每行記錄的字段"""cur = self.__GetConnect()#執(zhí)行sql語句,獲取所有數(shù)據(jù)print(sql)cur.execute(sql)#resList是list,而其中的每個(gè)元素是 tupleresList = cur.fetchall()#查詢完畢后必須關(guān)閉連接self.conn.close()return resList5 SQL非查詢操作
輸入SQL操作語句–>執(zhí)行sql語句–>關(guān)閉數(shù)據(jù)庫連接–>返回查詢結(jié)果
請(qǐng)注意一定要有conn.commit()這句來提交事務(wù),要不然不能真正的插入數(shù)據(jù)。
def ExecNonQuery(self,sql):"""執(zhí)行非查詢語句"""cur = self.__GetConnect()#執(zhí)行sql語句,修改數(shù)據(jù)cur.execute(sql)#執(zhí)行sql語句self.conn.commit()self.conn.close()6 一份完整的SQL數(shù)據(jù)庫操作代碼
#在這里進(jìn)行SQL數(shù)據(jù)庫操作import pymssqlclass MSSQL:def __init__(self,host,user,pwd,db):self.host = hostself.user = userself.pwd = pwdself.db = dbdef __GetConnect(self):"""得到連接信息返回: conn.cursor()"""if not self.db:raise(NameError,"沒有設(shè)置數(shù)據(jù)庫信息")self.conn = pymssql.connect(host=self.host,user=self.user,password=self.pwd,database=self.db,charset="utf8")#打開游標(biāo)cur = self.conn.cursor()if not cur:raise(NameError,"連接數(shù)據(jù)庫失敗")else:return curdef ExecQuery(self,sql):"""執(zhí)行查詢語句返回的是一個(gè)包含tuple的list,list的元素是記錄行,tuple的元素是每行記錄的字段"""cur = self.__GetConnect()#執(zhí)行sql語句,獲取所有數(shù)據(jù)print(sql)cur.execute(sql)#resList是list,而其中的每個(gè)元素是 tupleresList = cur.fetchall()#查詢完畢后必須關(guān)閉連接self.conn.close()return resListdef ExecNonQuery(self,sql):"""執(zhí)行非查詢語句"""cur = self.__GetConnect()#執(zhí)行sql語句,修改數(shù)據(jù)cur.execute(sql)#執(zhí)行sql語句self.conn.commit()self.conn.close()def main(opType,sql):#獲取數(shù)據(jù)庫對(duì)象ms = MSSQL(host="Test",user="sa",pwd="123",db="YSZYGLXT2")if opType == "YES":#進(jìn)行數(shù)據(jù)庫查詢resList = ms.ExecQuery(sql)# for (account,password,typeData) in resList:return resListelif opType == "NO":#進(jìn)行非查詢操作ms.ExecNonQuery(sql)else:print("程序在判斷操作類型處出錯(cuò)!請(qǐng)檢查,出錯(cuò)地址:SQLOperation.py文件")if __name__ == '__main__':main()總結(jié)
以上是生活随笔為你收集整理的Python学习之GUI--SQL数据库连接的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: gitHub使用记录
- 下一篇: urllib.parse包学习