#====================================================================== # Save data in a database #====================================================================== import pymysql as mydb #Python 3x Mysql database driver # import cx_Oracle as oradb #if using Oracle database driver firstname = input("Enter your first name: ") #prompt for user entry lastname = input("Enter your last name: ") address = input("Enter your address: ") flavor = input("Enter your ice cream flavor: ") topping = input("Enter your ice cream topping: ") creditCard = input("Enter your credit card: ") def saveDB( ): #save data to database sql = F""" INSERT INTO cust_order VALUES(null, '{firstname}', '{lastname}', '{address}', '{flavor}', '{topping}', '{creditCard}', 1 ) """ # print(sql) #debugging to ensure SQL code is OK try: conn = mydb.connect(host='localhost',user='demo2',password='demo2',database='demo2') # conn = oradb.connect( 'demo2/demo2@localhost:1521/orcl' ) cursor = conn.cursor( ) #create a cursor cursor.execute(sql); #execute the sql conn.commit( ) except mydb.Error as e: #catch MySql DB errors (errorNum, errorMsg) = e.args msg = 'Database Error - ' + str(errorNum) + errorMsg return # except oradb.DatabaseError as e: #catch Oracle DB errors # msg = 'Database Error - ' + e # return print("Your order has been saved successfully") cursor.close( ) #close the cursor/buffer conn.close( ) #close the connection #===================================================================== # Main code #===================================================================== saveDB( ) #save into the database