import pymysql
conn = pymysql.connect(host='localhost', port=3306, user='root',password='123456',database='mydb17_pymysql', charset='utf8')
# 获取游标
cursor = conn.cursor()
# 执行SQL语句 返回值就是SQL语句在执行过程中影响的行数
sql = "select * from student;"
row_count = cursor.execute(sql)
print("SQL语句执行影响的行数%d" % row_count)
# 取出结果集中一行 返回的结果是一行
# print(cursor.fetchone())
# 取出结果集中的所有数据 返回一行数据
for line in cursor.fetchall():
print(line)
# 关闭游标
cursor.close()
# 关闭连接
conn.close()
import pymysql
#获取MySQL连接
conn = pymysql.connect(host='localhost', port=3306, user='root',password='123456',database='mydb17_pymysql', charset='utf8')
# 获取游标
cursor = conn.cursor()
# 执行SQL语句 返回值就是SQL语句在执行过程中影响的行数
sql = "select * from student;"
row_count = cursor.execute(sql)
print("SQL语句执行影响的行数%d" % row_count)
# 取出结果集中一行 返回的结果是一行
# print(cursor.fetchone())
# 取出结果集中的所有数据 返回一行数据
for line in cursor.fetchall():
print(line)
# 关闭游标
cursor.close()
# 关闭连接
conn.close()
import pymysql
#获取MySQL连接
conn = pymysql.connect(host='localhost', port=3306, user='root',password='123456',database='mydb17_pymysql', charset='utf8')
# 获取游标
cursor = conn.cursor()
#插入数据
# sql = "insert into student values(%s,%s,%s)"
# data = (4, '晁盖', 34)
# cursor.execute(sql, data) #sql和data之间以","隔开
# 修改数据
# sql = "update student set sname=%s where sid=%s"
# data = ('李逵', 4)
# cursor.execute(sql, data)
# 删除数据
sql = "delete from student where sid=%s"
data = (4)
cursor.execute(sql, data)
conn.commit() # 提交,不然无法保存插入或者修改的数据(这个一定不要忘记加上)
# 关闭游标
cursor.close()
# 关闭连接
conn.close()