简体   繁体   English

如何使用 Python 在 MySQL 中创建唯一 ID 字段

[英]How to create a unique ID field in MySQL using Python

How to automatically create unique ID field in MySQL (SuiteCRM)?如何在 MySQL (SuiteCRM) 中自动创建唯一 ID 字段? For example: 'e3df34-dg324g-sdsew23-dsdsw2'例如:'e3df34-dg324g-sdsew23-dsdsw2'

python: Python:

    try:
        with connection.cursor() as cursor:
            sql = "INSERT INTO `accounts` (`id`, `name`) VALUES (%s, %s)"
            cursor.execute(sql, ('sdi_234023', 'Alex'))

        connection.commit()
    finally:
        connection.close()

You can you generate random uuid for the same.您可以为此生成随机 uuid。 Using following code.使用以下代码。

    from uuid import uuid4
    try:
        with connection.cursor() as cursor:
            sql = "INSERT INTO `accounts` (`id`, `name`) VALUES (%s, %s)"
            cursor.execute(sql, (str(uuid4), 'Alex'))

        connection.commit()
    finally:
        connection.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM