本文章主要來說python對mysql數(shù)據(jù)庫的基本操作,,前提是已搭建python環(huán)境和Mysql數(shù)據(jù)庫的環(huán)境,,這里不再詳細介紹。在python的命令行環(huán)境下看是否可以導(dǎo)入MySQLdb,,如果可以導(dǎo)入并且無任何的錯誤提示,,表示已經(jīng)安裝成功了,見截圖: 已經(jīng)很成功的安裝了python操作mysql的數(shù)據(jù)庫,,在這里,,我們詳細的介紹對python對mysql的增加,刪除,,修改和查詢的基本操作,,這里使用的數(shù)據(jù)庫名稱是“day2017”,,我們對數(shù)據(jù)庫的操作,首先是創(chuàng)建數(shù)據(jù)庫,,然后是在數(shù)據(jù)庫中創(chuàng)建表,,在這里,表的名稱為:userInfo,,見創(chuàng)建好的表字段信息: 創(chuàng)建好數(shù)據(jù)庫以及創(chuàng)建好了數(shù)據(jù)庫中的表以后,,下來開始操作數(shù)據(jù)庫,操作數(shù)據(jù)庫的第一步當(dāng)然是連接數(shù)據(jù)庫,,然后是創(chuàng)建游標(biāo),,接下來是對數(shù)據(jù)庫的各種操作,,這里我們先來操作Insert數(shù)據(jù)的操作,,見實現(xiàn)的代碼: 查看數(shù)據(jù)庫,可以看到,,數(shù)據(jù)已經(jīng)插入到數(shù)據(jù)庫中,,見查詢的結(jié)果: 在上面的案例中,只是插入了單條數(shù)據(jù),,實際上,,某些時候,會插入多條數(shù)據(jù),,也就是批量插入,,批量插入實現(xiàn)的代碼為: 接下來,,我們來查看數(shù)據(jù)庫的查詢,數(shù)據(jù)查詢分為二種,,一種是查詢的結(jié)果是一條語句,,使用的是fetchone()方法,另外一種是查詢的數(shù)據(jù)結(jié)果是多條,,使用的方法是fetchmany(),,我們分別來看這二個方法的使用,我們先來看單條數(shù)據(jù)的查詢,,見實現(xiàn)的代碼: 多條數(shù)據(jù)的查詢,,見實現(xiàn)的代碼: 下面我們來看更新語句的測試,見實現(xiàn)的代碼: 最后一步,,也就是刪除數(shù)據(jù)了,,直接看如下的實現(xiàn)代碼: 事實上,對于如上操作數(shù)據(jù)庫的方式,有很多的代碼是可以重夠的,,比如連接數(shù)據(jù)庫的方式,,另外,我們可以把操作數(shù)據(jù)庫的方式寫在一個類里面,,在業(yè)務(wù)調(diào)用的時候直接調(diào)用我們的數(shù)據(jù)庫方法進行操作,,見下面操作mysql數(shù)據(jù)庫的方法,見源碼: #!/usr/bin/env python #coding:utf-8 import MySQLdb class MySQLHelper(object): def __init__(self): pass def get_one(self,sql,params): conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='server',db='day2017') cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) retCount = cur.execute(sql,params) data = cur.fetchone() cur.close() conn.close() return data def get_many(self,sql,params): conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='server',db='day2017') cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) retCount = cur.execute(sql,params) data = cur.fetchall() cur.close() conn.close() return data def insert_one(self,sql,params): conn = MySQLdb.connect(host='127.0.0.1', user='root', passwd='server', db='day2017') cur = conn.cursor() cur.execute(sql, params) conn.commit() cur.close() return u'插入數(shù)據(jù)庫成功' def insert_many(self,sql,params): conn = MySQLdb.connect(host='127.0.0.1', user='root', passwd='server', db='day2017') cur = conn.cursor() cur.executemany(sql, params) conn.commit() cur.close() return u'批量插入數(shù)據(jù)庫成功' def update_one(self,sql,params): conn = MySQLdb.connect(host='127.0.0.1', user='root', passwd='server', db='day2017') cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) ret = cur.execute(sql, params) conn.commit() cur.close() conn.close() return u'更新數(shù)據(jù)庫成功' def delete_one(self,sql,params): conn = MySQLdb.connect(host='127.0.0.1', user='root', passwd='server', db='day2017') cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) ret = cur.execute(sql, params) conn.commit() cur.close() conn.close() return u'刪除數(shù)據(jù)庫成功' 把連接數(shù)據(jù)庫部分進行重構(gòu),,放到一個config.py的文件中,,這樣我們連接數(shù)據(jù)庫的方法就只需要在config.py文件維護了,而不需要在如上代碼中每個都得看的修改,,這實在是很糟糕,,見重構(gòu)后的config.py文件源碼: #!/usr/bin/env python #coding:utf-8 conn_dict=dict(host='127.0.0.1', user='root', passwd='server', db='day2017') 見重構(gòu)后操作mysql的數(shù)據(jù)庫方法,見源碼: #!/usr/bin/env python #coding:utf-8 import MySQLdb import config class MySQLHelper(object): def __init__(self): self.conn=config.conn_dict def get_one(self,sql,params): conn = MySQLdb.connect(**self.conn) cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) retCount = cur.execute(sql,params) data = cur.fetchone() cur.close() conn.close() return data def get_many(self,sql,params): conn = MySQLdb.connect(**self.conn) cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) retCount = cur.execute(sql,params) data = cur.fetchall() cur.close() conn.close() return data def insert_one(self,sql,params): conn = MySQLdb.connect(**self.conn) cur = conn.cursor() cur.execute(sql, params) conn.commit() cur.close() return u'插入數(shù)據(jù)庫成功' def insert_many(self,sql,params): conn = MySQLdb.connect(**self.conn) cur = conn.cursor() cur.executemany(sql, params) conn.commit() cur.close() return u'批量插入數(shù)據(jù)庫成功' def update_one(self,sql,params): conn = MySQLdb.connect(**self.conn) cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) ret = cur.execute(sql, params) conn.commit() cur.close() conn.close() return u'更新數(shù)據(jù)庫成功' def delete_one(self,sql,params): conn = MySQLdb.connect(**self.conn) cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) ret = cur.execute(sql, params) conn.commit() cur.close() conn.close() return u'刪除數(shù)據(jù)庫成功' 寫數(shù)據(jù)庫的操作方法,,是為了進行對業(yè)務(wù)的操作,,要不僅僅寫這些沒什么實際的意義,如我們實現(xiàn)輸入用戶名和密碼,,在在數(shù)據(jù)庫中驗證,,如果用戶名和密碼都是admin,那么通過,如果有其中一個不是admin,就提示用戶,,請?zhí)崾居脩粲脩裘蛘呙艽a錯誤,,下面來實現(xiàn)這樣的一個過程,,見實現(xiàn)的源碼: #!/usr/bin/env python #coding:utf-8 import MySQLdb import config class MySQLHelper(object): def __init__(self): self.conn=config.conn_dict def get_one(self,sql,params): conn = MySQLdb.connect(**self.conn) cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) retCount = cur.execute(sql,params) data = cur.fetchone() cur.close() conn.close() return data def get_many(self,sql,params): conn = MySQLdb.connect(**self.conn) cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) retCount = cur.execute(sql,params) data = cur.fetchall() cur.close() conn.close() return data def insert_one(self,sql,params): conn = MySQLdb.connect(**self.conn) cur = conn.cursor() cur.execute(sql, params) conn.commit() cur.close() return u'插入數(shù)據(jù)庫成功' def insert_many(self,sql,params): conn = MySQLdb.connect(**self.conn) cur = conn.cursor() cur.executemany(sql, params) conn.commit() cur.close() return u'批量插入數(shù)據(jù)庫成功' def update_one(self,sql,params): conn = MySQLdb.connect(**self.conn) cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) ret = cur.execute(sql, params) conn.commit() cur.close() conn.close() return u'更新數(shù)據(jù)庫成功' def delete_one(self,sql,params): conn = MySQLdb.connect(**self.conn) cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) ret = cur.execute(sql, params) conn.commit() cur.close() conn.close() return u'刪除數(shù)據(jù)庫成功' class CheckUserInfo(object): def __init__(self): self.__helper=MySQLHelper() def checkValid(self,username,password): sql='select * from userInfo where username=%s and password=%s' params=(username,password) return self.__helper.get_one(sql,params) def info(): username=raw_input(u'請輸入你的用戶名:\n') password=raw_input(u'請輸入你的密碼:\n') userInfo=CheckUserInfo() result=userInfo.checkValid(username,password) if not result: print u'用戶名或者密碼錯誤,,請聯(lián)系管理員' else: print u'恭喜您,輸入正確,!' if __name__=='__main__': info() |
|