Python MySQLdb 类型错误("not all arguments converted during string formatting")
Python MySQLdb TypeError("not all arguments converted during string formatting")
我知道这是一个热门话题,但我搜索了各种答案,但没有看到我的问题的明确答案。我有一个函数,我想用它来将记录插入我的 NDBC 数据库,这给了我在标题中提到的错误。函数如下:
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 个元组)。传递的每个值都是字符串、日期时间或数字。我仍然没有发现问题。任何愿意传递的见解都将不胜感激。
谢谢!
您还没有为 SQL 查询提供 station_id
或 date_time
的值,因此当它解压缩您的参数时,缺少两个。
我怀疑您希望最终调用类似于:
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)'
注意额外的两个 %s
。看起来您的元组已经包含 station_id 和 date_time 的值,因此您可以尝试以下更改:
strSQL1='REPLACE INTO std_met (station_id,date_time,'
strSQL2='VALUES (%s, %s, '
我知道这是一个热门话题,但我搜索了各种答案,但没有看到我的问题的明确答案。我有一个函数,我想用它来将记录插入我的 NDBC 数据库,这给了我在标题中提到的错误。函数如下:
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 个元组)。传递的每个值都是字符串、日期时间或数字。我仍然没有发现问题。任何愿意传递的见解都将不胜感激。
谢谢!
您还没有为 SQL 查询提供 station_id
或 date_time
的值,因此当它解压缩您的参数时,缺少两个。
我怀疑您希望最终调用类似于:
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)'
注意额外的两个 %s
。看起来您的元组已经包含 station_id 和 date_time 的值,因此您可以尝试以下更改:
strSQL1='REPLACE INTO std_met (station_id,date_time,'
strSQL2='VALUES (%s, %s, '