zoukankan      html  css  js  c++  java
  • Mysql学习---Python操作Mysql 1231

    安装PyMysql

    安装PyMysql:Py3默认自带pip3安装,Py2默认无pip命令

    cmd进入PyCharm的安装目录完成安装 pip3 install pymysql

    image

    安装完成的位置:E:PyCharm 2017.2.4Python3.2.5Libsite-packages

    image

    故障处理:更新一下默认的Python安装

    image

    Py下操作Mysql

    PyMySQL   - 专门用于操作MySQLpython模块, Py2和Py3同时兼容

                       - MySQLdb(py3暂时不支持MySQLdb)

    基本操作:添加信息——Insert

    # -*- coding:utf-8 -*-
    import pymysql
    
    # 创建连接
    conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='test_python', charset='utf8')
    # 创建游标
    cursor = conn.cursor()  # 默认是元组,可以更改为字典类型
    # 第一种:直接插入
    # 执行SQL,并返回受影响行数
    insert_effect_row = cursor.execute("insert into course(cname, teacher_id) VALUES ('hhhh43hhkkhh', '2')") 
    inp = input('请输入姓名:')
    inp2 = input('请输入教师ID:')
    # 第二种:字符串拼接
    # sql = 'insert into course(cname) VALUES ("%s")' %inp  
    # cursor.execute(sql)                 # 字符串拼接可以用,但是容易造成sql注入不推荐使用
    # 第三种:参数传递,利用%s做占位符号,传入参数进去,PyMysql内部帮我们转换
    insert_effect_row_sec = cursor.execute("insert into course(cname, teacher_id) VALUES (%s, %s)", (inp, inp2))  # 参数传递
    # 第四种: 多条信息的插入
    li = [
        ('哇哈哈1', 1),
        ('哇哈哈2', 2),
        ('哇哈哈3', 3),
    ]
    executmany = cursor.executemany("insert into course(cname, teacher_id) VALUES (%s, %s)", li) # 传入可迭代的类型
    print('executmany:', executmany)  # executmany: 3 ,修改成功3条
    # 提交,不然无法保存新建或者修改的数据
    conn.commit()
    # 关闭游标
    cursor.close()
    # 关闭连接
    conn.close()

    image

    基本操作:查找信息——Select

    # -*- coding:utf-8 -*-
    import pymysql
    # 创建连接
    conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='test_python', charset='utf8')
    # 创建游标
    cursor = conn.cursor()
    ret = cursor.execute('select * from student')  # 仅仅数据加载到内存,需要fetch取值
    print(ret)  # 查找到结果的数量
    # 第一种: 直接打印,数据量大的时候容易导致内存不够用(内部有一个指针索引)
    # r = cursor.fetchall()
    # print('取出所有值
    ', r)  # 打印结果,结果是一个元组
    # 第二种: 从内存中取出来1条数据,此时数据已经加载到内存
    r1 = cursor.fetchone()
    print('拿出一个
    :', r1)
    # 第三种: 从内存中取出来3条数据,此时数据已经加载到内存
    r3 = cursor.fetchmany(3)
    print('拿出三个
    :', r3)
    # 第四种:操作指针取出数据
    # cursor.scroll(0, mode='relative')  # 相对位置,指针索引回归0, +1/-1 分别表示向上/向下
    # r4 = cursor.fetchmany(3)
    # print('相对索引拿出三个
    :', r4)   # 从第5个开始取值 : ((5, '女', 1, '张二'), (6, '男', 1, '张四'), (7, '女', 2, '铁锤'))
    cursor.scroll(0, mode='absolute')    # 绝对位置,指针索引回归0
    r5 = cursor.fetchmany(3)
    print('绝对索引拿出三个
    :', r5)     # 从第0个位置开始取值: ((1, '男', 1, '理解'), (2, '女', 1, '钢蛋'), (3, '男', 1, '张三'))
    # 关闭游标
    cursor.close()
    # 关闭连接
    conn.close()

    image

    基本操作:更改信息——Update

    # -*- coding:utf-8 -*-
    import pymysql
    
    # 创建连接
    conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='test_python', charset='utf8')
    # 创建游标
    cursor = conn.cursor()
    inp  = input('请输入更新后的信息:')
    ret  = cursor.execute("update course set cname = %s where cname = '哇哈哈4'", inp)
    ret2 = cursor.execute("update course set cname = %s where cname = '哇哈哈1'", inp)
    # 提交,不然无法保存新建或者修改的数据
    conn.commit()
    print('不存在且更新结果:', ret, '
    存在且更新结果:', ret2)
    # 关闭游标
    cursor.close()
    # 关闭连接
    conn.close()

    image

    基本操作:删除信息——Delete

    # -*- coding:utf-8 -*-
    import pymysql
    
    # 创建连接
    conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='test_python', charset='utf8')
    # 创建游标
    cursor = conn.cursor()
    inp  = input('请输入更新后的信息:')
    ret  = cursor.execute("update course set cname = %s where cname = '哇哈哈4'", inp)
    ret2 = cursor.execute("update course set cname = %s where cname = '哇哈哈1'", inp)
    # 提交,不然无法保存新建或者修改的数据
    conn.commit()
    print('不存在且更新结果:', ret, '
    存在且更新结果:', ret2)
    # 关闭游标
    cursor.close()
    # 关闭连接
    conn.close()

    image

    其他操作: 更改游标的返回值为字典

    # -*- coding:utf-8 -*-
    import pymysql
    
    # 创建连接
    conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='test_python', charset='utf8')
    # 创建游标
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    cursor.execute('select cid as id , cname as name from course')  # 可以更改原理字典的key[cname]为name
    print(cursor.fetchall())  # 可以根据字典取值
    # 关闭游标
    cursor.close()
    # 关闭连接
    conn.close()

    image

    其他操作:获取自增ID

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    import pymysql
    
    conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='test_python', charset='utf8')
    cursor = conn.cursor()
    cursor.executemany("insert into course(cname, teacher_id)values(%s,%s)", [("百事可乐", 1), ("可口可乐", 2)])
    conn.commit()
    # 获取最新自增ID
    new_id = cursor.lastrowid
    print(new_id)
    cursor.close()
    conn.close()

    防SQL注入的方法

    防SQL注入的方法:

    1. 存储过程  

    2. 占位符拼接  切记用字符串拼接

    SQL注入:更改了原来的sql语句,不推荐拼接,推荐参数传递

    # -*- coding:utf-8 -*-
    import pymysql
    
    # 创建连接
    conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='test_python', charset='utf8')
    # 创建游标
    cursor = conn.cursor()
    sql = 'select * from course where cid = "%s" and cname = "%s"'
    # sql = sql % ('24', '哇哈哈3')        # 正常
    sql = sql % ('24"-- ', '哇哈哈3')      # SQL注入取值,注释掉了后面的内容
    # sql = sql % ('24" or 1=1 -- ', '哇哈哈3')   # SQL注入取值,后面的条件恒成立,可查询所有结果
    print(sql)
    ret = cursor.execute(sql);
    r = cursor.fetchall()
    print('执行结果:', r)
    # 关闭游标
    cursor.close()
    # 关闭连接
    conn.close()

    image

  • 相关阅读:
    基础数据结构总结
    图论总结
    【bzoj1614】[Usaco2007 Jan]Telephone Lines架设电话线
    【bzoj1015】星球大战starwar
    NOIP2012摆花
    最勇敢的机器人
    【bzoj1056】排名系统
    图的第k短路
    【bzoj1455】罗马游戏
    ti
  • 原文地址:https://www.cnblogs.com/ftl1012/p/9385377.html
Copyright © 2011-2022 走看看