python3.6连接mysql数据库及增删改查操作详解

作者:E1ffC1 时间:2024-01-23 22:27:27 

折腾好半天的数据库连接,由于之前未安装 pip ,而且自己用的python 版本为3.6. 只能用 pymysql 来连接数据库,下边 简单介绍一下 连接的过程,以及简单的增删改查操作。

1.通过 pip 安装 pymysql

进入 cmd  输入  pip install pymysql  

回车等待安装完成;

python3.6连接mysql数据库及增删改查操作详解

安装完成后出现如图相关信息,表示安装成功。

2.测试连接


import pymysql #导入 pymysql

如果编译未出错,即表示 pymysql 安装成功

简单的增删改查操作

示例表结构

python3.6连接mysql数据库及增删改查操作详解

2.1查询操作i


import pymysql #导入 pymysql

#打开数据库连接
db= pymysql.connect(host="localhost",user="root",
password="123456",db="test",port=3307)

# 使用cursor()方法获取操作游标
cur = db.cursor()

#1.查询操作
# 编写sql 查询语句 user 对应我的表名
sql = "select * from user"
try:
cur.execute(sql) #执行sql语句

results = cur.fetchall()#获取查询的所有记录
print("id","name","password")
#遍历结果
for row in results :
id = row[0]
name = row[1]
password = row[2]
print(id,name,password)
except Exception as e:
raise e
finally:

2.2插入操作


import pymysql
#2.插入操作
db= pymysql.connect(host="localhost",user="root",
password="123456",db="test",port=3307)

# 使用cursor()方法获取操作游标
cur = db.cursor()

sql_insert ="""insert into user(id,username,password) values(4,'liu','1234')"""

try:
cur.execute(sql_insert)
#提交
db.commit()
except Exception as e:
#错误回滚
db.rollback()
finally:
db.close()

2.3更新操作


import pymysql
#3.更新操作
db= pymysql.connect(host="localhost",user="root",
password="123456",db="test",port=3307)

# 使用cursor()方法获取操作游标
cur = db.cursor()

sql_update ="update user set username = '%s' where id = %d"

try:
cur.execute(sql_update % ("xiongda",3)) #像sql语句传递参数
#提交
db.commit()
except Exception as e:
#错误回滚
db.rollback()
finally:
db.close()

2.4删除操作


import pymysql
#4.删除操作
db= pymysql.connect(host="localhost",user="root",
password="123456",db="test",port=3307)

# 使用cursor()方法获取操作游标
cur = db.cursor()

sql_delete ="delete from user where id = %d"

try:
cur.execute(sql_delete % (3)) #像sql语句传递参数
#提交
db.commit()
except Exception as e:
#错误回滚
db.rollback()
finally:
db.close()

来源:https://blog.csdn.net/qq_37176126/article/details/72824106

标签:python,mysql,pymysql
0
投稿

猜你喜欢

  • python实现复制整个目录的方法

    2023-04-08 18:14:47
  • Python如何基于Tesseract实现识别文字功能

    2022-11-09 11:52:18
  • 朴素贝叶斯算法的python实现方法

    2023-03-01 07:05:53
  • python基础教程之实现石头剪刀布游戏示例

    2022-02-09 15:41:11
  • MAC 中mysql密码忘记解决办法

    2024-01-18 04:13:48
  • python魔法方法之__setattr__()

    2021-06-06 13:27:47
  • python查看矩阵的行列号以及维数方式

    2021-03-25 20:24:58
  • php测试kafka项目示例

    2023-11-19 20:40:04
  • eval(function(p,a,c,k,e,d)系列解密javascript程序

    2024-04-19 10:01:20
  • python使用pandas实现数据分割实例代码

    2021-07-02 11:09:19
  • pip安装tensorflow的坑的解决

    2022-02-07 13:26:32
  • python中itertools模块zip_longest函数详解

    2023-01-02 09:09:35
  • python随机模块random的22种函数(小结)

    2022-08-11 18:09:03
  • 解决Python安装时报缺少DLL问题【两种解决方法】

    2023-12-27 01:49:12
  • Django框架中render_to_response()函数的使用方法

    2023-09-03 13:58:49
  • GO语言实现列出目录和遍历目录的方法

    2024-05-10 10:58:20
  • Python中的变量与常量

    2022-01-14 22:26:43
  • 用Python手把手教你实现2048小游戏

    2023-02-22 23:27:57
  • 详解python中的模块及包导入

    2023-12-05 08:20:16
  • 关于Python dict存中文字符dumps()的问题

    2022-08-22 17:53:17
  • asp之家 网络编程 m.aspxhome.com