zoukankan      html  css  js  c++  java
  • pymysql增删改查

    #!/usr/bin/env python
    # encoding: utf-8  
    # Date: 2018/6/24

    # 1、增删改
    import pymysql

    conn = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='123',
        db='stu_system',
        charset='utf8'
    )
    # 拿到游标
    cursor = conn.cursor()
    sql = 'insert into stu_teacher(tname) values (%s)'

    # 插入一条
    # rows = cursor.execute(sql, ('wxx'))
    # print(rows)

    # 插入多条
    rows = cursor.executemany(sql, [('wxx',), ('egonxx',)])
    print(rows)

    # 查看当前自增id数值
    print(cursor.lastrowid)

    conn.commit()
    cursor.close()
    conn.close()


    # # 2、查询
    # import pymysql
    #
    # conn = pymysql.connect(
    #     host='127.0.0.1',
    #     port=3306,
    #     user='root',
    #     password='123',
    #     db='stu_system',
    #     charset='utf8'
    # )
    # # 拿到游标
    # # cursor = conn.cursor()  # 元组形式的游标
    # cursor = conn.cursor(pymysql.cursors.DictCursor)  # 字典形式的游标
    # # 查询
    # rows = cursor.execute('select * from stu_teacher;')
    #
    # # 取一行
    # r1 = cursor.fetchone()  # 元组(1, '李必胜')
    # print(r1)
    #
    # # 取多行
    # r2 = cursor.fetchmany(2)  # 2行
    # print(r2)
    #
    # # 取所有
    # rall = cursor.fetchall()
    # print(rall)
    #
    # # cursor 设置光标位置
    # # cursor.scroll(3, mode='relative')  # 相对位置移动
    # # cursor.scroll(3, mode='absolute')  # 绝对位置移动
    #
    # cursor.close()
    # conn.close()


  • 相关阅读:
    Linux的各个文件夹名称解释(FHS)
    ThinkPHP3.1URL分发BUG修正
    HTTP响应头缓存控制
    Web性能测试工具:http_load安装&使用简介
    无法登陆github官网的解决办法
    次梯度(Subgradient)
    科普帖:深度学习中GPU和显存分析
    关于图像分类的问题
    深度学习中的多尺度模型设计
    Pytorch模型定义的三要
  • 原文地址:https://www.cnblogs.com/fmgao-technology/p/9220083.html
Copyright © 2011-2022 走看看