使用软件:Navicat for MySQL、PyCharm Community Edition 2021.3.3
一、pycharm安装pymysql包
第一步 点击“File”——点击“Settings”
第二步 点击“Python Interpreter”——点击“+”
第三步 搜索“pymysql”——点击“Install Package” ,下载成功即可
二、使用Python的三方库PyMySQL来访问MySQL数据库
1、准备数据库
drop database if exists sxs;
create database sxs default charset utf8;
use sxs;
drop table if exists s_student;
create table s_student
(
dnum int not null comment '学号',
dname varchar(10) not null comment '姓名',
dclass varchar(20) not null comment '班级',
primary key (dnum)
);
insert into s_student values
(100, 'candy', '1班'),
(101, 'jeny', '3班'),
(102, 'marry', '2班'),
(103, 'jane', '2班');
2、使用pymysql增加数据
import pymysql
def main():
num = int(input('学号: '))
name = input('名字: ')
class1 = input('班级: ')
# 1. 创建数据库连接对象
con = pymysql.connect(host=&