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

时间:2021-08-01 04:21:11

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)