TypeError: not all arguments converted during string formatting , during uploading data to web sql server,

TypeError: not all arguments converted during string formatting , during uploading data to web sql server,

import pymysql as psl
con6s = psl.connect(host='myhost',database='mydatabase',user='usr',passwd='12345')
c6s = con6s.cursor()
c6s.execute( "INSERT INTO %s (stmp,dat,tm,tc,mc,sd,rp,st,stf,stt) VALUES(?,?,?,?,?,?,?,?,?,?)" % (str(db)), (stmp,dat,tm,tc,mc,sd,rp,st,stf,stt))
con6s.commit()
con6s.close()

File "C:/Users/Bwrl/Desktop/al/Time_wither_server_upload/testdb.py", line 134, in cld_sd (str(db)), (stmp,dat,tm,tc,mc,sd,rp,st,stf,stt))

File "C:\Users\Bwrl\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pymysql\cursors.py", line 164, in execute query = self.mogrify(query, args)

File "C:\Users\Bwrl\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pymysql\cursors.py", line 143, in mogrify query = query % self._escape_args(args, conn)

TypeError: not all arguments converted during string formatting

我反复检查了所有这些变量,所有变量的类型都没有问题。

您的 execute 通话有问题。

您使用 %string formatexecute args 混合在一起。

您需要将 % 替换为 , 并使用 string.format.

string format 移出
 sql_cmd = """INSERT INTO 
               {} (stmp,dat,tm,tc,mc,sd,rp,st,stf,stt) 
                   VALUES(?,?,?,?,?,?,?,?,?,?)""".format(table_name)

 c6s.execute(sql_cmd, (stmp,dat,tm,tc,mc,sd,rp,st,stf,stt))

cursor.execute

的原型
 cursor.execute(sql, (args,))

这是official docs

在那个 .execute 调用中,您应该只将 db 传递给 % 字符串插值运算符,但 Python 认为您也在尝试对其他变量进行插值,因此您需要更改括号:

c6s.execute(("INSERT INTO %s (stmp,dat,tm,tc,mc,sd,rp,st,stf,stt) VALUES(?,?,?,?,?,?,?,?,?,?)" % str(db)), (stmp,dat,tm,tc,mc,sd,rp,st,stf,stt))

而如果db是一个可以简化为

的字符串
c6s.execute(("INSERT INTO %s (stmp,dat,tm,tc,mc,sd,rp,st,stf,stt) VALUES(?,?,?,?,?,?,?,?,?,?)" % db), (stmp,dat,tm,tc,mc,sd,rp,st,stf,stt))

尽管我倾向于将其拆分以使其更易于阅读:

cmd = ("INSERT INTO %s (stmp,dat,tm,tc,mc,sd,rp,st,stf,stt) "
    "VALUES(?,?,?,?,?,?,?,?,?,?)" % db)
c6s.execute(cmd, (stmp,dat,tm,tc,mc,sd,rp,st,stf,stt))