Form submitted
No entry in the database
The image attached describes the problem that I am facing.
I can’t find any problem in the code stated below. Is there any problem with the SQL connection? Kindly state what should I do to solve this problem. I have sent this to my friends as well and they also can’t connect it to the database.
import tkinter as tk
from tkinter import messagebox, ttk
from tkcalendar import DateEntry
import mysql.connector
def submit_form(self):
try:
# Connect to the MySQL database
conn = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="shipment"
)
cursor = conn.cursor()
# Extract data from entry fields
data = {}
for field, entry in self.entry_fields.items():
data[field] = entry.get()
# Insert basic information into the preshipment table
preshipment_query = "INSERT INTO preshipment (Account, Shipper, AIN_BIN, Depot_Name, Commodity, Quantity, Date_of_Stuffing, BL_No, Container_No, Seal_No, Number_of_Transshipment"
preshipment_values = (
data["Account"],
data["Shipper"],
data["AIN/BIN"],
data["Depot Name"],
data["Commodity"],
data["Quantity"],
data["Date of Stuffing"],
data["BL No."],
data["Container No."],
data["Seal No."],
data["Number of Transshipment"]
)
num_transshipment = int(data["Number of Transshipment"])
if num_transshipment == 0:
preshipment_query += ", Feeder Vessel_Voyage, 1st Departure Port, 1st Arrival Port, 1st Departure Date, 1st Arrival Date"
preshipment_values += (
data["Feeder Vessel_Voyage"],
data["1st Departure Port"],
data["1st Arrival Port"],
data["1st Departure Date"],
data["1st Arrival Date"]
)
elif num_transshipment == 1:
preshipment_query += ", Feeder Vessel_Voyage, 1st Departure Port, 1st Arrival Port, 1st Departure Date, 1st Arrival Date, 1st Mother Vessel_Voyage, 2nd Departure Port, 2nd Arrival Port, 2nd Departure Date, 2nd Arrival Date"
preshipment_values += (
data["Feeder Vessel_Voyage"],
data["1st Departure Port"],
data["1st Arrival Port"],
data["1st Departure Date"],
data["1st Arrival Date"],
data["1st Mother Vessel_Voyage"],
data["2nd Departure Port"],
data["2nd Arrival Port"],
data["2nd Departure Date"],
data["2nd Arrival Date"]
)
elif num_transshipment == 2:
preshipment_query += ",Feeder Vessel_Voyage, 1st Departure Port, 1st Arrival Port, 1st Departure Date, 1st Arrival Date, 1st Mother Vessel_Voyage, 2nd Departure Port, 2nd Arrival Port, 2nd Departure Date, 2nd Arrival Date, 2nd Mother Vessel_Voyage, 3rd Departure Port, 3rd Arrival Port, 3rd Departure Date, 3rd Arrival Date"
preshipment_values += (
data["Feeder Vessel_Voyage"],
data["1st Departure Port"],
data["1st Arrival Port"],
data["1st Departure Date"],
data["1st Arrival Date"],
data["1st Mother Vessel_Voyage"],
data["2nd Departure Port"],
data["2nd Arrival Port"],
data["2nd Departure Date"],
data["2nd Arrival Date"],
data["2nd Mother Vessel_Voyage"],
data["3rd Departure Port"],
data["3rd Arrival Port"],
data["3rd Departure Date"],
data["3rd Arrival Date"]
)
preshipment_query += ") VALUES (" + ("%s, " * (len(preshipment_values) - 1)) + "%s)"
cursor.execute(preshipment_query, preshipment_values)
# Commit changes and close cursor and connection
conn.commit()
cursor.close()
conn.close()
# Display success message
messagebox.showinfo("Form Submitted", "Form submitted successfully!")
except mysql.connector.Error as e:
# Display error message if an error occurs
messagebox.showerror("Error", f"An error occurred: {e}")
class MyApp:
def __init__(self, root):
self.root = root
self.root.title("Desktop App")
self.root.geometry("600x400") # Larger window size
self.root.configure(bg="white")
# Create a frame for the left side
self.left_frame = tk.Frame(self.root, bg="DodgerBlue4")
self.left_frame.pack(side="left", fill="both", expand=True)
# Create a welcome message label
self.welcome_label = tk.Label(self.left_frame, text="Let's get Started!", font=("Helvetica", 16), bg="DodgerBlue4", fg="white")
self.welcome_label.pack(pady=50)
self.welcome_label.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
# Create a frame for the right side
self.right_frame = tk.Frame(self.root, bg="white")
self.right_frame.pack(side="right", fill="both", expand=True)
# Create a frame to hold the buttons
self.button_frame = tk.Frame(self.right_frame, bg="white")
self.button_frame.pack(pady=50, padx=20, anchor="center")
# Create buttons with some basic styling
self.btn1 = tk.Button(self.button_frame, text="Pre-shipment Advice", command=self.pre_shipment_advice, width=20, height=1, bg="DodgerBlue4", fg="white", font=("Helvetica", 12))
self.btn1.pack(pady=10)
self.btn2 = tk.Button(self.button_frame, text="Stuffing Report Analysis", command=self.stuffing_report_analysis, width=20, height=1, bg="DodgerBlue4", fg="white", font=("Helvetica", 12))
self.btn2.pack(pady=10)
self.suggestion_displayed = False # Flag to track if suggestion box is displayed
# Configure weight to make both frames equal in size
self.root.grid_columnconfigure(1, weight=1)
self.root.grid_columnconfigure(1, weight=1)
def pre_shipment_advice(self):
# Create a new window for Pre-shipment Advice
self.advice_window = tk.Toplevel(self.root)
self.advice_window.title("Pre-shipment Advice")
self.advice_window.geometry("1200x600") # Set window size
# Configure background color
self.advice_window.configure(bg="lightgrey")
# Frame for the main fields
self.main_frame = tk.Frame(self.advice_window, bg="lightgrey")
self.main_frame.pack(pady=10)
# Frame for text fields
self.text_fields_frame = tk.Frame(self.main_frame, bg="snow2")
self.text_fields_frame.grid(row=0, column=0, columnspan=2, padx=10, pady=5, sticky="ew")
# Form fields
fields = ["Account", "Shipper", "AIN/BIN", "Depot Name", "Commodity", "Quantity", "Date of Stuffing", "BL No.", "Container No.", "Seal No."]
# Add labels and entry fields for each field
self.entry_fields = {}
for i, field in enumerate(fields):
if i < 5:
column = 0
else:
column = 2
i -= 5
label = tk.Label(self.text_fields_frame, text=field + ":", bg="snow2") # Set background color for labels
label.grid(row=i, column=column, padx=10, pady=5, sticky="e")
if field == "Date of Stuffing":
entry = DateEntry(self.text_fields_frame, date_pattern="dd/mm/yy")
entry.grid(row=i, column=column + 1, padx=10, pady=5, sticky="w")
elif field == "Depot Name":
entry = ttk.Combobox(self.text_fields_frame, width=30) # Adjust the width as needed
entry.grid(row=i, column=column + 1, padx=10, pady=5, sticky="w")
entry['values'] = ["BM Container Depot Limited", "Esack Brothers Industries Limited (Container Yard)", "Eastern Logistics Limited","Golden Containers Limited", "Haji Saber Ahmed Timer Company Limited (Container Yard)", "Incontrade Limited", "KDS Logistics Limited", "K&T Logistics Limited", "Nemsan Container Limited", "Port Link Logistics Centre Limited", "QNS Container Services Limited", "Summit Alliance Port Limited ", "Shafi Motors Limited", "Vertex Off-dock Logistic Services Limited"]
else:
entry = tk.Entry(self.text_fields_frame)
entry.grid(row=i, column=column + 1, padx=10, pady=5, sticky="w")
self.entry_fields[field] = entry
# Frame for shipment details
self.shipment_details_frame = tk.Frame(self.main_frame, bg="lightgrey")
self.shipment_details_frame.grid(row=1, column=0, columnspan=2, padx=10, pady=10, sticky="ew")
# Shipment Details label
self.shipment_details_label = tk.Label(self.shipment_details_frame, text="Shipment Details", bg="lightgrey", font=("Helvetica", 12, "bold"))
self.shipment_details_label.grid(row=0, column=0, columnspan=2, padx=10, pady=5, sticky="w")
# Number of Transshipment label
self.transshipment_label = tk.Label(self.shipment_details_frame, text="Number of Transshipment:", bg="lightgrey")
self.transshipment_label.grid(row=1, column=0, padx=10, pady=5, sticky="e")
# Combobox for Number of Transshipment
self.transshipment_combobox = ttk.Combobox(self.shipment_details_frame, values=["0", "1", "2"], width=5)
self.transshipment_combobox.grid(row=1, column=1, padx=10, pady=5, sticky="w")
self.transshipment_combobox.bind("<<ComboboxSelected>>", self.transshipment_selection)
# Frame for submit button
self.submit_button_frame = tk.Frame(self.advice_window, bg="lightgrey")
self.submit_button_frame.pack(side="bottom", pady=10)
# Submit button
self.submit_button = tk.Button(self.submit_button_frame, text="Submit", command=self.submit_form, width=10, height=1, bg="snow2", fg="black", font=("Helvetica", 10, "bold"))
self.submit_button.pack(pady=10)
def transshipment_selection(self, event=None):
num_transshipment = int(self.transshipment_combobox.get())
if hasattr(self, "transshipment_frame"):
# Remove any event bindings associated with widgets inside transshipment_frame
self.transshipment_combobox.unbind("<<ComboboxSelected>>")
self.transshipment_combobox.bind("<<ComboboxSelected>>", self.transshipment_selection)
self.transshipment_frame.destroy()
if num_transshipment == 0:
self.transshipment_frame = tk.Frame(self.main_frame, bg="lightgrey")
self.transshipment_frame.grid(row=2, column=0, columnspan=2, padx=10, pady=10, sticky="ew")
fields = ["Feeder Vessel_Voyage", "1st Departure Port", "1st Arrival Port", "1st Departure Date", "1st Arrival Date"]
for i, field in enumerate(fields):
label = tk.Label(self.transshipment_frame, text=field + ":", bg="lightgrey")
label.grid(row=i, column=0, padx=10, pady=5, sticky="e")
if field.endswith("Date"):
entry = DateEntry(self.transshipment_frame, date_pattern="dd/mm/yy")
elif "Port" in field:
entry = ttk.Combobox(self.transshipment_frame, width=30) # Adjust the width as needed
entry.grid(row=i, column=1, padx=10, pady=5, sticky="w")
if field == "1st Departure Port":
entry['values'] = ["BDCGP"]
elif field == "1st Arrival Port":
entry['values'] = ["SGSIN", "CNNGB", "CNSHA", "MYPKG"]
else:
entry = tk.Entry(self.transshipment_frame)
entry.grid(row=i, column=1, padx=10, pady=5, sticky="w")
self.entry_fields[field] = entry
elif num_transshipment == 1:
self.transshipment_frame = tk.Frame(self.main_frame, bg="lightgrey")
self.transshipment_frame.grid(row=2, column=0, columnspan=2, padx=10, pady=10, sticky="ew")
fields = ["Feeder Vessel_Voyage", "1st Departure Port", "1st Arrival Port", "1st Departure Date", "1st Arrival Date",
"1st Mother Vessel_Voyage", "2nd Departure Port", "2nd Arrival Port", "2nd Departure Date", "2nd Arrival Date"]
for i, field in enumerate(fields):
row = i % 5
col = i // 5
label = tk.Label(self.transshipment_frame, text=field + ":", bg="lightgrey")
label.grid(row=row, column=col*2, padx=10, pady=5, sticky="e")
if field.endswith("Date"):
entry = DateEntry(self.transshipment_frame, date_pattern="dd/mm/yy")
elif "Port" in field:
entry = ttk.Combobox(self.transshipment_frame, width=30) # Adjust the width as needed
entry.grid(row=row, column=col*2 + 1, padx=10, pady=5, sticky="w")
if field == "1st Departure Port":
entry['values'] = ["BDCGP"]
elif field == "1st Arrival Port":
entry['values'] = ["SGSIN", "CNNGB", "CNSHA", "MYPKG"]
elif field == "2nd Departure Port":
entry['values'] = ["SGSIN", "CNNGB", "CNSHA", "MYPKG"]
elif field == "2nd Arrival Port":
entry['values'] = ["RUVVO", "KRPUS", "RUVYP", "TRKMX"]
else:
entry = tk.Entry(self.transshipment_frame)
entry.grid(row=row, column=col*2 + 1, padx=10, pady=5, sticky="w")
self.entry_fields[field] = entry
elif num_transshipment == 2:
self.transshipment_frame = tk.Frame(self.main_frame, bg="lightgrey")
self.transshipment_frame.grid(row=2, column=0, columnspan=2, padx=10, pady=10, sticky="ew")
fields = ["Feeder Vessel_Voyage", "1st Departure Port", "1st Arrival Port", "1st Departure Date", "1st Arrival Date",
"1st Mother Vessel_Voyage", "2nd Departure Port", "2nd Arrival Port", "2nd Departure Date", "2nd Arrival Date",
"2nd Mother Vessel_Voyage", "3rd Departure Port", "3rd Arrival Port", "3rd Departure Date", "3rd Arrival Date"]
for i, field in enumerate(fields):
row = i % 5
col = i // 5
label = tk.Label(self.transshipment_frame, text=field + ":", bg="lightgrey")
label.grid(row=row, column=col*2, padx=10, pady=5, sticky="e")
if field.endswith("Date"):
entry = DateEntry(self.transshipment_frame, date_pattern="dd/mm/yy")
elif "Port" in field:
entry = ttk.Combobox(self.transshipment_frame, width=25) # Adjust the width as needed
entry.grid(row=row, column=col*2 + 1, padx=10, pady=5, sticky="w")
if field == "1st Departure Port":
entry['values'] = ["BDCGP"]
elif field == "1st Arrival Port":
entry['values'] = ["SGSIN", "CNNGB", "CNSHA", "MYPKG"]
elif field == "2nd Departure Port":
entry['values'] = ["SGSIN", "CNNGB", "CNSHA", "MYPKG"]
elif field == "2nd Arrival Port":
entry['values'] = ["RUVVO", "KRPUS", "RUVYP", "TRKMX"]
elif field == "3rd Departure Port":
entry['values'] = ["TRKMX"]
elif field == "3rd Arrival Port":
entry['values'] = ["RUNOV"]
else:
entry = tk.Entry(self.transshipment_frame)
entry.grid(row=row, column=col*2 + 1, padx=10, pady=5, sticky="w")
self.entry_fields[field] = entry
def stuffing_report_analysis(self):
# Show a message box for the Stuffing Report Analysis
messagebox.showinfo("Stuffing Report Analysis", "Stuffing Report Analysis button clicked!")
def submit_form(self):
# Placeholder for form submission logic
messagebox.showinfo("Form Submitted", "Form submitted successfully!")
root = tk.Tk()
app = MyApp(root)
root.mainloop()
I tried to find the problem in the code but couldn’t find any.
2
Answers
Here are some tips to debug your application:
(1) Open a terminal and manually run database connection part and fire a query using cursor.execute() like select and check whether it runs:
(2) Print query in console in every condition part like,
in if – elif condition every part (you can use logging instead of print as well)
After getting whole query you can manually fire it in python cursor.execute() and check what happens or you can directly fire in mysql query runner (PHPMyAdmin).
This way you will also get whether your function receives submitted data or not.
If you are using pycharm of vscode you should definitely use debugger and breakpoints to solve this issue.
Make sure you have installed
pip install mysql-connector-python
and not
pip install mysql
because the former does not have connector module try to make the connection first then go about debugging further.