I can’t seem to get two values passed on a query, I can get one variable to pass no problem, can’t figure out what I’m doing wrong, error posted when I try to use the two variable code, one variable code passes.
One Variable:
JavaScript
x
18
18
1
mycursor = mydb.cursor()
2
3
mycursor.execute("SELECT `HOME TEAM:` FROM `nbav8` WHERE `HOME TEAM:` is NOT NULL")
4
5
myresult = mycursor.fetchall()
6
7
awayuyu = []
8
homeuyu = []
9
kt = "Atlanta"
10
for team in myresult:
11
mycursor = mydb.cursor()
12
13
mycursor.execute("SELECT `ACTUAL OVER UNDER RESULT:` FROM `previous_losses_nbav1_with_date_agg` WHERE `HOME TEAM:` = %s", team)
14
myresult = mycursor.fetchall()
15
print(myresult)
16
17
print(myresult)
18
Two Variable:
JavaScript
1
17
17
1
mycursor = mydb.cursor()
2
3
mycursor.execute("SELECT `HOME TEAM:` FROM `nbav8` WHERE `HOME TEAM:` is NOT NULL")
4
5
myresult = mycursor.fetchall()
6
7
awayuyu = []
8
homeuyu = []
9
kt = "Atlanta"
10
for team in myresult:
11
mycursor = mydb.cursor()
12
mycursor.execute("SELECT `ACTUAL OVER UNDER RESULT:` FROM `previous_losses_nbav1_with_date_agg` WHERE `HOME TEAM:` = %s OR 'AWAY TEAM:' = %s;", team, team)
13
myresult = mycursor.fetchall()
14
print(myresult)
15
16
print(myresult)
17
Error:
JavaScript
1
9
1
Traceback (most recent call last):
2
File "C:/Users/joshb/PycharmProjects/NBA_V8/grabbing_page_data.py", line 26, in <module>
3
mycursor.execute("SELECT `ACTUAL OVER UNDER RESULT:` FROM `previous_losses_nbav1_with_date_agg` WHERE `HOME TEAM:` = %s OR 'AWAY TEAM:' = %s;", team, team)
4
File "C:UsersjoshbPycharmProjectspythonProject2venvlibsite-packagesmysqlconnectorcursor.py", line 540, in execute
5
stmt = RE_PY_PARAM.sub(psub, stmt)
6
File "C:UsersjoshbPycharmProjectspythonProject2venvlibsite-packagesmysqlconnectorcursor.py", line 77, in __call__
7
raise errors.ProgrammingError(
8
mysql.connector.errors.ProgrammingError: Not enough parameters for the SQL statement
9
Updated code after answer:
JavaScript
1
21
21
1
mycursor = mydb.cursor()
2
3
mycursor.execute("SELECT `HOME TEAM:` FROM `nbav8` WHERE `HOME TEAM:` is NOT NULL")
4
5
myresult = mycursor.fetchall()
6
print(myresult)
7
yeet = 0
8
awayuyu = []
9
homeuyu = []
10
kt = "Atlanta"
11
temp1 = "Miami"
12
temp2 = "Toronto"
13
for team in myresult:
14
data1 = team
15
data2 = team
16
mycursor = mydb.cursor()
17
sql = """SELECT `ACTUAL OVER UNDER RESULT:` FROM `previous_losses_nbav1_with_date_agg` WHERE `HOME TEAM:` = %s OR 'AWAY TEAM:' = %s"""
18
mycursor.execute(sql, (data1, data2), multi=True)
19
myresult = mycursor.fetchall()
20
print(myresult)
21
Error:
JavaScript
1
9
1
Traceback (most recent call last):
2
File "C:/Users/joshb/PycharmProjects/NBA_V8/grabbing_page_data.py", line 28, in <module>
3
mycursor.execute(sql, (data1, data2), multi=True,)
4
File "C:UsersjoshbPycharmProjectspythonProject2venvlibsite-packagesmysqlconnectorcursor.py", line 539, in execute
5
psub = _ParamSubstitutor(self._process_params(params))
6
File "C:UsersjoshbPycharmProjectspythonProject2venvlibsite-packagesmysqlconnectorcursor.py", line 421, in _process_params
7
raise errors.ProgrammingError(
8
mysql.connector.errors.ProgrammingError: Failed processing format-parameters; Python 'tuple' cannot be converted to a MySQL type
9
I am not sure I still get this error
Advertisement
Answer
Place you parameters in a tuple
, like this:
JavaScript
1
2
1
cursor.execute(sql, (param1, param2))
2
The MySQL Connector cursor.execute method accepts three parameters: the SQL statement, parameters and a flag (multi
). When you execute
JavaScript
1
2
1
cursor.execute(sql, team, team)
2
the final parameter is assumed to be the multi
flag. It’s best practice to always pass parameter values as a tuple*, even if there is only one ((param,)
). You may get other errors otherwise.
* You can use a list
also, but using a tuple
is conventional.