![](/img/trans.png)
[英]Not all parameters were used in the SQL statement (Python, MySQL)
[英]Python: Not all parameters were used in the SQL statement using jupyter and mysql
我正在嘗試制作一個 function 以在 jupyter 中傳遞參數並返回查詢,但它返回以下錯誤“SQL 語句中未使用所有參數”。
現在我使用以下方法建立連接:
import mysql.connector as connection
import pandas as pd
db = connection.connect (parameters of connection)
我的 function 必須傳遞 3 個參數並根據這些參數返回 dataframe 及其查詢,所以我在做:
def param (id, id_user, date):
cursor = db.cursor()
if (id == '' or None) and (id_user == '' or None) and (date == '' or None):
query = 'select * from database'
df = pd.read_sql(query, db)
elif (id_user == '' or None) and (date == '' or None):
query = 'select * from database where id = %s'
df = pd.read_sql(query, db, params = id)
else:
print(':(')
return df
然后當我執行時出現錯誤:
param (18, '', '')
我究竟做錯了什么?
嘗試傳遞一個元組作為params
的值:
query = 'select * from database where id = %s'
df = pd.read_sql(query, db, params = (id,)) # change is here
在您當前的代碼中,您直接傳遞了一個變量id
,但很可能它需要一個元組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.