2021年4月4日

时间:1个小时左右

代码:200行左右

博客:1

知识点:疫情数据入库后的增删改查操作

import pymysql

from day_04 import utils


# 查询

def query():
    # 获取数据库的连接
    conn, cursor = utils.get_conn()
    sql = 'select * from student'
    cursor.execute(sql)
    connect = cursor.fetchall()
    print(connect)
    # 释放资源
    utils.close(conn, cursor)


# 插入数据
def insert():
    # 获取数据库的连接
    conn, cursor = utils.get_conn()
    sql = 'insert into student values(null,"siyu",17,"python",78)'
    cursor.execute(sql)

    # 提交事务
    conn.commit()
    # 释放资源
    utils.close(conn, cursor)


# 更新数据

def updata():
    # 获取数据库的连接
    conn, cursor = utils.get_conn()
    sql = 'update student set name="wangwu" where id=2'
    cursor.execute(sql)
    conn.commit()
    # 释放资源
    utils.close(conn, cursor)


# 删除

def delete():
    # 获取数据库的连接
    conn, cursor = utils.get_conn()
    sql = 'delete from student where id=2'
    cursor.execute(sql)
    conn.commit()
    # 释放资源
    utils.close(conn, cursor)


if __name__ == "__main__":
    query()
    insert()
    # delete()
    # query()
原文地址:https://www.cnblogs.com/j-y-s/p/14903254.html