解决python mysql插入数据时报错:TypeError: %d format: a number is required, not str,,今天在使用pytho
解决python mysql插入数据时报错:TypeError: %d format: a number is required, not str,,今天在使用pytho
今天在使用python爬取数据并将其写入mysql数据库时,使用该如下语句:
cursor.execute( "insert into comments_p_spider(owner_id,from_name,content,create_time,score,comment_level) values(%d,%s,%s,%s,%f,%s)", (p_id,str(username), str(contentStr), str(create_time),float(score), str(comment_level))) conn.commit()
报错如下:
TypeError: %d format: a number is required, not str
解决方案:Theformat string isnot really a normal Pythonformat string. Youmust alwaysuse%sfor all fields.
也就是MySQLdb的字符串格式化不是标准的python的字符串格式化,应当一直使用%s用于字符串格式化。
所以将代码的sql语句的value格式均改为%s(无论要插入的数据是什么类型),即可正常运行。
cursor.execute( "insert into comments_p_spider(owner_id,from_name,content,create_time,score,comment_level) values(%s,%s,%s,%s,%s,%s)", (p_id,str(username), str(contentStr), str(create_time),float(score), str(comment_level))) conn.commit()
解决python mysql插入数据时报错:TypeError: %d format: a number is required, not str
相关内容
- python3.6.2利用pyinstaller发布EXE,, 我的环境是Ubu
- python中array类型的数据存进数据库,sql语句的写法,,
- python 接口测试字符类型转换,,接口返回数据:pri
- python学习笔记-day7-【python操作数据库】,,上次说到了
- 四.Python高阶函数和装饰器,,目录高阶函数数学概念
- Python科学计算系列6—积分,,1.定积分例1:求下
- python3学习之匿名函数,,在python中,对
- 【Python】创建和使用类,,面向对象编程是最有效
- 萌新向Python数据分析及数据挖掘 第一章 Python基础 第一
- python面向对象,类,,类:抽象的概念;是属
评论关闭