简体   繁体   中英

How to create a unique ID field in MySQL using Python

How to automatically create unique ID field in MySQL (SuiteCRM)? For example: '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()

You can you generate random uuid for the same. 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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