zoukankan      html  css  js  c++  java
  • MYSQL——pymysql模块

    1、基本规则及其input命令

    import pymysql
    
    conn=pymysql.connect(host='127.0.0.1',port=3306,user='root',password='123',database='db4',charset='utf8mb4')
    #游标
    cursor=conn.cursor() #执行完毕返回的结果集默认以元组显示
    
    # cursor.execute("insert into user(name,pwd) values('egon','123'),('tom','456'),('jack','111');")
    
    # sql="insert into user(name,pwd) values('%s','%s');" %('lili','123')
    # cursor.execute(sql)
    
    # %s不要加引号
    # cursor.execute("insert into user(name,pwd) values(%s,%s);",('kkk','123'))
    
    
    username = input("username>>>: ").strip()
    password = input("password>>>: ").strip()
    # sql = "select * from user where name='%s' and pwd='%s'" %(username,password)
    # select * from user where name='egon' -- hello' and pwd='%s'
    # select * from user where name='xxx' or 1=1 -- hello' and pwd='%s';
    # rows=cursor.execute(sql)
    
    rows=cursor.execute("select * from user where name=%s and pwd=%s",(username,password))
    
    if rows:
        print('ok')
    else:
        print('no')
    
    conn.commit()
    cursor.close()
    conn.close()

    2、用法

    import pymysql  # pip3 install pymysql
    
    conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", password="123", db="db13", charset="utf8mb4")
    #游标 cursor
    = conn.cursor(cursor=pymysql.cursors.DictCursor)#返回字典 # 记录=》增删改 # cursor.execute("insert into student values(%s,%s)",[5,'kkk']) # cursor.executemany("insert into student values(%s,%s)",[(6,'xxx'),(7,'yyy'),(8,'zzzz')]) cursor.execute("insert into student(name) values(%s)",['zzz']) print(cursor.lastrowid) # cursor.execute("delete from student where id > 6;") # 记录=》查 # rows = cursor.execute('select * from student;') # print(rows) # line = cursor.fetchone() # print(line) # line = cursor.fetchone() # print(line) # # lines = cursor.fetchmany(2) # print(lines) # lines = cursor.fetchall() # print(lines) # for line in cursor: # print(line) # cursor.scroll(3,mode='absolute') # 相对绝对位置移动 # print(cursor.fetchone()) # print(cursor.fetchall()) # cursor.scroll(0,mode='absolute') # 相对绝对位置移动 # print(cursor.fetchall()) # cursor.fetchone() # cursor.scroll(1,mode='relative') # 相对当前位置移动 # cursor.scroll(0,mode='absolute') # 相对当前位置移动 # print(cursor.fetchone()) conn.commit() cursor.close() conn.close()

    3、在pymysql里调用存储过程

    import pymysql  # pip3 install pymysql
    
    conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", password="123", db="db4", charset="utf8mb4")
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    
    
    cursor.callproc('p2',(3,0))  # @_p2_0=3,@_p2_1=0
    '''
    set @_p2_0=3
    set @_p2_1=0
    
    call p2(@_p2_0,@_p2_1);
    
    '''
    
    print(cursor.fetchall())
    
    cursor.execute("select @_p2_1;")
    print(cursor.fetchall())
    
    cursor.execute("select @_p2_0;")
    print(cursor.fetchall())
    
    cursor.close()
    conn.close()

    ----46---

  • 相关阅读:
    python实现单线程多任务非阻塞TCP服务端
    sudo命令
    ajax 未加载出数据时,显示loding,数据显示后,隐藏loading
    ie7 解决圆角,阴影问题
    学习JavaScript你必须掌握的8大知识点!
    es6 个人笔记
    原生js,实现单选框
    原生js开发简单复选框,点击变颜色,再点击取消颜色,三种解决方法
    es6对象转数组
    JS正则截取两个字符串之间及字符串前后内容的方法
  • 原文地址:https://www.cnblogs.com/guojieying/p/13632732.html
Copyright © 2011-2022 走看看