Delrius Euphoria
Delrius Euphoria

Reputation: 15098

Failed processing format-parameters; Python 'tuple' cannot be converted to a MySQL type

I was doing this piece of code here and i'm getting error here for the code that tuples cant be used in mysql? why is that? is there a mistake in code?

Code:

            def edit():
                manage = Toplevel(update)
                manage.title('Manage')
                #Clearing entry box
                e_i_d.delete(0,END) 

                def edit():
                    #Establishing connection
                    con = mysql.connect(host='^^^.^^', user='user',
                                            password='^^^^', database='DB')
                    # Making SQL command
                    sql_command = "UPDATE patient_info set `full_name`=%s ,`ph_no`=%s,`emirate_id`=%s,`email_addr`=%s,`gender`=%s,`DOB`=%s,`nationality`=%s,`blood_grp`=%s,`COVID_test`=%s,`emergency_no`=%s ;"
                    values = e10.get(), e20.get(), e30.get(), e40.get(), g.get(), e_dt.get(), e50.get(),b.get(),co.get() ,e60.get() 
                    c = con.cursor()
                    #Executing and saving SQL command
                    c.execute(sql_command, (values,))
                    c.execute('commit')
                    con.close()    

                #Defining Labels
                l_head = Label(manage, text='Edit',font=Font(size='20'))
                l1 = ttk.Label(manage, text='Name', font=font_text)
                l2 = ttk.Label(manage, text='Phone Number', font=font_text)
                l3 = ttk.Label(manage, text='Emirates ID', font=font_text)
                l4 = ttk.Label(manage, text='Email Address', font=font_text)
                l5 = ttk.Label(manage, text='Nationality', font=font_text)
                l6 = ttk.Label(manage, text='Emergency Contact Number', font=font_text)
                l7 = ttk.Label(manage, text='Gender', font=font_text)
                l8 = ttk.Label(manage, text='Blood Group', font=font_text)
                l9 = ttk.Label(manage, text='Test for COVID-19', font=font_text)
                l10 = ttk.Label(manage, text='DOB', font=font_text)
                l_wa = Label(manage, text='NOTE: Date must be in yyyy-mm-dd format always', font=font_text,fg='red')

                #Defining Entry boxes and button
                e10 = ttk.Entry(manage)
                e20 = ttk.Entry(manage)
                e30 = ttk.Entry(manage)
                e40 = ttk.Entry(manage)
                e50 = ttk.Entry(manage)
                e60 = ttk.Entry(manage)
                e_dt = ttk.Entry(manage)
                b_cls = ttk.Button(manage, text='Close',command=manage.destroy)
                b_db = ttk.Button(manage, text='Update Data',command=edit)
                
                main = records[0]

                #Defining Dropdowns
                g = StringVar()
                g.set(main[5])
                opt_g = OptionMenu(manage, g, *gen)

                b = StringVar()
                b.set(main[8])
                opt_blo = OptionMenu(manage, b, *bl_gr)

                co = StringVar()
                co.set(main[9])
                opt_cov = OptionMenu(manage, co, *cov)

                #Placing entry boxes on screen
                e10.grid(row=1, column=1, pady=5, ipady=5, padx=5)
                e20.grid(row=1, column=3, pady=5, ipady=5, padx=5)
                e30.grid(row=2, column=1, pady=5, ipady=5, padx=5)
                e40.grid(row=2, column=3, pady=5, ipady=5, padx=5)
                e50.grid(row=3, column=1, pady=6, ipady=5, padx=5)
                e60.grid(row=3, column=3, pady=5, ipady=5, padx=5)

                #Inserting results on to boxes
                for record in records:
                    e10.insert(0,record[1])
                    e20.insert(0,record[2])
                    e30.insert(0,record[3])
                    e40.insert(0,record[4])
                    e50.insert(0,record[7])
                    e60.insert(0,record[10])
                    e_dt.insert(0,record[6])
                # Closing the connection
                con.close()
                
                #Placing labels and dropdowns on screen
                l_head.grid(row=0, columnspan=5, pady=10, padx=5)
                l1.grid(row=1, column=0, pady=5, ipady=5, padx=5)
                l2.grid(row=1, column=2, pady=5, ipady=5, padx=5)
                l3.grid(row=2, column=0, pady=5, ipady=5, padx=5)
                l4.grid(row=2, column=2, pady=5, ipady=5, padx=5)
                l5.grid(row=3, column=0, pady=5, ipady=5, padx=5)
                l6.grid(row=3, column=2, pady=5, ipady=5, padx=5)
                l7.grid(row=4, column=0, pady=5, ipady=5, padx=5)
                l8.grid(row=4, column=2, pady=5, ipady=5, padx=5)
                l9.grid(row=5, column=0, pady=5, ipady=5, padx=5)
                l10.grid(row=5, column=2, pady=5, ipady=5, padx=5)

                opt_g.grid(row=4, column=1, pady=5, ipadx=10, padx=5)
                opt_blo.grid(row=4, column=3, pady=5, ipadx=10, padx=5)
                opt_cov.grid(row=5, column=1, pady=5, ipadx=10, padx=5)
                e_dt.grid(row=5, column=3, pady=5, ipady=5, padx=5)
                b_db.grid(row=7, columnspan=5, pady=(5,0), ipadx=10, sticky=E+W)
                b_cls.grid(row=8, columnspan=5, pady=(5,0), ipadx=10, sticky=E+W)
                l_wa.grid(row=6, columnspan=5, pady=5, ipady=5, padx=5, sticky=E+W)

Error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\xxxx\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\conversion.py", line 183, in to_mysql
    return getattr(self, "_{0}_to_mysql".format(type_name))(value)
AttributeError: 'MySQLConverter' object has no attribute '_tuple_to_mysql'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\xxxx\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\cursor.py", line 432, in _process_params
    res = [to_mysql(i) for i in res]
  File "C:\Users\xxxx\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\cursor.py", line 432, in <listcomp>
    res = [to_mysql(i) for i in res]
  File "C:\Users\xxxx\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\conversion.py", line 186, in to_mysql
    "MySQL type".format(type_name))
TypeError: Python 'tuple' cannot be converted to a MySQL type

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\xxxx\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "c:\pyprojects\patient data entry\Patient_Information.py", line 125, in edit
    c.execute(sql_command, (values,))
  File "C:\Users\xxxx\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\cursor.py", line 557, in execute
    psub = _ParamSubstitutor(self._process_params(params))
  File "C:\Users\xxxx\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\cursor.py", line 437, in _process_params
    "Failed processing format-parameters; %s" % err)
mysql.connector.errors.ProgrammingError: Failed processing format-parameters; Python 'tuple' cannot be converted to a MySQL type

Upvotes: 0

Views: 2138

Answers (1)

MacOS
MacOS

Reputation: 1159

I guess that it breaks here

c.execute(sql_command, (values,))

which means that either sql_command or values causes the problem. Either you write a test to check the values for this, or you add a print statement for values. I strongly believe that values has somewhere a python tuple where there should not be one.

I hope this helps!

Upvotes: 1

Related Questions