title: Python 3.6 & Inserting Values into MariaDB via variable
body: <p>Code description - Running a program to add values to a maria db table. The program executes and prompts for the variables without error but the table shows no rows when I query it. Any idea where I am going wrong? Wanted to make it dynamic so I am using variables for the db and the table.
       ######## Output #########</p>
  
  <pre><code>[root@localhost student_program]# python db_setup.py
  Connection successful!!
  Database already exists: students
  create_table
  /usr/local/lib/python3.6/site-packages/pymysql/cursors.py:329: Warning: (1050, " Table 'students_info' already exists")
    self._do_get_result()
  use students;
  Enter the first name :Frank
  Enter the last name: Brady
  Enter the email address: frank@noemail.com
  Enter the address: 300 Main Street, Belfast
  Enter the DOB in YYYY-MM-DD: 1980-02-02
  Enter the english mark: 90
  Enter the maths mark: 80
  Enter the history mark: 70
  Enter the science mark: 45
  ('INSERT into students_info (firstname, lastname,email,address,DOB,english,maths,history,science) VALUES  (%s,%s,%s,%s,%s,%s,%s,%s,%s);', ('Frank', 'Brady', 'frank@noemail.com', '300 Main Street, Belfast', '1980-02-02', '90', '80', '70', '45'))
  
  
  ######### Program below #################
  
       import os,pymysql
  
      mariadb_root = '/var/lib/mysql/'
  
      db_to_create = 'students'
  
      conn = pymysql.connect(host='localhost',  user='xxx', passwd='xxx',  cursorclass=pymysql.cursors.DictCursor)
  
      print('Connection successful!!')
  
  
  # Check if db exists, if not create
  
  def check_db_exists():
      dbfile = os.path.join(mariadb_root, db_to_create)
      if not os.path.exists(dbfile):
          cur = conn.cursor()
          command = "CREATE DATABASE %s;" % db_to_create
          print(command)
          cur.execute(command)
          print("Database created %s:", db_to_create)
      else:
          print("Database already exists: %s" % db_to_create)
  check_db_exists()
  
  def create_database():
      cur = conn.cursor()
      command = "use %s; " %db_to_create
      cur.execute(command)
      create_table = ("""
                  CREATE TABLE IF NOT EXISTS students_info (
                  ID int NOT NULL AUTO_INCREMENT,
                  firstname varchar(255),
                  lastname varchar(255),
                  email varchar(255),
                  address varchar(255),
                  DOB DATE,
                  english varchar(255),
                  maths varchar(255),
                  history varchar(255),
                  science varchar(255),
                  PRIMARY KEY (ID));
                  """)
      print("create_table")
      cur.execute(create_table)
      print(command)
  
  create_database()
  
  def add_student():
      firstname = input("Enter the first name :")
      lastname = input("Enter the last name: ")
      email = input("Enter the email address: ")
      address = input("Enter the address: ")
      DOB = input("Enter the DOB in YYYY-MM-DD: ")
      english = input("Enter the english mark: ")
      maths = input("Enter the maths mark: ")
      history = input("Enter the history mark: ")
      science = input("Enter the science mark: ")
  
      cur = conn.cursor()
      command = "use %s; " %db_to_create
      cur.execute(command)
  
      cur.execute = ("""INSERT into students_info (firstname, lastname,email,address,DOB,english,maths,history,science) VALUES  (%s,%s,%s,%s,%s,%s,%s,%s,%s);""",
      (firstname,lastname,email,address, DOB,english,maths,history,science))
      print(cur.execute)
  
      cur.close()
      conn.close()
  add_student()
  </code></pre>