pymysql pymysql2.py

"""
功能:pymysql 的使用。
知识点:
1.
数据库的“增”,“删”,“改”,“查”。
除了“查”,都需要提交操作,即,commit()。
# cursor.execute('commit;')
db.commit()
"""
import pymysql

# 建立连接
db = pymysql.connect('localhost', 'root', '123456', 'test')
# 使用cursor()方法创建一个游标对象cursor
cursor = db.cursor()

sql2 = 'select * from customers;'
cursor.execute(sql2)
data = cursor.fetchall()
# print("data:", data)
for row in data:
    print(row)
print()

# 更改customers表中的最低工资,提高10%。
sql3 = 'update customers set salary = salary * 1.1\
        where salary = (select min(salary) from customers2); '
cursor.execute(sql3)
# sql4 = 'commit;'
# cursor.execute(sql4)
db.commit()
print()

cursor.execute(sql2)
data = cursor.fetchall()
for row in data:
    print(row)


发布了198 篇原创文章 · 获赞 58 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/weixin_42193179/article/details/103859824