zoukankan      html  css  js  c++  java
  • 5.python 操作 MySQL

    python 操作 MySQL

    1.基本语法

    # python操作MySQL 
    import pymysql  #需要先安装pymysql
    # 1.基本语法
    # (1)连接数据库,创建连接对象
    conn = pymysql.connect(host="127.0.0.1",user="root",password="123456",database="lianxi104",charset="utf8",port=3306)
    
    # (2) 创建游标对象(用来做增删改查 MySQL操作)
    # cursor = conn.cursor()
    
    # 设置查询返回的数据为字典,默认是元组
    cursor = conn.cursor(cursor = pymysql.cursors.DictCursor)
    
    # (3)执行sql语句
    sql = "select * from t1"
    
    res = cursor.execute(sql)
    print(res) # 返回的是查询的条数
    
    # (4) 获取数据
    """
    fetchone 获取一条数据
    """
    res = cursor.fetchone()
    print(res)    # 返回元组(2, '熊二')
    
    res = cursor.fetchone() # 类似迭代器
    print(res)   # 返回字典 {'id': 1, 'name': '熊大'}
    
    # (5)释放游标对象
    cursor.close()
    # (6)释放连接对象
    conn.close()
    
    

    2.创建删除表操作 与 事务处理

    # 1. 创建删除表相关操作
    import pymysql
    
    conn = pymysql.connect(host="127.0.0.1",user = "root",password = "123456",database = "lianxi104", charset = "utf8",port = 3306)
    cursor = conn.cursor(cursor = pymysql.cursors.DictCursor)
    
    # 创建表
    sql = """
    create table t2 (
    id int,
    name varchar(222)
    )
    """
    cursor.execute(sql) 
    
    # 查看表结构
    sql = "desc t2"
    res = cursor.execute(sql)
    print(res)  # 返回的是字段个数
    
    res = cursor.fetchone()
    print(res) 
    
    # 删除表
    
    sql = "drop table  t2"
    res = cursor.execute(sql)
    print(res) # 创建删除表的返回值没有意义
    
    # 关闭游标连接对象
    cursor.close()
    conn.close()
    
    # 2.事务处理 (提交,回滚)
    conn = pymysql.connect(host="127.0.0.1",user="root",password="123456",database="lianxi104")
    cursor = conn.cursor()
    
    sql1 = "begin"
    sql2 = "insert into t1 values(null,'好人')"
    sql3 = "commit"
    
    cursor.execute(sql1)
    cursor.execute(sql2)
    cursor.execute(sql3)
    
    cursor.close()
    conn.close()
    

    3.python 操作 MySQL 增删改查

    # python 操作 MySQL 增删改查
    import pymysql
    
    # 连接数据库
    conn = pymysql.connect(host="127.0.0.1",user="root",password="123456",database="lianxi104")
    cursor = conn.cursor(cursor = pymysql.cursors.DictCursor)
    """
    pymysql 操作数据库时(增删改),默认开启事务处理(增删改)
    必须调用commit提交数据,才会真正的去操作数据库
    语法: conn.commit()
    """
    # 一: 增
    sql = "insert into t1(name) values (%s)"
    # 1.插入一条数据
    
    res = cursor.execute(sql,("熊大"))
    print(res)  # 返回的是插入几条数据
    print(cursor.lastrowid)  # 最后一行数据的索引
    
    # 2.一次插入多条数据 :executemany (列表里放元组)
    res = cursor.executemany(sql,[("熊二"),("光头强"),("吉吉国王")])
    print(res)
    print(cursor.lastrowid)
    
    # 二 : 删  
    """删除一条或多条(与插入多条数据一样)"""
    sql = "delete from t1 where name = %s"
    res = cursor.execute(sql,("熊二"))
    print(res) # 返回删除几条数据
    
    # 三 : 改 
    """更改一条或多条(与插入多条数据一样)"""
    sql = "update t1 set name = %s where id = %s"
    res = cursor.execute(sql,("大林子",1))
    print(res) # 返回更新了几条数据
    
    # 四 : 查  
    # 1. 整除查询
    """
    fetchone()          查询一条数据
    fetchmany(n)     查询n条数据    
    fetchall()             查询所有数据    
    """
    sql = "select * from t1"
    res  = cursor.execute(sql)
    print(res) # 返回查询了多少条数据
    
    # (1).fetchone  查询一条数据
    res = cursor.fetchone() #相当迭代器一样查看数据
    print(res) 
    res = cursor.fetchone()
    print(res) 
    
    # (2).fetchmany 查询多条数据
    res = cursor.fetchmany(4)
    print(res)
    
    # (3).fetchall 查看所有数据
    """
    res = cursor.fetchall()
    print(res)
    """
    # 2. scroll : 滚动查询,调整查询位置
    # (1) 相对滚动  relative  
    """相对于上一条数据向下滚动n个位置 , 整数"""
    cursor.scroll(4,mode="relative")
    res = cursor.fetchone() # 查询下一条数据
    print(res)
    """相对于上一条数据向上滚动n个位置 , 负数"""
    cursor.scroll(-5,mode="relative")
    res = cursor.fetchone() # 查询下一条数据
    print(res)
    
    # (2) 绝对滚动 absolute 
    """永远从头开始, 0代表第一条数据, 不能为负数"""
    cursor.scroll(0,mode="absolute")
    res = cursor.fetchone()
    print(res)
    
    conn.commit()
    cursor.close()
    conn.close()
    

    4.mysql 注入攻击

    # sql 注入攻击
    import pymysql
    
    user = input("请输入您的账号:")
    pwd = input("请输入您的密码:")
    #连接数据库
    conn = pymysql.connect(host="127.0.0.1", user ="root" , password="123456",database="lianxi104")
    cursor = conn.cursor(cursor = pymysql.cursors.DictCursor)
    
    # 1.注入攻击
    """
    实现: 不知道账号密码也可以登陆
        账号: sdfsdfsdf' or 5=5 -- adfa  密码: 随便写
        -- 在sql中代表的注释的含义,可以把密码注释掉
    """
    sql = "select * from zrgj where name = '%s' and pwd = '%s'"%(user,pwd)
    print(sql)
    
    res = cursor.execute(sql)
    print("登陆成功~" if res else "登陆失败~") 
    
    # 2.解决方案: 预处理机机制
    """
    在执行sql语句之前,提前对sql中的符号进行过滤优化,避免sql注入攻击
    execute(sql,(参数1,参数2,参数3))
    """
    sql = "select * from zrgj where name = %s and pwd = %s"
    print(sql)
    res = cursor.execute(sql,(user,pwd))
    print("登陆成功~" if res else "登陆失败~") 
    
    cursor.close()
    conn.close()
    
  • 相关阅读:
    为ccflow增加禁用用户立刻生效功能
    关于工作流引擎授权问题的需求变更
    sql server 2005 安装过程中出现错误Insatalling performance countter: Cannot create a file when that file already exists.
    web.config中错误
    三个SQL视图查出所有SQL Server数据库字典
    恢复备份的数据库
    sql语句读取excel数据
    It is an error to use a section registered allowDefinition='MachineToApplication' beyond application level. 错误
    DOS命令实现创建文件夹
    如何查看sql server 的版本(网摘)
  • 原文地址:https://www.cnblogs.com/jia-shu/p/14275382.html
Copyright © 2011-2022 走看看