python3.6使用pymysql连接Mysql数据库

作者:SmallTankPy 时间:2024-01-27 13:00:48 

python3.6使用pymysql连接Mysql数据库及简单的增删改查操作,供大家参考,具体内容如下

折腾好半天的数据库连接,由于之前未安装pip ,而且自己用的Python 版本为3.6. 只能用 pymysql 来连接数据库,(如果有和我一样未安装 pip 的朋友请 点这里windows下python安装pip简易教程),下边简单介绍一下连接的过程,以及简单的增删改查操作。

1.通过pip 安装pymysql

进入 cmd  输入  pip install pymysql 
回车等待安装完成;

python3.6使用pymysql连接Mysql数据库

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

2.测试连接

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

简单的增删改查操作

示例表结构

python3.6使用pymysql连接Mysql数据库

2.1查询操作


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:
db.close() #关闭连接

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/SmallTankPy/article/details/76064251

标签:python,pymysql,mysql
0
投稿

猜你喜欢

  • mysql中如何对列求和

    2024-01-16 12:28:48
  • Bootstrap Table的使用总结

    2024-03-14 01:53:06
  • Pyqt助手安装PyQt5帮助文档过程图解

    2021-01-31 00:34:06
  • IE6下图片下方有空隙的解决方法

    2008-08-01 18:02:00
  • JavaScript实现大文件上传的示例代码

    2024-05-28 15:40:23
  • vue3缓存页面keep-alive及路由统一处理详解

    2024-05-02 16:34:12
  • python的变量和简单数字类型详解

    2021-01-10 15:34:40
  • 微信小程序实现列表下拉刷新上拉加载

    2024-05-21 10:11:26
  • Python实现将DNA序列存储为tfr文件并读取流程介绍

    2021-08-04 08:02:23
  • pycharm中如何使用快捷键按出代码提示框

    2023-03-11 22:18:52
  • PyQt5实现进度条与定时器及子线程同步关联

    2023-03-20 14:48:35
  • JS+HTML5 canvas绘制验证码示例

    2023-07-19 11:23:53
  • python 实现两个npy档案合并

    2022-08-20 13:29:55
  • 使用Python中PDB模块中的命令来调试Python代码的教程

    2021-04-28 21:22:57
  • python元组简单介绍

    2023-07-31 18:06:12
  • jsp/javascript打印九九乘法表代码

    2024-03-23 02:23:17
  • python 多维高斯分布数据生成方式

    2022-12-20 20:29:40
  • Oracle数据库逻辑备份的SH文件

    2010-07-27 13:26:00
  • python3模拟实现xshell远程执行linux命令的方法

    2022-05-26 11:45:14
  • 详解Python中heapq模块的用法

    2022-01-15 05:58:35
  • asp之家 网络编程 m.aspxhome.com