python中使用了MySQLdb ,执行的时候却报错: 

cursor.execute("insert into hq_rank_xueqiu(symbol,name,current) values(%s,%s,%.1f)",('12','123',12.2))
MySQLdbTypeError: float argument required, not str


插入的current字段明明是float类型,却报错提示类型错误,查找官方原文,才知道为题所在:

 

Notice that there is a mix of types (strings, ints, floats) though we still only use %s

原文意思为:即使 字段中有int,float等类型,也仍然要用%s作为占位符


也就是说mysqldb提供的字符串格式化并不是python的字符串格式化,所有的变量类型插入 的时候都必须用%s代替,于是将%.1f替换成%s就可以了

>>>cursor.execute("insertinto hq_rank_xueqiu(symbol,name,current) values(%s,%s,%s)",('12','123',12.3))