이 항목은 많이 사용되는 주제이지만 여러 가지 답변을 검색했으며 내 문제에 대한 명확한 답을 찾지 못했습니다. 나는 내가 제목에서 언급 한 오류를 내 NDBC 데이터베이스에 삽입하기 위해 사용하고자하는 기능을 가지고있다. 이 기능은 다음과 같습니다 : 나는 걸쳐 % 기호를 사용하고 있는데 나는 목록을 전달하고Python MySQLdb TypeError ("문자열 서식을 지정할 때 모든 인수가 변환되지 않았습니다")
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)'
을 :
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은 다음과 같습니다 튜플 (~ 2315 개의 튜플). 전달되는 모든 값은 문자열, datetime 또는 숫자입니다. 나는 아직도 문제를 발견하지 못했다. 누군가가 돌볼 수있는 통찰력은 진심으로 감사 할 것입니다.
감사합니다.
물론 바보 같았습니다. 감사! 그것은 지금 너무 잘 작동합니다! – RyanM