from pymysql import connect
def check_data(cur): sql = "select * from student;" # sql语句 cur.execute(sql) # 执行sql语句 # 获取查询的所有记录 result = cur.fetchall() print(result) for value in result: print(value)
def insert_data(cur, conn): sql = """insert into student value(0,"小美",1990-01-02,"女");""" cur.execute(sql) conn.commit() # python中默认开启事务,必须提交数据后才能完成修改 def main(): # 链接数据库 conn = connect(host="localhost", user="root", password="MySQL", db="python_1", port=3306) # 使用cursor获取游标对象 cur = conn.cursor() # 查询数据库信息
check_data(cur) # 插入数据操作
insert_data(cur, conn) # 关闭游标对象 cur.close() # 关闭链接 conn.close() if __name__ == "__main__": main() 删除和修改都还不多,只是对应的sql语句不一样。这里就不做赘述!
原文地址:https://www.cnblogs.com/lzb888/p/11140960.html
时间: 2024-10-09 04:05:24