簡體   English   中英

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

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

如何在 MySQL (SuiteCRM) 中自動創建唯一 ID 字段? 例如:'e3df34-dg324g-sdsew23-dsdsw2'

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM