Python 通

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)
#

 

以上是关于Python 通的主要内容,如果未能解决你的问题,请参考以下文章

常用python日期日志获取内容循环的代码片段

简单的方法来分享/讨论/协作的代码片段?

python 有用的Python代码片段

Python 向 Postman 请求代码片段

python [代码片段]一些有趣的代码#sort

使用 Python 代码片段编写 LaTeX 文档