[Python] I am trying to add variables into a new DB row using Python. The code I have written...

Discussão em 'Python' iniciado por Stack, Fevereiro 2, 2025.

  1. Stack

    Stack Membro Participativo


    Connection to database
    ​

    import datetime
    import pymysql
    conn = pymysql.connect(
    host='localhost',
    user='XXXX',
    #password=,
    db='golf_db',
    charset='utf8mb4',
    cursorclass=pymysql.cursors.DictCursor
    )


    #Define variables for the table row
    ​

    lastname="Doe"
    firstname="John"
    front9=8
    back9=8
    birthday=datetime.date(2025,1,31)


    SQL statements to add row
    ​

    with conn.cursor() as cursor:

    sql = "INSERT INTO `Golfers` (`Last_Name`, `First_Name`, `F_Handicap`, `B_Handicap`, `B_Date`) VALUES (%(lastname)s, %(firstname)s, %(front9)s, %(back9)s, %(birthday)s)"
    conn.commit()


    Check to see if variables were properly defined
    ​

    print(firstname, lastname, front9, back9, birthday)

    Continue reading...

Compartilhe esta Página