Mar-10-2022, 09:49 PM
I am creating a query on the fly based on user inputs. I have separated my code into building the SELECT statements, the JOIN caluses and finally the WHERE clauses. To prevent SQL injection I am trying to use the qmark formatting and passing the variable for the user inputs rather than using Python string operations to create the query. I have the actual query snippets in a separate class so it's easier to debug them.
Thisis a subset of the code that builds the query
cmdvariables contains ("%McG%",)
command contains
SELECT
contacts_table.id_contactsid
, contacts_table.contact_first_name
, contacts_table.contact_company
, flock_prefix_table.flock_prefix
FROM contacts_table
LEFT JOIN owner_registration_table
ON contacts_table.id_contactsid = owner_registration_table.id_contactsid
LEFT JOIN flock_prefix_table
ON flock_prefix_table.id_flockprefixid = owner_registration_table.id_flockprefixid
WHERE contacts_table.contact_last_name LIKE (?)
and it generates this error on the line resultcursor.execute(cmd, cmdvariables)
Exception in Tkinter callback
I am pretty sure that the problem is that the cmd, cmdvariables format is incorrect with missing or extra parentheses or commas. I thought that by creating the single item tuple for the variable that was correct per the documentation but I am clearly not understanding it or not building my query correctly.
In the larger app I will have to have several WHERE statements that will be joined using AND operators so I have to buuild the query on the fly.
I have tested the query against my database by entering it in by hand and it works but I can't enter it in exactly in the structure that Python expects so I can't test the generated query outside running the program.
I'm really hoping it's a simple stupid error that is totally obvious to everyone else.
Thanks in advance for you assistance and patience.
update, tried to fix the indentation errors
Thisis a subset of the code that builds the query
def search_contact(self): checkboxvalues = self.get_checkbox_values() cmd = AnimalTrakker_Query_Code.display_start_select_contacts_table cmdjoinclause = "" cmdwhereclause = "" if checkboxvalues[0] : cmd = cmd + AnimalTrakker_Query_Code.display_member_name_true_select if checkboxvalues[1]: cmd = cmd +AnimalTrakker_Query_Code.display_farm_name_true_select if checkboxvalues[2]: cmd = cmd + AnimalTrakker_Query_Code.display_flock_prefix_true_select cmdjoinclause = AnimalTrakker_Query_Code.join_flock_prefix_table cmd = cmd + "FROM contacts_table " cmd = cmd + cmdjoinclause cmdvariables = "" search_name_data_entry = self.contact_name.get() if search_name_data_entry: cmdwhereclause = 'WHERE contacts_table.contact_last_name LIKE (?)' cmdvariables = '"%'+search_name_data_entry+'%",' print("cmdvariables contains "+cmdvariables) search_prefix_data_entry = self.farm_prefix.get() print(search_prefix_data_entry) if search_prefix_data_entry: if search_name_data_entry: cmdwhereclause = cmdwhereclause + 'AND flock_prefix_table.flock_prefix LIKE (?)' cmdvariables = cmdvariables + ' "%' + search_prefix_data_entry + '%",' print("cmdvariables contains "+cmdvariables) else: cmdwhereclause = cmdwhereclause + 'WHERE flock_prefix_table.flock_prefix LIKE (?)' cmdvariables = cmdvariables + '"%' + search_prefix_data_entry + '%",' print("cmdvariables contains "+cmdvariables) if cmdvariables: cmdvariables = " (" + cmdvariables + ")" cmd = cmd + cmdwhereclause print("command contains "+cmd) if cmdvariables: cmdvariables = " (" + cmdvariables + ")" cmd = cmd + cmdwhereclause print("command contains "+cmd) connection = sqlite3.connect(config.currentdatabase) resultcursor = connection.cursor() if cmdvariables: resultcursor.execute(cmd, cmdvariables) else: resultcursor.execute(cmd) results = resultcursor.fetchall()The query snippets in the query class are
display_start_select_contacts_table =""" SELECT contacts_table.id_contactsid """ display_member_name_true_select =""" , contacts_table.contact_first_name """ display_farm_name_true_select = """ , contacts_table.contact_company """ display_flock_prefix_true_select = """ , flock_prefix_table.flock_prefix """ join_flock_prefix_table = """ LEFT JOIN owner_registration_table ON contacts_table.id_contactsid = owner_registration_table.id_contactsid LEFT JOIN flock_prefix_table ON flock_prefix_table.id_flockprefixid = owner_registration_table.id_flockprefixid """When I run the program after entering in McG in the contact last name text entry box I get the following output.
cmdvariables contains ("%McG%",)
command contains
SELECT
contacts_table.id_contactsid
, contacts_table.contact_first_name
, contacts_table.contact_company
, flock_prefix_table.flock_prefix
FROM contacts_table
LEFT JOIN owner_registration_table
ON contacts_table.id_contactsid = owner_registration_table.id_contactsid
LEFT JOIN flock_prefix_table
ON flock_prefix_table.id_flockprefixid = owner_registration_table.id_flockprefixid
WHERE contacts_table.contact_last_name LIKE (?)
and it generates this error on the line resultcursor.execute(cmd, cmdvariables)
Exception in Tkinter callback
Error:Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 1883, in __call__
return self.func(*args)
File "/Users/eugeniemmcguire/PycharmProjects/AnimalTrakkerSystem/AnimalTrakkerUI/BullTestContactScreens.py", line 279, in search_contact
resultcursor.execute(cmd, cmdvariables)
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 11 supplied.
If I do not do any WHERE selections the execution works as expected.I am pretty sure that the problem is that the cmd, cmdvariables format is incorrect with missing or extra parentheses or commas. I thought that by creating the single item tuple for the variable that was correct per the documentation but I am clearly not understanding it or not building my query correctly.
In the larger app I will have to have several WHERE statements that will be joined using AND operators so I have to buuild the query on the fly.
I have tested the query against my database by entering it in by hand and it works but I can't enter it in exactly in the structure that Python expects so I can't test the generated query outside running the program.
I'm really hoping it's a simple stupid error that is totally obvious to everyone else.
Thanks in advance for you assistance and patience.
update, tried to fix the indentation errors