1,首先在电脑输入 win+r ,在命令框中输入cmd
2,在终端输入 pip install mysql-connector
pip install mysql-connector
3, 安装完成后,打开pycharm进行链接
import mysql.connector
#创建连接对象
db=mysql.connector.connect(host='localhost',
user='root',
passwd='root',
database='mytestdb1',
auth_plugin='mysql_native_password')
#print(conn)
mycursor=db.cursor()
#编写sql语句
sql='insert into student (id,sname,sclass) values (%s,%s,%s)'
val=(201222,'张三','理科1班')
#执行sql语句
mycursor.execute(sql,val)
#提交
conn.commit()
print(mycursor.rowcount,'记录插入成功')
(1)host是我们的主机,也可以是端口号
(2)user是用户名
(3)passwd是密码
(4)database是我们在MySQL中的数据库
(5)auth_plugin是保证我们链接
(6)sql就是我们所编写的sql的语言,上图中就是sql语句的插入语句
(7)mycursor.execute()进行一条语句的执行