MySQL 查询传递两个变量 Python SQLConnector

MySQL Query Passing Two Variables Python SQLConnector

我似乎无法在查询中传递两个值,我可以传递一个变量没问题,无法弄清楚我做错了什么,当我尝试使用两个时出现错误可变码,一个可变码通过。

一个变量:

mycursor = mydb.cursor()

mycursor.execute("SELECT `HOME TEAM:` FROM `nbav8` WHERE `HOME TEAM:` is NOT NULL")

myresult = mycursor.fetchall()

awayuyu = []
homeuyu = []
kt = "Atlanta"
for team in myresult:
    mycursor = mydb.cursor()

    mycursor.execute("SELECT `ACTUAL OVER UNDER RESULT:` FROM `previous_losses_nbav1_with_date_agg` WHERE `HOME TEAM:` = %s", team)
    myresult = mycursor.fetchall()
    print(myresult)

print(myresult)

两个变量:

mycursor = mydb.cursor()

mycursor.execute("SELECT `HOME TEAM:` FROM `nbav8` WHERE `HOME TEAM:` is NOT NULL")

myresult = mycursor.fetchall()

awayuyu = []
homeuyu = []
kt = "Atlanta"
for team in myresult:
    mycursor = mydb.cursor()
    mycursor.execute("SELECT `ACTUAL OVER UNDER RESULT:` FROM `previous_losses_nbav1_with_date_agg` WHERE `HOME TEAM:` = %s OR 'AWAY TEAM:' = %s;", team, team)
    myresult = mycursor.fetchall()
    print(myresult)

print(myresult)

错误:

Traceback (most recent call last):
  File "C:/Users/joshb/PycharmProjects/NBA_V8/grabbing_page_data.py", line 26, in <module>
    mycursor.execute("SELECT `ACTUAL OVER UNDER RESULT:` FROM `previous_losses_nbav1_with_date_agg` WHERE `HOME TEAM:` = %s OR 'AWAY TEAM:' = %s;", team, team)
  File "C:\Users\joshb\PycharmProjects\pythonProject2\venv\lib\site-packages\mysql\connector\cursor.py", line 540, in execute
    stmt = RE_PY_PARAM.sub(psub, stmt)
  File "C:\Users\joshb\PycharmProjects\pythonProject2\venv\lib\site-packages\mysql\connector\cursor.py", line 77, in __call__
    raise errors.ProgrammingError(
mysql.connector.errors.ProgrammingError: Not enough parameters for the SQL statement

回答后更新代码:

mycursor = mydb.cursor()

mycursor.execute("SELECT `HOME TEAM:` FROM `nbav8` WHERE `HOME TEAM:` is NOT NULL")

myresult = mycursor.fetchall()
print(myresult)
yeet = 0
awayuyu = []
homeuyu = []
kt = "Atlanta"
temp1 = "Miami"
temp2 = "Toronto"
for team in myresult:
    data1 = team
    data2 = team
    mycursor = mydb.cursor()
    sql = """SELECT `ACTUAL OVER UNDER RESULT:` FROM `previous_losses_nbav1_with_date_agg` WHERE `HOME TEAM:` = %s OR 'AWAY TEAM:' = %s"""
    mycursor.execute(sql, (data1, data2), multi=True)
    myresult = mycursor.fetchall()
    print(myresult)

错误:

Traceback (most recent call last):
  File "C:/Users/joshb/PycharmProjects/NBA_V8/grabbing_page_data.py", line 28, in <module>
    mycursor.execute(sql, (data1, data2), multi=True,)
  File "C:\Users\joshb\PycharmProjects\pythonProject2\venv\lib\site-packages\mysql\connector\cursor.py", line 539, in execute
    psub = _ParamSubstitutor(self._process_params(params))
  File "C:\Users\joshb\PycharmProjects\pythonProject2\venv\lib\site-packages\mysql\connector\cursor.py", line 421, in _process_params
    raise errors.ProgrammingError(
mysql.connector.errors.ProgrammingError: Failed processing format-parameters; Python 'tuple' cannot be converted to a MySQL type

我不确定我是否仍然收到此错误

将参数放在 tuple 中,像这样:

cursor.execute(sql, (param1, param2))

MySQL 连接器 cursor.execute 方法接受三个参数:SQL 语句、参数和标志 (multi)。当你执行

cursor.execute(sql, team, team)

最后一个参数假定为 multi 标志。最好的做法是始终将参数值作为元组*传递,即使只有一个 ((param,))。否则您可能会遇到其他错误。


* 您也可以使用 list,但通常使用 tuple