python操作mysql数据库之"删改查"
时间:2021-07-01 10:21:17
帮助过:3人阅读
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)
python操作mysql数据库之"删改查"
标签:comm fetchall _id from print 操作 int exe imp