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

如何使用Python在MySQL中创建唯一的ID字段,python,mysql,suitecrm,Python,Mysql,Suitecrm,如何在MySQL(SuiteCRM)中自动创建唯一的ID字段?例如:“e3df34-dg324g-sdsew23-DSW2” python: try: with connection.cursor() as cursor: sql = "INSERT INTO `accounts` (`id`, `name`) VALUES (%s, %s)" cursor.execute(sql, ('sdi_2340

如何在MySQL(SuiteCRM)中自动创建唯一的ID字段?例如:“e3df34-dg324g-sdsew23-DSW2”

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()

您可以为相同的对象生成随机uuid。使用下面的代码

    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()

例如:“e3df34-dg324g-sdsew23-DSW2”非标准值。。。通过您自己的用户定义函数生成。id应该是什么样子?它是固定格式还是随机格式?