zoukankan      html  css  js  c++  java
  • 使用python 操作mysql 数据库

    首先导入第三方模块 pymysql

    import pymysql
    #连接mysql服务器
    conn = pymysql.connect(host= 'localhost',user='root',password='123',database='db2',charset='utf8')
    # 通过pymysql连接MySQL数据库,指定需要操作的数据库
    # 设置光标,并指定取出数据的格式为字典形式
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    # 拼接sql语句
    sql = 'delect from student_grade where id = 10'
    # sql = 'select * from syudent0 '
    
    # 执行sql语句
    cursor.execute(sql)
    #删除更新时需要进行一个事物的提交
    conn.commit()
    # 取出一条数据
    # res = cursor.fetchall()
    # 取出多条
    # res = cursor.fetchmany()
    # 全部取出
    res = cursor.fetchall()
    # res = cursor.fetchmany(5)
    print(res)
    
    # 操作完成后关闭光标,关闭连接
    cursor.close()
    conn.close()

    连接mysql 实现登录验证

    写sql 语句时候,%传值时需要加上引号:sql = "select * from 表名 where name = ‘%s’ and pwd='%s'"%(name,pwd);

    这样写的风险:例一:

    username = zekai' #

    select * from 表名 where name = 'zekai' #' and pwd = ''

    例二:

    username = dbsahvbdsha' or 1=1 #

    select * from 表名 where name = 'dbsahvbdsha' or 1=1

    这样的问题称为sql注入

    出现这样的问题的根源:因为太过于相信用户的输入, 导致我们在接受用户输入的参数的时候, 并没有对他进行转义。

    解决SQL注入:

    1. 自己手工对用户输入的值进行转义

    2. 使用execute()自动进行过滤

    sql = "select * from t4 where name = %s and pwd = %s"

    cursor.execute(sql,(username, pwd))

    补充插入数据; cursor.execute(sql,('sds','123'))

    插入多条:

        data = [
                    ('aaaaa', 'aaa'),
                    ('bbbb', 'bbb'),
                    ('ffff', '666'),
                    ('rrrr', '888'),
                ]
                cursor.executemany(sql, data)
        
        
                try:
                    cursor.execute(sql, ('lxxx', '1234'))
                    
                    ###  删除和更新的时候, 需要事物提交
                    conn.commit()
                except Exception as e:
                    conn.rollback()
        
            
                cursor.lastrowid : 最后一行的行数
            

    事务

    一组操作要么都成功,要么一起失败。

    特性:原子性 一组操作要么都成功,要么都失败。

       一致性(Consisiency): 只事务发生前和发生后数据的总额不变
       隔离性(Isolation) : 一个事务的操作和变化不会影响到其他事务

              持久性(Durability):当事务完成后,影响就会被保存下来,不能撤销,只能再重新开启一个事务来进修修改

     

    场景:
    思考:
    我去银行给朋友汇款,
    我卡上有1000元,
    朋友卡上500元,
    我给朋友转账100元(无手续费),
    如果,网线断了, 我的钱刚扣,而朋友的钱又没加时, 怎么办?

    create table t11 (
    id int auto_increment primary key,
    name varchar(32) not null default '',
    money int not null default 0
    )engine=Innodb charset=utf8;
    
    insert into t11 (name,money) values ('zekai', 1000), ('eagon', 500);
    
    
    解决方法:
    
    开启事务 (start transaction)
    
    (执行sql操作)
    
    commit : 提交上面的SQL, 让其生效
    
    rollback: 回滚
    
    show full tables; 显示全部类型
    
            
  • 相关阅读:
    “让开发者爱上安全测试”系列之“源码安全测试”——开发者之伤
    敏捷开发中高质量 Java 代码开发实践
    软件测试转型之路--革新全程软件测试
    衡量DevOps成功的15个标准
    C# winFrom窗体设计问题-部分文件打不开窗体设计器 变成类.cs
    Asp.net IIS Express 无法启动 解决办法
    C# winForm资源文件实现多语言切换
    C#用DataTable实现Group by数据统计
    C# 控件拖动
    Android studio Error:org.gradle.api.internal.tasks.DefaultTaskInputs$TaskInputUnionFileCollection cannot be cast to
  • 原文地址:https://www.cnblogs.com/1624413646hxy/p/11040924.html
Copyright © 2011-2022 走看看