zoukankan      html  css  js  c++  java
  • mysql pymysql ,分页

    pymysql 

    # 1.先安装pymysql 模块 pip3 install pymysql
    import pymysql
    # 相当于mysql的客户端程序
    # 前端中获取的用户名和密码
    username = input('请输入用户名:')
    pwd = input('请输入密码:')
    
    
    
    # 建立连接
    conn = pymysql.connect(
        host = '127.0.0.1',
        port = 3306,
        db = 'db20',
        user = 'root',
        password='',
        charset ='utf8'
    )
    
    # 创建游标
    cur = conn.cursor()
    
    # select * from userinfo where name = 'alex' and password = '123';
    sql  = "select * from userinfo where name = '%s' and password = '%s'" %(username,pwd)
    print(sql)
    
    # 执行sql  返回是查询的成功的记录
    result = cur.execute(sql) 使用这个可以防止sql注入
    
    print(result)
    
    # 游标关闭  连接关闭
    cur.close()
    conn.close()
    
    if result:
    
        # 响应 数据 到前端
        print('登录成功')
    
    else:
        print('登录失败')
    

      

    sql  = "select * from userinfo where name = %(name)s and password = %(password)s"
    print(sql)
    
    # sql  --
    
    # 执行sql  返回是查询的成功的记录
    result = cur.execute(sql,{"name":username,"password":pwd})
    
    print(result)# 字典
    
    # 1.先安装pymysql 模块 pip3 install pymysql
    import pymysql
    # 相当于mysql的客户端程序
    
    
    # 建立连接
    conn = pymysql.connect(
        host = '127.0.0.1',
        port = 3306,
        db = 'db20',
        user = 'root',
        password='',
        charset ='utf8'
    )
    
    # 创建游标
    cur = conn.cursor()
    
    # 插入
    # select * from userinfo where name = 'alex' and password = '123';
    sql = "insert into userinfo(name,password) values (%s,%s)"
    
    
    # 更改
    # sql = "update userinfo set name = %s where id = 3"
    # 更改
    # sql = "delete from userinfo  where id = 3"
    
    # print(sql)
    
    # sql  --
    
    # 执行sql 插入数据 删除数据 更改数据 一定记得commit()
    # 插入一条数据
    # result = cur.execute(sql)
    
    # 插入多条数据
    result = cur.executemany(sql,[('alex2','321'),('alex3','678')])
    
    
    
    print(result)
    
    # 一定要提交
    conn.commit()
    # 游标关闭  连接关闭
    cur.close()
    conn.close()
    

      查询

    :

     创建游标 查询出来的记录 是字典的形式
    cur = conn.cursor(cursor=pymysql.cursors.DictCursor)
    
    # select * from userinfo where name = 'alex' and password = '123';
    sql  = "select * from userinfo"
    print(sql)
    
    # sql  --
    
    # 执行sql  返回是查询的成功的记录
    result = cur.execute(sql)
    print(result)
    
    
    
    # rows = cur.fetchone()
    # print(rows)
    # rows = cur.fetchone()
    # print(rows)
    # rows = cur.fetchone()
    # print(rows)
    # rows = cur.fetchone()
    # print(rows)
    
    # 查询多条数据
    # rows = cur.fetchmany(2)
    
    # 查询所有的数据
    rows = cur.fetchall()
    print(rows)
    
    elect * from userinfo where name = 'alex' and password = '123';
    sql  = "select * from userinfo"
    print(sql)
    
    # sql  --
    
    # 执行sql  返回是查询的成功的记录
    result = cur.execute(sql)
    print(result)
    
    rows = cur.fetchone()
    print(rows)
    rows = cur.fetchone()
    print(rows)
    
    cur.scroll(1,mode='absolute')
    rows = cur.fetchone()
    print(rows)
    
    # rows = cur.fetchone()
    # print(rows)
    # rows = cur.fetchone()
    # print(rows)
    # rows = cur.fetchone()
    # print(rows)
    

    只有一页 和下一页

    1,记录当前页的最大id或者最小的id

    下一页:

    select * from user where id > max_id limit 10,

    select 8 from user where id< min order by id desc limit 10;

    2,页面有页码的情况

    select * from user where id in (

      select id from (select *from user where id > pre_max_id limit (cu)max_id-pre_max_id)*10 ) as A order by A.id desc limit 10);

      select * from (select * from user where id > 1500011 limit 30) as  A order by id order by desc;

      

      

      

  • 相关阅读:
    剑指offer23-二叉搜索树的后序遍历序列
    剑指offer24-二叉树中和为某一值的路径
    剑指offer-复杂链表的复制
    剑指offer-二叉搜索树与双向链表
    剑指offer-字符串的排序
    剑指offer-数组中出现次数超过一半的数字
    剑指offer-最小的k个数
    c++中参数加&与不加的区别
    第九届蓝桥杯(A组填空题)
    基于优先级的多道程序系统作业调度——基于优先级
  • 原文地址:https://www.cnblogs.com/lnrick/p/9581063.html
Copyright © 2011-2022 走看看