在Python 3中,我们通常使用纯Python的三方库PyMySQL来访问MySQL数据库,它应该是目前Python操作MySQL数据库最好的选择。
安装PyMySQL。
添加一个部门。
```Python import pymysql
if name == ‘main‘: main()
3. 删除一个部门。
```Python
import pymysql
def main():
no = int(input('编号: '))
database='hrs', charset='utf8',
autocommit=True)
try:
with con.cursor() as cursor:
result = cursor.execute(
'delete from tb_dept where dno=%s',
(no, )
)
if result == 1:
print('删除成功!')
finally:
if __name__ == '__main__':
main()
更新一个部门。
```Python import pymysql
if name == ‘main‘: main()
分页查询员工信息。
```Python import pymysql from pymysql.cursors import DictCursor
class Emp(object):
def __init__(self, no, name, job, sal):
self.no = no
self.name = name
self.job = job
self.sal = sal
return f'\n编号:{self.no}\n姓名:{self.name}\n职位:{self.job}\n月薪:{self.sal}\n'
if name == ‘main‘: main() ```