import pymysql
db=pymysql.connect(host='localhost',user='root',passwd='xxxxxx',db='pymysql',port=3306)
def select_one():
sql='select * from tb_user'
cursor=db.cursor()
cursor.execute(sql)
data=cursor.fetchone()
return data
def select_by_id(id):
sql='select * from tb_user where id=%d'%id
cursor=db.cursor()
cursor.execute(sql)
data=cursor.fetchone()
return data
def select_all():
sql = 'select * from tb_user'
cursor = db.cursor()
cursor.execute(sql)
datas= cursor.fetchall()
return datas
def delete_one(id):
sql='delete from tb_user where id=%d'%id
cursor=db.cursor()
cursor.execute(sql)
def insert_one():
sql='insert into tb_user(id,name,age) values(2,"wanchao",18)'
cursor=db.cursor()
cursor.execute(sql)
def update():
sql='update tb_user set name="gaohan" where name="wanchao"'
cursor=db.cursor()
cursor.execute(sql)
if __name__=='__main__':
print('查询一条语句:{}'.format(select_one()))
print('按id查询一条语句:{}'.format(select_by_id(2)))
print('查询多条语句:')
[ print(data) for data in select_all()]
print('删除:')
delete_one(2)
[print(data) for data in select_all()]
print('添加')
insert_one()
[print(data) for data in select_all()]
print('修改:')
update()
[print(data) for data in select_all()]