参考大佬
https://blog.youkuaiyun.com/yoggieCDA/article/details/99971149?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase
连接MySQL和python
本文涉及到的开发环境:
操作系统 Windows 10
数据库 MySQL 8.0
Python 3.7.2
pip 19.0.3
步骤:
连接数据库
生成游标对象
执行SQL语句
关闭游标
关闭连接
先调出Windows PowerShell窗口
安装pip install PyMySQL
MySQL数据库已安装,且已建好名为test的数据库,其中有名为student的表
利用 mysql.connector连接数据库
import pymysql
#连接数据库
conn=pymysql.connect(host = ‘127.0.0.1’ # 连接名称,默认127.0.0.1
,user = ‘root’ # 用户名
,passwd=‘password’ # 密码
,port= 3306 # 端口,默认为3306
,db=‘test’ # 数据库名称
,charset=‘utf8’ # 字符编码
)
cur = conn.cursor() # 生成游标对象
sql="select * from student " # SQL语句
cur.execute(sql) # 执行SQL语句
data = cur.fetchall() # 通过fetchall方法获得数据
for i in data[:2]: # 打印输出前2条数据
print (i)
cur.close() # 关闭游标
conn.close() # 关闭连接
Python对MySql数据库实现增删改查
增
import pymysql
#连接数据库
conn=pymysql.connect(host = ‘127.0.0.1’ # 连接名称,默认127.0.0.1
,user = ‘root’ # 用户名
,passwd=‘password’ # 密码
,port= 3306 # 端口,默认为3306
,db=‘test’ # 数据库名称
,charset=‘utf8’ # 字符编码
)
cur = conn.cursor() # 生成游标对象
#=插入语句===================
sql= “INSERT INTO student VALUES (‘p’,‘魏六’,‘17’)”
#===================================================
try:
cur.execute(sql1) # 执行插入的sql语句
conn.commit() # 提交到数据库执行
except:
coon.rollback()# 如果发生错误则回滚
conn.close() # 关闭数据库连接
然后我们再运行查询语句
import mysql.connector
conn=mysql.connector.connect(host = ‘127.0.0.1’ # 连接名称,默认127.0.0.1
,user = ‘root’ # 用户名
,passwd=‘password’ # 密码
,port= 3306 # 端口,默认为3306
,db=‘test’ # 数据库名称
,charset=‘utf8’ # 字符编码
)
cur = conn.cursor() # 生成游标对象
sql="select * from student " # SQL语句
cur.execute(sql) # 执行SQL语句
data = cur.fetchall() # 通过fetchall方法获得数据
for i in data[:]: # 打印输出所有数据
print (i)
cur.close() # 关闭游标
conn.close() # 关闭连接
执行结果就是
(‘b’, ‘钱二’, ‘16’)
(‘c’, ‘张三’, ‘17’)
(‘d’, ‘李四’, ‘17’)
(‘e’, ‘王五’, ‘16’)
(‘a’, ‘赵大’, ‘16’)
(‘p’, ‘魏六’, ‘17’)
查
import mysql.connector
conn=mysql.connector.connect(host = ‘127.0.0.1’ # 连接名称,默认127.0.0.1
,user = ‘root’ # 用户名
,passwd=‘password’ # 密码
,port= 3306 # 端口,默认为3306
,db=‘test’ # 数据库名称
,charset=‘utf8’ # 字符编码
)
cur = conn.cursor() # 生成游标对象
sql="select * from student " # SQL语句
cur.execute(sql) # 执行SQL语句
data = cur.fetchall() # 通过fetchall方法获得数据
for i in data[:]: # 打印输出所有数据
print (i)
cur.close() # 关闭游标
conn.close() # 关闭连接
执行结果就是
(‘b’, ‘钱二’, ‘16’)
(‘c’, ‘张三’, ‘17’)
(‘d’, ‘李四’, ‘17’)
(‘e’, ‘王五’, ‘16’)
(‘a’, ‘赵大’, ‘16’)
(‘p’, ‘魏六’, ‘17’)
删
import pymysql
#连接数据库
conn=pymysql.connect(host = ‘127.0.0.1’ # 连接名称,默认127.0.0.1
,user = ‘root’ # 用户名
,passwd=‘password’ # 密码
,port= 3306 # 端口,默认为3306
,db=‘test’ # 数据库名称
,charset=‘utf8’ # 字符编码
)
cur = conn.cursor() # 生成游标对象
#=删除语句===================
sql = "DELETE FROM student WHERE 学号 = “a”
#===================================================
try:
cur.execute(sql) # 执行插入的sql语句
conn.commit() # 提交到数据库执行
except:
coon.rollback()# 如果发生错误则回滚
conn.close() # 关闭数据库连接
改
import pymysql
#连接数据库
conn=pymysql.connect(host = ‘127.0.0.1’ # 连接名称,默认127.0.0.1
,user = ‘root’ # 用户名
,passwd=‘password’ # 密码
,port= 3306 # 端口,默认为3306
,db=‘test’ # 数据库名称
,charset=‘utf8’ # 字符编码
)
cur = conn.cursor() # 生成游标对象
#=删除语句===================
sql ="UPDATE student SET 学员姓名 = ‘欧阳’ WHERE 学号 = ‘b’ "
#===================================================
try:
cur.execute(sql) # 执行插入的sql语句
conn.commit() # 提交到数据库执行
except:
coon.rollback()# 如果发生错误则回滚
conn.close() # 关闭数据库连接
查
import pymysql
#连接数据库
conn=pymysql.connect(host = ‘127.0.0.1’ # 连接名称,默认127.0.0.1
,user = ‘root’ # 用户名
,passwd=‘password’ # 密码
,port= 3306 # 端口,默认为3306
,db=‘test’ # 数据库名称
,charset=‘utf8’ # 字符编码
)
cur = conn.cursor() # 生成游标对象
#=删除语句===================
sql="select * from student " # SQL语句
#====================================================
try:
cur.execute(sql) # 执行插入的sql语句
data = cur.fetchall()
for i in data[:]:
print (i)
conn.commit() # 提交到数据库执行
except:
coon.rollback()# 如果发生错误则回滚
conn.close() # 关闭数据库连接