当前位置:网站首页>【笔记】2022.5.27 通过pycharm操作MySQL
【笔记】2022.5.27 通过pycharm操作MySQL
2022-06-30 03:27:00 【Sprite.Nym】
1. 通过pycharm插入数据
RIGHT Example:
# example01 - 连接MySQL数据库插入数据
# SQL Injection - SQL注射攻击
# 经验:一定不能够使用字符串拼接或者格式化等方式组装动态的SQL,否则就会直面SQL注射攻击。
import pymysql
from pymysql.cursors import Cursor
no = input('请输入部门编号:')
name = input('请输入部门名称:')
location = input('请输入部门地址:')
# 1. 创建连接
conn = pymysql.connect(host='localhost', port=3306,
user='guest', password='guest.618',
database='hrs', charset='utf8mb4')
try:
# 2. 获取游标对象
with conn.cursor() as cursor: # type: Cursor
# 3. 通过游标对象执行SQL语句
affected_rows = cursor.execute(
'select dno from tb_dept where dno = (%s)',
(no,)
)
if affected_rows == 0:
affected_rows = cursor.execute(
'insert into tb_dept (dno, dname, dloc) '
'values (%s, %s, %s)',
(no, name, location)
)
if affected_rows == 1:
print('添加部门成功!!!')
else:
print('部门已存在')
# 4. 手动提交(让之前的操作生效)
conn.commit()
except pymysql.MySQLError as err:
# 4. 手动回滚(撤销之前的操作)
conn.rollback()
print(err)
finally:
# 5. 关闭连接
conn.close()
常见错误:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sfyhL76v-1653644986252)(C:\Users\HP\AppData\Roaming\Typora\typora-user-images\image-20220527091525487.png)]](/img/6a/9e4f00a09348f9358b6a533bdeba7a.png)


2. 通过pycharm删除数据
RIGHT Example:
# example02 - 连接MySQL数据库删除数据
import pymysql
from pymysql.cursors import Cursor
no = input('请输入部门编号:')
# 1. 创建连接
conn = pymysql.connect(host='localhost', port=3306,
user='guest', password='guest.618',
database='hrs', charset='utf8mb4')
try:
# 2. 获取游标对象
with conn.cursor() as cursor: # type: Cursor
# 3. 通过游标对象执行SQL语句
affected_rows = cursor.execute(
'select dno from tb_dept where dno = (%s)',
(no,)
)
if affected_rows == 0:
print('早已删除')
else:
affected_rows = cursor.execute(
'delete from tb_dept where dno = (%s) ',
(no,)
)
if affected_rows == 1:
print('删除部门成功!!!')
# 4. 手动提交(让之前的操作生效)
conn.commit()
except pymysql.MySQLError as err:
# 4. 手动回滚(撤销之前的操作)
conn.rollback()
print(err)
finally:
# 5. 关闭连接
conn.close()
3. 通过pycharm更改数据
RIGHT Example:
# example03 - 连接MySQL数据库更改数据
import pymysql
from pymysql.cursors import Cursor
no = input('请输入部门编号:')
name = input('请输入部门名称:')
location = input('请输入部门地址')
# 1. 创建连接
conn = pymysql.connect(host='localhost', port=3306,
user='guest', password='guest.618',
database='hrs', charset='utf8mb4')
try:
# 2. 获取游标对象
with conn.cursor() as cursor: # type: Cursor
# 3. 通过游标对象执行SQL语句
affected_rows = cursor.execute(
'update tb_dept set dname = %s, dloc = %s where dno = %s',
(name, location, no)
)
if affected_rows == 1:
print('更新成功')
# 4. 手动提交(让之前的操作生效)
conn.commit()
except pymysql.MySQLError as err:
# 4. 手动回滚(撤销之前的操作)
conn.rollback()
print(err)
finally:
# 5. 关闭连接
conn.close()
4. 通过pycharm查数据
# example04 - 连接MySQL数据库查数据
import pymysql
import csv
from pymysql.cursors import Cursor, DictCursor
f = open('员工表.csv', 'w', encoding='utf-8', newline='')
writer = csv.writer(f)
writer.writerow(['员工编号', '员工姓名', '员工职位', '员工主管', '员工月薪', '员工奖金', '所属部门编号'])
# 1. 创建连接
conn = pymysql.connect(host='localhost', port=3306,
user='guest', password='guest.618',
database='hrs', charset='utf8mb4')
try:
# 2. 获取游标对象
with conn.cursor() as cursor: # type: Cursor
# 3. 通过游标对象执行SQL语句
affected_rows = cursor.execute(
'select eno, ename, job, mgr, sal, comm, dno from tb_emp'
)
# 4. 通过游标对象抓取数据
while data := cursor.fetchone():
writer.writerow(data)
except pymysql.MySQLError as err:
print(err)
finally:
# 5. 关闭连接
conn.close()
边栏推荐
- An article to get you started VIM
- Simple custom MVC
- Dripping backward (II)
- Use common fileUpload to upload files
- The broadcast module code runs normally in autojs4.1.1, but an error is reported in pro7.0 (not resolved)
- Some common functions and precautions
- Problem record: FEL_ lib. c:26:10: fatal error: libusb. h: There is no such file or directory
- Auto.js学习笔记16:按项目保存到手机上,不用每次都保存单个js文件,方便调试和打包
- 简单自定义MVC优化
- Mysql性能优化(5):主从同步原理与实现
猜你喜欢
![[QT] QMap使用详解](/img/ee/6e71a3dc5b90d2d1b7f7d3f6b56221.png)
[QT] QMap使用详解

Redis中的SDS理解

SDS understanding in redis

Redis中的Hash设计和节省内存数据结构设计

LitJson解析 生成json文件 读取json文件中的字典

Stc89c52/90c516rd/89c516rd ADC0832 ADC driver code

【笔记】2022.6.7 数据分析概论

What are outer chain and inner chain?
![[wechat applet] how did the conditional rendering list render work?](/img/db/4e79279272b75759cdc8d6f31950f1.png)
[wechat applet] how did the conditional rendering list render work?

X书6.89版本shield-unidbg调用方式
随机推荐
Redis中的SDS理解
Arrangement of language resources of upgraded version
Tidb 6.0: making Tso more efficient tidb Book rush
General paging (2)
Global and Chinese market of centrifugal pumps 2022-2028: Research Report on technology, participants, trends, market size and share
实用调试技巧
1152_ Makefile learning_ Pattern matching rules
图的邻接矩阵存储 C语言实现BFS
C#【高级篇】 C# 泛型(Generic)【需进一步补充:泛型接口、泛型事件的实例】
11: I came out at 11:04 after the interview. What I asked was really too
JS 字母和数字的相互转换
MySQL + JSON = King fried
If you can tell whether the external stock index futures trading platform I am trading is formal and safe?
51单片机的室内环境监测系统,MQ-2烟雾传感器和DHT11温湿度传感器,原理图,C编程和仿真
Principle of device driver
【笔记】AB测试和方差分析
链接乱码转义符
Local, locallow and roaming in the appdata folder
自定义MVC的使用
JS conversion of letters and numbers