時光很短暫

导航

 

表字段操作补充

# 1.添加表字段
alter table 表名 add 字段名 字段类型 约束条件;  # 默认尾部追加
alter table 表名 add 字段名 字段类型 约束条件 after 已经存在的字段名; 
alter table 表名 add 字段名 字段类型 约束条件 first;  # 了解 

# 2.修改字段
"""modify只能改字段数据类型完整约束,不能改字段名,但是change可以!"""
ALTER TABLE 表名 
                          MODIFY  字段名 数据类型 [完整性约束条件…];
ALTER TABLE 表名 
                          CHANGE 旧字段名 新字段名 数据类型 [完整性约束条件…];

# 3.删除字段
ALTER TABLE 表名 
                          DROP 字段名;

python操作MySQL(掌握)

1.下载模块
    pip3 install pymysql

"""
python默认下载模块的地址是国外的网站
速度有时候会非常的慢 如果想要提升速度我们可以切换下载源
"""1)阿里云 http://mirrors.aliyun.com/pypi/simple/2)豆瓣http://pypi.douban.com/simple/3)清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/4)中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/5)华中科技大学http://pypi.hustunique.com/
# 1.命令行切换源
    pip3 install pymysql -i 源地址
# 2.pycharm永久切换
    file
        setting
            interpreter
                双击
                    manage 仓库
   
2.基本使用
import pymysql


# 创建链接对象
conn = pymysql.connect(
    host='127.0.0.1',
    port=3306,
    user='root',
    password='1',
    database='db4_3',
    charset='utf8'
)
# 生成游标对象
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)  # 括号内不写参数是元祖组织不够明确
# 定义SQL语句
# sql = 'show tables'
sql = 'select * from teacher'
# 执行sql语句
affect_rows = cursor.execute(sql)
print(affect_rows)  # 执行SQL语句所影响的行数
# 获取结果
res = cursor.fetchall()
print(res)

SQL注入

利用特殊符号的组合绕过相应的机制

如何解决
    不要自己手动处理敏感数据
tom' -- fdsfdsfsdf

xxx' or 1=1 -- fffdf

sql = "select * from userinfo where name=%s and password=%s"
# 执行sql语句
cursor.execute(sql,(username,password))  # 交由execute自动拼接 自动筛选

其他操作

针对增删改查
    查重要程度很低 无序二次确认
    增改删重要程度很高 都需要二次确认
 conn = pymysql.connect(
    host='127.0.0.1',
    port=3306,
    user='root',
    password='1',
    database='db5',
    charset='utf8',
    autocommit=True  # 自动二次确认
)

 

posted on 2022-03-06 19:57  時光很短暫  阅读(29)  评论(0)    收藏  举报