import pymysql
class Cpymysql():
def __init__(self):
self.conn=pymysql.connect(host='127.0.0.1',port=3306,user='root',password='1111',database='atm_woniu')
self.cur=self.conn.cursor()
# def chage_db(sql):
def change_db(self,sql):
# 执行sql语句
self.cur.execute(sql)
# 提交事务
self.conn.commit()
# 查询
def query_db(self,sql,mod):
# 执行sql语句
self.cur.execute(sql)
# 获取数据
if mod == 'all':
result01 = self.cur.fetchall()
elif mod == 'one':
result01 = self.cur.fetchone()
elif mod == 'many':
result01 = self.cur.fetchmany()
return result01
# 断开连接
def __del__(self):
self.cur.close()
self.conn.close()
name="admin"
mysql = "SELECT password FROM user where username='%s';" %name
cp=Cpymysql()
x=cp.query_db(mysql,'one')
print(x)