标签:mysql into etc 物理 一个 rom fetchall import mys
import pymysql
try:
conn=pymysql.Connect(
host="localhost",
port=3306,
db="er",
user="root",
passwd="password",
charset="utf8"
)
#2.创建游标对象cursor
cur=conn.cursor()
# #增加一条数据 科目表加上爬虫
# insert_sub=‘insert into subjects values(0,"爬虫")‘
# result=cur.execute(insert_sub)
# #修改
# update_sub=‘update subjects set title="物理" where id=3‘
# result = cur.execute(update_sub)
#删除 sub表有外键关联,删不掉,不用管,记住操作就行
# delete_sub=‘delete from subjects where id=4‘
# result=cur.execute(delete_sub)
# print(result)
#fetchall接收所有的结果 只能fetch一次,第二次结果就是None
#一行数据一个元组
cur.execute(‘select * from subjects‘)
res=cur.fetchall()
print(res) #((1, ‘语文‘), (2, ‘数学‘), (3, ‘物理‘), (4, ‘科学‘), (6, ‘爬虫‘))
#fetchone:每次一行数据
#提交事务
conn.commit()
#关闭游标
cur.close()
#关闭链接
conn.close()
except Exception as e:
print(e)
标签:mysql into etc 物理 一个 rom fetchall import mys
原文地址:https://www.cnblogs.com/wuweixiong/p/12746310.html