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

最低0.47元/天 解锁文章
2116

被折叠的 条评论
为什么被折叠?



