資源簡介
python 的mysql 數據庫封裝。直接IMPORT使用即可。
實現了insert\update\delete\getone\getall,自動連接和關閉數據庫
代碼片段和文件信息
import?pymysql
class?MysqlHelper(object):
????conn?=?None
????def?__init__(self?host?username?password?db?charset=‘utf8‘?port=3306):
????????self.host?=?host
????????self.username?=?username
????????self.password?=?password
????????self.db?=?db
????????self.charset?=?charset
????????self.port?=?port
????def?connect(self):
????????self.conn?=?pymysql.connect(host=self.host?port=self.port?user=self.username?password=self.password
????????????????????????????????????db=self.db
????????????????????????????????????charset=self.charset)
????????self.cursor?=?self.conn.cursor()
????def?close(self):
????????self.cursor.close()
????????self.conn.close()
????def?get_one(self?sql?params=()):
????????result?=?None
????????try:
????????????self.connect()
?
評論
共有 條評論