zoukankan      html  css  js  c++  java
  • pyMysql模块

    # pip3 install pymysql
    import pymysql
    
    client=pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='',
        database='db5',
        charset='utf8'
    )
    
    cursor=client.cursor()
    
    sql='insert into t1 values(1,"egon");  '
    try:
        res=cursor.execute(sql)
        # res=cursor.execute(sql)
        # res=cursor.execute(sql)
        # res=cursor.execute(sql)
        print(res)
    
        client.commit()
    except Exception:
        client.rollback()
    
    cursor.close()
    client.close()
    
    
    
    # 基于pymysql模块的增删改
    
    # pip3 install pymysql
    import pymysql
    
    client=pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='',
        database='db5',
        charset='utf8'
    )
    
    cursor=client.cursor()
    
    #
    # userinfo=[
    #     (3,"alex"),
    #     (4,"lxx"),
    #     (5,"xxx")
    # ]
    #
    # for user in userinfo:
    #     sql='insert into t1 values(%s,"%s");  '%(user[0],user[1])
    #     print(sql)
    #     cursor.execute(sql)
    
    
    # 增简便方法
    # userinfo=[
    #     (3,"alex"),
    #     (4,"lxx"),
    #     (5,"xxx")
    # ]
    # sql='insert into t1 values(%s,%s);'
    # cursor.executemany(sql,userinfo)
    
    
    #
    # cursor.execute('delete from t1 where id =3;')
    
    
    #
    # sql='alter table t1 add age int; '
    sql='alter table t1 modify name varchar(20); '
    
    cursor.execute(sql)
    
    client.commit()
    
    cursor.close()
    client.close()
    
    
    
    
    
    
    # 基于mysql模块的查询
    
    import pymysql
    
    client=pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='',
        database='db5',
        charset='utf8'
    )
    
    cursor=client.cursor()
    
    
    inp_user=input('输入账号:').strip()
    inp_pwd=input('输入密码:').strip()
    
    # sql='select id from user where name="%s" and pwd=password("%s");'%(inp_user,inp_pwd)
    sql='select id from user where name="%s" and pwd="%s";'%(inp_user,inp_pwd)
    print(sql)
    rows=cursor.execute(sql)        #rows行数 如果有证明存在
    if rows:
        print('33[45m登录成功33[0m')
    else:
        print('33[46m用户或密码错误33[0m')
    
    
    cursor.execute(sql)
    
    client.commit()
    
    cursor.close()
    client.close()
    
    
    # 输入:egon" -- xxxxx
    # xxx" or 1=1 -- hellosb  可以登录
    
    
    
    
    # 解决sql 注入问题
    
    import pymysql
    
    client=pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='',
        database='db5',
        charset='utf8'
    )
    
    cursor=client.cursor()
    #查询
    inp_user=input('输入账号名: ').strip()
    inp_pwd=input('输入密码: ').strip()
    
    sql='select id from user where name = %s and pwd = %s;'
    rows=cursor.execute(sql,(inp_user,inp_pwd))              #不进行字符串拼接,放在execute执行
    if rows:
        print('33[45m登陆成功33[0m')
    else:
        print('33[46m用户名或密码错误33[0m')
    
    cursor.close()
    client.close()
    
    
    
    
    # 提交查询语句并且拿到查询结果
    import pymysql
    
    client=pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='egon123',
        database='db6',
        charset='utf8'
    )
    
    cursor=client.cursor(pymysql.cursors.DictCursor)
    #查询
    
    sql='select * from user where id > 3'
    rows=cursor.execute(sql)
    # print(rows)
    # print(cursor.fetchall())
    # print(cursor.fetchall())
    
    # print(cursor.fetchone())
    # print(cursor.fetchone())
    # print(cursor.fetchone())
    
    # print(cursor.fetchmany(2))
    # print(cursor.fetchone())
    
    
    
    # print(cursor.fetchall())
    # # cursor.scroll(0,mode='absolute') # 绝对位置移动
    # # cursor.scroll(1,mode='absolute') # 绝对位置移动
    # print(cursor.fetchall())
    
    
    # print(cursor.fetchone())
    # cursor.scroll(2,mode='relative') # 相对当前位置移动
    # print(cursor.fetchone())
    
    cursor.close()
    client.close()
    
    
    
    
    
    # 在Python3中调用存储过程:
    import pymysql
    
    client=pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='',
        database='db5',
        charset='utf8'
    )
    
    cursor=client.cursor(pymysql.cursors.DictCursor)
    res=cursor.callproc('p4',(3,111)) # set @_p4_0 = 3 ;set @_p4_1 = 111
    
    print(cursor.fetchall())
    
    cursor.execute('select @_p4_1;')
    print(cursor.fetchone())
    
    cursor.close()
    client.close()
  • 相关阅读:
    狗 日 的 360
    Django搭建简单的站点
    ZOJ 3675 Trim the Nails(bfs)
    Qt移动应用开发(二):使用动画框架
    SPOJ QTREE2 lct
    [Phonegap+Sencha Touch] 移动开发77 Cordova Hot Code Push插件实现自己主动更新App的Web内容
    Bitmap工具类BitmapHelper
    Material-design icon生成插件
    闲聊ROOT权限——ROOT权限的前世今生
    Java深入浅出系列(四)——深入剖析动态代理--从静态代理到动态代理的演化
  • 原文地址:https://www.cnblogs.com/kingyanan/p/9460123.html
Copyright © 2011-2022 走看看