#通过python数据库操作的方式获取你的方维用户名信息
#把这个过程封装成一个函数,把sql和数据库连接信息都当做参数传递,这个函数需要返回查询出来的结果数据
#选做:在传一个参数 用来判断是查询操作,删除操作还是修改操作
# class Mysqldf:
import pymysql
def sqldf(a=1,host='127.0.0.1',user='root',password='',database='clublff',port=3306,charset='utf8',*sql,**kw):
# 创建数据库连接
# global mysqllist
# mysqllist = list(sql)
# print(mysqllist)
# conn = pymysql.connect(host=mysqllist[0],
# user=mysqllist[1],
# password=mysqllist[2],
# database=mysqllist[3],
# port=mysqllist[4],
# charset=mysqllist[5]
# )
conn = pymysql.connect(host=host,
user=user,
password=password,
database=database,
port=port,
charset=charset
)
# 创建游标
data = conn.cursor()
def select(ziduan="*",table='student'):
sql_select = "select "+ziduan+" from "+table+";"
data.execute(sql_select)
d2 = data.fetchall()
# print(d2)
conn.commit()
return d2
def updata(table='student',ziduan='age=99',wheret='sid=1'):
up_sql = "update " + table + " set " + ziduan + " where " + wheret + ";"
data.execute(up_sql)
d3 = data.fetchall()
print(d3)
conn.commit()
return d3
def delete(table='student',wheret='sid=11'):
del_sql = "delete from " + table + " where " + wheret + ";"
data.execute(del_sql)
d4 = data.fetchall()
print(d4)
conn.commit()
return d4
if a==1:
select(ziduan="name",table='student')
data1=select(ziduan="sid",table='student')
print(data1)
elif a==2:
updata()
print(updata())
elif a==3:
delete()
print(delete())
else:
print('输入数据有错误')
# 关闭游标
data.close()
# 关闭连接
conn.close()
a=int(input('输入1.查询数据 2.修改数据3.删除数据 :'))
sqldf(a,'localhost','root','','clublff',3306,'utf8')
# print(sqldf(1,'localhost','root','','clublff',3306,'utf8'))
# 'localhost','root','','clublff',3306, 'utf-8'
python操作数据库函数,修改参数