python操作mysql数据库之"删改查"

import pymysql
conn = pymysql.connect(host='127.0.0.1',port=3306,user='root',passwd='990310',db = 'school',charset='utf8')
cursor = conn.cursor()
l = [
    ('鸭蛋1','',2),
    ('鸭蛋2', '', 2),
    ('鸭蛋3', '', 2)
]
#r = cursor.execute('insert into student(sname,gender,class_id) values(%s,%s,%s)',(inp,'女',1))
#r = cursor.executemany('insert into student(sname,gender,class_id) values(%s,%s,%s)',l)

#更新数据行
r = cursor.execute('update student set sname = %s where sid = %s',("马大狗",3))
conn.commit()
print(r)
#删除数据行
r = cursor.execute('delete from student where sid = %s',7)
conn.commit()

#查找数据库行
r = cursor.execute('select * from student')
print(r)
#fetchall()从内存中取出所有的数据
#result = cursor.fetchall()
#print(result)
result = cursor.fetchone()
print(result)
result = cursor.fetchone()
print(result)
cursor.scroll(0,mode='absolute')#回到数据库第1行
#scroll()还有另一个mode参数relative,表示相对移动数.
result = cursor.fetchone()
print(result)
posted @ 2019-12-28 11:25  Iceberg_710815  阅读(184)  评论(0编辑  收藏  举报