Advertisement
Guest User

Untitled

a guest
Nov 5th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.25 KB | None | 0 0
  1. import mysql.connector
  2. import getpass
  3. import datetime
  4. import sys
  5.  
  6. def connection():
  7.     mydb = mysql.connector.connect(
  8.         host="localhost",
  9.         user="root",
  10.         passwd=getpass.getpass("Password: "),
  11.         database="Expenses"
  12.         )
  13.     mycursor = mydb.cursor()
  14. # mycursor.execute("CREATE TABLE October (id INT AUTO_INCREMENT PRIMARY KEY, day VARCHAR(255), item VARCHAR(255), cost FLOAT)")
  15.  
  16. #mycursor.execute("ALTER TABLE October (id FLOAT AUTO_INCREMENT PRIMARY KEY)")
  17.  
  18.     currentDate = datetime.datetime.now().strftime("%y-%m-%d")
  19.     sql = "INSERT INTO October (day, item, cost) VALUES (%s, %s, %s)"
  20.     val = []
  21.     while True:
  22.         item = input("Please enter the name of the item: ")
  23.         cost = float(input("Please enter the price of {}: ".format(item)))
  24.         objects = currentDate, item, cost
  25.         entry = val.append(objects)
  26.         more_entries = input("Would you like to add more items?(Y/N) ")
  27.         if more_entries == "Y".lower():
  28.             pass
  29.         else:
  30.             mycursor.executemany(sql, val)
  31.             mydb.commit()
  32.             print(mycursor.rowcount, "records inserted with a total of {} of IDs.".format(mycursor.lastrowid))
  33.             sys.exit("All done here...")
  34.  
  35. connection()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement