当前位置:网站首页>【笔记】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()
常见错误:
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()
边栏推荐
- Hudi record
- Neo4j--- performance optimization
- From 2500 a month, no one wants to go to the programming road of the technical director of a large factory | my ten years
- Buffer pool of MySQL notes
- Chapter 2 control structure and function (programming problem)
- 链接乱码转义符
- Redis中的Hash设计和节省内存数据结构设计
- Tidb 6.0: making Tso more efficient tidb Book rush
- SDS understanding in redis
- Global and Chinese market for sensor screwdrivers 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
From 2500 a month, no one wants to go to the programming road of the technical director of a large factory | my ten years
第2章 控制结构和函数(编程题)
【笔记】AB测试和方差分析
Number of students from junior college to Senior College (III)
简单自定义mvc
LitJson解析 生成json文件 读取json文件中的字典
How to realize remote collaborative office, keep this strategy!
Simple custom MVC optimization
什么是外链和内链?
Linked list: insert a node in the head
随机推荐
Possible problems in MySQL cross database operation with database name
C#【高级篇】 C# 泛型(Generic)【需进一步补充:泛型接口、泛型事件的实例】
Auto. JS learning notes 15:ui interface basics of autojs Chapter 2
【常见问题】浏览器环境、node环境的模块化问题
1152_ Makefile learning_ Pattern matching rules
From 2500 a month, no one wants to go to the programming road of the technical director of a large factory | my ten years
1148_ Makefile learning_ Targets, variables, and wildcards in makefile
Redis在windows系统中使用
Neo4j--- performance optimization
51 single chip microcomputer indoor environment monitoring system, mq-2 smoke sensor and DHT11 temperature and humidity sensor, schematic diagram, C programming and simulation
WPF initialized event in The reason why binding is not triggered in CS
【作业】2022.5.28 将CSV写入数据库
专升本高数(四)
Practical debugging skills
On the role of database tables
The 5-year Android development interview took 20 days to join Alibaba
专升本语文资源整理
设备驱动程序的原理
Hisense A7 ink screen mobile phone cannot be started
C#【高级篇】 C# 接口(Interface)