import pymysql
class OperationDatabase():
def __init__(self,Ip,User,PassWd,DBname):
self.ip=Ip
self.user=User
self.passwd=PassWd
self.dbname=DBname
def ConnectDatabase(self):
self.db = pymysql.connect(self.ip,self.user,self.passwd,self.dbname)
self.cursor = self.db.cursor()
def SelectDatabase(self,sql):
self.ConnectDatabase()
result=()
try:
self.cursor.execute(sql)
result=self.cursor.fetchall()#获取全部数据
except:
print("查询失败")
return result
self.CloseDataBase()
def SelectDatabase_Row(self,sql):
self.ConnectDatabase()
result=None
try:
self.cursor.execute(sql)
result=self.cursor.fetchone()#获取单条数据
#print(result)
except:
print("查询失败")
return result
self.CloseDataBase()
def InsertDatabase(self,sql):
return self.__edit(sql)
def UpdateDatabase(self,sql):
return self.__edit(sql)
def DeleteDatabase(self,sql):
return self.__edit(sql)
def __edit(self,sql):
self.ConnectDatabase()
try:
self.cursor.execute(sql)
self.db.commit()
except:
print("提交失败")
self.db.rollback()
self.CloseDataBase()
def CloseDataBase(self):
self.cursor.close()
self.db.close()
#实例
sql="SELECT * from db_p2p.t_users_info where id=6214771006504961;"
c=OperationDatabase("数据库服务器ip", "用户名", 密码", "数据库名字")
c.SelectDatabase(sql)