zoukankan      html  css  js  c++  java
  • Python# Python通

    import pymysql
    import uuid
    # db = pymysql.connect(host='192.168.0.100',user='root',passwd='123456',db='mysql')
    #
    # cur = db.cursor()
    # # cur.execute("SELECT Host,User FROM user")
    # # cur.execute("SELECT version()")
    # cur.execute("SELECT * from testdb.person")
    #
    # print(cur.fetchall())
    #
    # cur.close()
    # db.close()
    
    
    
    
    # 生成 num 个验证码,每个长度为length,可设置默认长度
    def create_num(num,length=16):
        result = []
        while num > 0:
            uuid_id = uuid.uuid4()
            print(uuid_id)
            # 删去字符串中的'-',取出前length 个字符
            temp = str(uuid_id).replace('-', '')[:length]
            if temp not in result:
                result.append(temp)
                num -= 1
        return result
    
    # 保存到MySQL数据库
    def save_to_mysql():
        conn = pymysql.connect(host='192.168.0.100',user='root',passwd='123456',db='test1db')
        cur = conn.cursor()
        # cur.execute("INSERT INTO `jihuoma` (`id`, `name`) VALUES ('2', 'b')")
        # cur.execute("select * from test1db.jihuoma")
        conn.commit()
        print(cur.fetchall())
        cur.close()
        conn.close()
    save_to_mysql()
        # try:
        #     with conn.cursor() as cursor:
        #         # Create a new record
        #         sql = "INSERT INTO `jihuoma` (`code`) VALUES (%s)"
        #         cursor.execute(sql, code)
        #
        #         # connection is not autocommit by default. So you must commit to save
        #         # your changes.
        #         conn.commit()
        #
        #     with conn.cursor() as cursor:
        #         # Read a single record
        #         sql = "SELECT `id`, `code` FROM `jihuoma` WHERE `code`=%s"
        #         cursor.execute(sql, code)
        #         result = cursor.fetchone()
        #         print(result)
        # finally:
        #     conn.close()
    #
    # for code in create_num(20):
    #     save_to_mysql(code)
    #
  • 相关阅读:
    django系列5.4--ORM中执行原生SQL语句, Python脚本中调用django环境
    Cookie背景了解
    [leetcode] 832. Flipping an Image
    [leetcode] 888. Fair Candy Swap
    [leetcode] 66. Plus One
    0000:Deep Learning Papers Reading Roadmap
    [算法]时间复杂度
    [leetcode] 771. Jewels and Stones
    [cs231n] Convolutional Neural Networks for Visual Recognition
    推翻自己,从头来过
  • 原文地址:https://www.cnblogs.com/lwsup/p/7296512.html
Copyright © 2011-2022 走看看