找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 1647|回复: 1

[求助] python为什么插入不成功呀

1

主题

1

帖子

1

积分

贫民

积分
1
13820506617 发表于 2018-1-9 17:10:26 | 显示全部楼层 |阅读模式
  1. db = MySQLdb.connect(host = "XXX", user = "XXX", passwd = "XXX", db = "xxx", port = 3306)
  2. db.set_character_set('utf8')
  3. print('连接上了!')
  4. #获取操作游标
  5. cursor = db.cursor()
  6. cursor.execute('SET NAMES utf8;')
  7. cursor.execute('SET CHARACTER SET utf8;')
  8. cursor.execute('SET character_set_connection=utf8;')

  9. for font in fonts:
  10.     td = font.parent
  11.     a = td.a
  12.     href = "http://www.xxx.cn"+a['href']
  13.     title = a['title']
  14.     publishDate = td.span.text
  15.     #     获取文章详情信息
  16.     detailsContent = r.get(href).content
  17.     detailsSoup = BeautifulSoup(detailsContent, "html.parser")
  18.     detailsFont = detailsSoup.find('font', id='zoom').text
  19.     print detailsFont
  20.     createDate = time.strftime('%Y-%m-%d ',time.localtime(time.time()))
  21.     hashCode = str(hash("xxx")+hash(title)+hash(href)+hash(publishDate))
  22.     print type(hashCode)
  23.     insert_color = ("INSERT INTO t_monitoring_dynamic_info (hashcodeUnique) VALUES (%s)")
  24.     data_color = (hashCode)
  25.     cursor.execute(insert_color, data_color)
  26.     db.commit()

  27. cursor.close()
  28. db.close()
复制代码
小弟刚学python,插入总是报错
  1. TypeError: not all arguments converted during string formatting
复制代码
  1. print type(hashCode) 是<type 'str'>

复制代码
数据库中的hashcodeUnique字段也是 varchar类型,不明白为啥总是报这个错误
回复

使用道具 举报

2

主题

219

帖子

219

积分

版主

Rank: 7Rank: 7Rank: 7

积分
219

热心会员默默耕耘优秀版主

剑心无痕 发表于 2018-1-11 11:57:06 | 显示全部楼层
本帖最后由 剑心无痕 于 2018-1-11 12:23 编辑

data_color = (hashCode, ) 加个逗号就好了

data_color = (hashCode) # type(data_color)是str
data_color = (hashCode, ) # type(data_color)是tuple

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表