Я знаю, что это популярная тема, но я искал различные ответы и не видел четкого ответа на мою проблему. У меня есть функция, которую я хочу использовать для вставки записей в базу данных NDBC, которая дает мне ошибку, о которой я упоминал в названии. Функция ниже:Python MySQLdb TypeError («не все аргументы, преобразованные во время форматирования строки»)
def insertStdMet(station,cursor,data):
# This function takes in a station id, database cursor and an array of data. At present
# it assumes the data is a pandas dataframe with the datetime value as the index
# It may eventually be modified to be more flexible. With the parameters
# passed in, it goes row by row and builds an INSERT INTO SQL statement
# that assumes each row in the data array represents a new record to be
# added.
fields=list(data.columns) # if our table has been constructed properly, these column names should map to the fields in the data table
# Building the SQL string
strSQL1='REPLACE INTO std_met (station_id,date_time,'
strSQL2='VALUES ('
for f in fields:
strSQL1+=f+','
strSQL2+='%s,'
# trimming the last comma
strSQL1=strSQL1[:-1]
strSQL2=strSQL2[:-1]
strSQL1+=") " + strSQL2 + ")"
# Okay, now we have our SQL string. Now we need to build the list of tuples
# that will be passed along with it to the .executemany() function.
tuplist=[]
for i in range(len(data)):
r=data.iloc[i][:]
datatup=(station,r.name)
for f in r:
datatup+=(f,)
tuplist.append(datatup)
cursor.executemany(strSQL1,tuplist)
Когда мы добираемся до вызова cursor.executemany(), strSQL выглядит следующим образом:
REPLACE INTO std_met (station_id,date_time,WDIR,WSPD,GST,WVHT,DPD,APD,MWD,PRES,ATMP,WTMP,DEWP,VIS) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)'
Я использую знаки% во всем и я передаю список кортежи (~ 2315 кортежей). Каждое переданное значение представляет собой строку, дату и время. Я все еще не нашел проблему. Любые идеи, которые кто-либо хочет пройти, были бы искренне оценены.
Спасибо!
Конечно, это было что-то глупое. Благодаря! Он работает так хорошо! – RyanM