#!/usr/bin/env python3
#-*- encoding:utf-8 -*-
import pymysql
#mysql 連接類
class mysqlCommand():
def __init__(self,host,port,user,passwd,db):
self.conn = pymysql.connect(host = host , port = port , user = user , passwd = passwd , db = db)
def connects(self):
return self.conn
#關(guān)閉mysql連接
def close(self):
self.conn.close()
增寂纪、刪、改兄朋、查類(只完成部分徐块,后面會完善)
#!/usr/bin/env python3
#-*- coding:utf-8-*-
#mysql 映射類
class mapper_abstract():
def __init__(self,object):
self.conn = object
def cur(self):
cur = self.conn.cursor()
return cur
#查找
def findAll(self,table):
try:
cur = self.cur()
cur.execute("SELECT * FROM %s"%(table))
result = cur.fetchall()
return result
except:
print ('mysql error')
#刪除
def dels(self,table,ids):
sql="delete from `%s` where id='%d'" % (table,ids)
try:
cur = self.cur()
cur.execute(sql)
self.conn.commit()
except:
self.conn.rollback()
類的實(shí)例化調(diào)用示例
#!/usr/bin/env python3
#-*- coding:utf-8-*-
from mysql import mysqlCommand
from mapper import mapper_abstract
db = mysqlCommand('127.0.0.1',3306 ,'root' ,'pwd' ,'table')
conn = db.connects()
mapper = mapper_abstract(conn)
result = mapper.findAll('blog_categray')
for each in result:
print (each)