sqlalchemy.exc.ProgrammingError:(mysql.connector.errors.ProgrammingError)并非在SQL语句中使用了所有参数

时间:2019-06-18 12:50:24

标签: python mysql pandas sqlalchemy

我正在尝试使用元组通过数据帧更新mysql,但是我被卡住了,我不知道问题出在mydatabase还是我的代码

这是我使用的全部代码

from sqlalchemy import create_engine
import pandas as pd
import os
import csv
import MySQLdb
from sqlalchemy import types, create_engine


# MySQL Connection
MYSQL_USER      = 'root'
MYSQL_PASSWORD  = 'xxxxxxxxxx'
MYSQL_HOST_IP   = '127.0.0.1'
MYSQL_PORT      = 3306
MYSQL_DATABASE  = 'mydb'

engine = create_engine
('mysql+mysqlconnector://'+MYSQL_USER+':'+MYSQL_PASSWORD+'@'+MYSQ 
L_HOST_IP+':'+str(MYSQL_PORT)+'/'+MYSQL_DATABASE, echo=False)

mydir = (os.getcwd()).replace('\\', '/') + '/'
all_data = pd.read_sql('SELECT * FROM govtracker', engine)
# .drop(['#'], axis=1)
myOutlook_inBox = pd.read_csv(r'' + mydir + 'test.CSV', usecols=['Subject', 
'Body', 'From: (Name)', 'To: (Name)'],
                          encoding='latin-1')
myOutlook_inBox.columns = myOutlook_inBox.columns.str.replace(' ', '')

#this object extract 5 chars and 5 numbers from specific column in csv
replaced_sbj_value = myOutlook_inBox['Subject']
.str.extract(pat='(L(?:DEL|CAI|SIN).\d{5})').dropna()

#this columns I want to filter in database
myOutlook_inBox["Subject"] = replaced_sbj_value
# this conditions filters and get and dublicate repeated data from outlook 
exported file
# Condition 1: any mail from mowafy to te
frm_mwfy_to_te = myOutlook_inBox.loc[myOutlook_inBox['From: 
(Name)'].str.contains("mowafy", na=False)
                                 & myOutlook_inBox['To: 
(Name)'].str.contains("te", na=False)].drop_duplicates(
keep=False)


#Condition 2: any mail from pp to mowafy
frm_pp_to_mwfy = \
myOutlook_inBox.loc[myOutlook_inBox['From:(Name)'].str.contains("Amr Mohamed 
Abuella barakat", na=False)] \
| myOutlook_inBox.loc[myOutlook_inBox['From:(Name)'].str.contains("Reham", 
na=False)] \
| myOutlook_inBox.loc[myOutlook_inBox['From:(Name)'].str.contains("Hany", 
na=False)] \
| myOutlook_inBox.loc[myOutlook_inBox['From:(Name)'].str.contains("Ossama 
Monir", na=False)] \
& myOutlook_inBox.loc[myOutlook_inBox['To:(Name)'].str.contains("mowafy", 
na=False)].drop_duplicates(keep=False)

#Condition 3: any mail from TE to mowafy
frm_te_to_mwfy = \
myOutlook_inBox.loc[myOutlook_inBox['From:(Name)'].str.contains("te", 
na=False)] & \
myOutlook_inBox.loc[myOutlook_inBox['To:(Name)'].str.contains("mowafy", 
na=False)].drop_duplicates(keep=False)

frm_mwfy_to_te.Subject

filtered_data = all_data
.loc[all_data.site_code.str.contains('|'.join(frm_mwfy_to_te.Subject))]


print(myOutlook_inBox)

all_data.replace('\n', '', regex=True)
df = all_data.where((pd.notnull(all_data)), None)
print(df)

print("Success")

print(frm_mwfy_to_te.Subject)
print(filtered_data.site_code)

values = tuple(filtered_data[['site_code']].values)

query = """
UPDATE govtracker
SET pending = 'TE'  
WHERE site_code in %s)
"""
connection = engine.connect()
update_db_query = connection.execute(query, values)

这是我的回溯

Traceback (most recent call last):
File "C:\Users\DELL\PycharmProjects\MyALLRefProf\venv\lib\site- 
packages\sqlalchemy\engine\base.py", line 1244, in _execute_context
cursor, statement, parameters, context
File "C:\Users\DELL\PycharmProjects\MyALLRefProf\venv\lib\site- 
packages\sqlalchemy\engine\default.py", line 550, in do_execute
cursor.execute(statement, parameters)
File "C:\Users\DELL\PycharmProjects\MyALLRefProf\venv\lib\site- 
packages\mysql\connector\cursor.py", line 543, in execute
"Not all parameters were used in the SQL statement")
mysql.connector.errors.ProgrammingError: Not all parameters were used in 
the SQL statement

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "C:/Users/DELL/PycharmProjects/MyALLRefProf/MyAutomationFuncs.py", 
line 92, in <module>
update_db_query = connection.execute(query, values)
File "C:\Users\DELL\PycharmProjects\MyALLRefProf\venv\lib\site- 
packages\sqlalchemy\engine\base.py", line 982, in execute
return self._execute_text(object_, multiparams, params)
File "C:\Users\DELL\PycharmProjects\MyALLRefProf\venv\lib\site- 
packages\sqlalchemy\engine\base.py", line 1155, in _execute_text
parameters,
File "C:\Users\DELL\PycharmProjects\MyALLRefProf\venv\lib\site- 
packages\sqlalchemy\engine\base.py", line 1248, in _execute_context
e, statement, parameters, cursor, context
File "C:\Users\DELL\PycharmProjects\MyALLRefProf\venv\lib\site- 
packages\sqlalchemy\engine\base.py", line 1466, in _handle_dbapi_exception
util.raise_from_cause(sqlalchemy_exception, exc_info)
File "C:\Users\DELL\PycharmProjects\MyALLRefProf\venv\lib\site- 
packages\sqlalchemy\util\compat.py", line 383, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "C:\Users\DELL\PycharmProjects\MyALLRefProf\venv\lib\site- 
packages\sqlalchemy\util\compat.py", line 128, in reraise
raise value.with_traceback(tb)
File "C:\Users\DELL\PycharmProjects\MyALLRefProf\venv\lib\site- 
packages\sqlalchemy\engine\base.py", line 1244, in _execute_context
cursor, statement, parameters, context
File "C:\Users\DELL\PycharmProjects\MyALLRefProf\venv\lib\site- 
packages\sqlalchemy\engine\default.py", line 550, in do_execute
cursor.execute(statement, parameters)
File "C:\Users\DELL\PycharmProjects\MyALLRefProf\venv\lib\site- 
packages\mysql\connector\cursor.py", line 543, in execute
"Not all parameters were used in the SQL statement")
sqlalchemy.exc.ProgrammingError: (mysql.connector.errors.ProgrammingError) 
Not all parameters were used in the SQL statement
[SQL: 
UPDATE govtracker
SET pending = 'TE'  
WHERE site_code in %s)
]
[parameters: ('LCAIE30089', 'LSINI16321')]
(Background on this error at: http://sqlalche.me/e/f405)

现在,我将简要说明我的代码和情况

因此,在创建连接并读取熊猫后,我开始创建条件,此条件正在过滤并从pandas数据框中提取了一些值,因为结果是列数据框包含一些值,所以从现在开始,我将使用此输出作为过滤条件通过使用Join在我的datbase中,所以现在我想根据通过使用元组过滤的值来更新come列,并创建一个简单的查询,但是我被卡住了,我发现了此错误,所以如果有人可以帮助我解决这种情况,我谢谢他/她

希望每一件事都足够清楚

0 个答案:

没有答案