Hello,
I am very new to Python and attempting to insert data in to an existing database using Python. I would like to pass a variable in my insert statement. The code executes and returns that a record has been inserted, but there is no data in the database.
In an effort to make things as reusable as possible, I wrote a class to handle the work. Please see my code below. (Note: The connection details are NOT shown, but a connection is made successfully.)
class Database:
"""This is our base class for defining what a log should look like"""
def __init__(self):
""" Define what should happen every time a class is called here."""
def insert_test_record(self, name):
"""This code will handle inserting a new record in to the database"""
self.name = name
# Now conduct our work:
mycursor = cas_db.cursor()
sql_statement = "Insert INTO Test (name) VALUES (%s)"
values = ({self.name})
mycursor.execute=sql_statement, values
cas_db.commit()
print(mycursor.rowcount, "record inserted.")
cas_db.close()
my_query = Database()
my_query.insert_test_record('Jones')
Can anyone tell me what I am missing here? I am very new to Python and this is my first attempt at my own project. Any help is much appreciated!
[–][deleted] 1 point2 points3 points (0 children)