user3147955
user3147955

Reputation: 1

I am trying to add variables into a new DB row using Python. The code I have written gets no error but doesn't write the new row. into the table?

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)

Upvotes: 0

Views: 29

Answers (0)

Related Questions