• mysql python pymysql模块 增删改查 查询 fetchmany fetchall函数


      查询的fetchmany fetchall函数

    import pymysql
    
    
    
    mysql_host = '192.168.0.106'
    port = 3306
    mysql_user = 'root'
    mysql_pwd = '123'
    encoding = 'utf8'
    
    # 建立 连接mysql服务端
    
    conn = pymysql.connect(
        host=mysql_host,  # mysql服务端ip
        port=port,  # mysql端口
        user=mysql_user,  # mysql 账号
        password=mysql_pwd,  # mysql服务端密码
        db='db10',  # 操作的库
        charset=encoding  # 读取字符串编码
    
    )
    
    # 拿到游标对象
    cur = conn.cursor(pymysql.cursors.DictCursor)
    
    '''
    游标是给mysql提交命令的接口
    mysql> 
    把sql语句传递到这里
    '''
    
    
    # 执行sql语句
    # 增、删、改
    sql= 'select * from userinfo; '
    
    
    # 把sql语句传给游标执行
    # 让游标execute去帮我拼接字符串
    
    rows = cur.execute(sql)
    
    # 想看查询的内容 调游标对象
    # fetchmany 一次指定取多个
    
    # 一次取4条记录
    print(cur.fetchmany(4))
    
    
    # 执行完sql语句要关闭游标和mysql连接
    cur.close()
    conn.close()
    
    '''
    [{'id': 1, 'name': 'mike', 'pwd': '123'}, 
    {'id': 2, 'name': 'jack', 'pwd': '456'}, 
    {'id': 3, 'name': 'alex', 'pwd': '555'}, 
    {'id': 4, 'name': 'peter', 'pwd': '989'}]
    
    '''

     一次取4条记录

    fetchmany 一次指定取多少条记录
    fetchmany()



    import pymysql
    
    
    
    mysql_host = '192.168.0.106'
    port = 3306
    mysql_user = 'root'
    mysql_pwd = '123'
    encoding = 'utf8'
    
    # 建立 连接mysql服务端
    
    conn = pymysql.connect(
        host=mysql_host,  # mysql服务端ip
        port=port,  # mysql端口
        user=mysql_user,  # mysql 账号
        password=mysql_pwd,  # mysql服务端密码
        db='db10',  # 操作的库
        charset=encoding  # 读取字符串编码
    
    )
    
    # 拿到游标对象
    cur = conn.cursor(pymysql.cursors.DictCursor)
    
    '''
    游标是给mysql提交命令的接口
    mysql> 
    把sql语句传递到这里
    '''
    
    
    # 执行sql语句
    # 增、删、改
    sql= 'select * from userinfo; '
    
    
    # 把sql语句传给游标执行
    # 让游标execute去帮我拼接字符串
    
    rows = cur.execute(sql)
    
    # 想看查询的内容 调游标对象
    # fetchall 取所有
    print(cur.fetchall())
    
    
    # 执行完sql语句要关闭游标和mysql连接
    cur.close()
    conn.close()
    
    '''
    [{'id': 1, 'name': 'mike', 'pwd': '123'}, 
    {'id': 2, 'name': 'jack', 'pwd': '456'}, 
    {'id': 3, 'name': 'alex', 'pwd': '555'}, 
    {'id': 4, 'name': 'peter', 'pwd': '989'}, 
    {'id': 5, 'name': 'app', 'pwd': '123'}, 
    {'id': 6, 'name': 'tom', 'pwd': '556'}]
    '''

    取所有
    fetchall()
  • 相关阅读:
    第一周作业
    第一周作业
    C语言I博客作业07
    C语言I博客作业06
    C语言I博客作业03
    C语言I博客作业04
    c语言|博客作业02
    字段的约束验证
    [转]AS IS ? ??运算符
    BindingManagerBase 跟踪不一致
  • 原文地址:https://www.cnblogs.com/mingerlcm/p/9932570.html
Copyright © 2020-2023  润新知