在Python 3中,我们通常使用纯Python的三方库PyMySQL来访问MySQL数据库,它应该是目前Python操作MySQL数据库最好的选择。

    1. 安装PyMySQL。

    2. 添加一个部门。

      ```Python import pymysql

    if name == ‘main‘: main()

    1. 3. 删除一个部门。
    2. ```Python
    3. import pymysql
    4. def main():
    5. no = int(input('编号: '))
    6. database='hrs', charset='utf8',
    7. autocommit=True)
    8. try:
    9. with con.cursor() as cursor:
    10. result = cursor.execute(
    11. 'delete from tb_dept where dno=%s',
    12. (no, )
    13. )
    14. if result == 1:
    15. print('删除成功!')
    16. finally:
    17. if __name__ == '__main__':
    18. main()
    1. 更新一个部门。

      ```Python import pymysql

    if name == ‘main‘: main()

    1. 分页查询员工信息。

      ```Python import pymysql from pymysql.cursors import DictCursor

    class Emp(object):

    1. def __init__(self, no, name, job, sal):
    2. self.no = no
    3. self.name = name
    4. self.job = job
    5. self.sal = sal
    6. return f'\n编号:{self.no}\n姓名:{self.name}\n职位:{self.job}\n月薪:{self.sal}\n'

    if name == ‘main‘: main() ```