2016-06-20 6 views
0

私はこれが人気のあるトピックだと知っていますが、私は様々な回答を検索しています。私は、私がタイトルで述べたエラーを私に与える私の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タプル)。渡されるすべての値は、文字列、日時または数値です。私はまだ問題を発見していない。誰かが抱く洞察は、心から感謝します。

ありがとうございます!

答えて

0

SQLクエリーにstation_idまたはdate_timeのいずれかの値を指定していないため、引数を解凍するときに2つが欠落しています。

私はあなたが最後のコールのようなものになりたい疑う:

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,%s,%s)' 

余分な2 %sに注意してください。あなたのタプルにはすでにstation_idとdate_timeの値が含まれているようですので、この変更を試すことができます:

strSQL1='REPLACE INTO std_met (station_id,date_time,' 
strSQL2='VALUES (%s, %s, ' 
+0

もちろん、それは愚かなものでした。ありがとう!それは今とてもうまくいく! – RyanM

関連する問題