【问题标题】:How to solve TypeError: cannot concatenate 'str' and 'int' objects?如何解决 TypeError:无法连接 'str' 和 'int' 对象?
【发布时间】:2015-08-18 09:51:10
【问题描述】:

我正在尝试在我的数据库中检索一个列表。我的声明是这样的:

cr.execute("select es.* from student_resource es where es.date between '"+str(date_from)+"' and '"+str(date_to)+"'")

date_fromdate_to 属于 fields.date 类型,但在执行时,即我试图访问我的视图,它说:

TypeError: 无法连接 'str' 和 'int' 对象

我的陈述遗漏了什么?

日志:

File "/opt/openerp/server-7/openerp/osv/osv.py", line 187, in execute_cr
    return getattr(object, method)(cr, uid, *args, **kw)
  File "/opt/openerp/custom_addons/sample_new/student_resource.py", line 207, in generate_ewt_line
    from student_resource es where es.date between '"+str(date_from.strftime('%Y-%m-%d'))+"' and '"+str(date_to.strftime('%Y-%m-%d'))+"'\
TypeError: cannot concatenate 'str' and 'int' objects

这就是我得到的。

编辑

这实际上是我正在做的:

_logger.info("\n\t\t\tI was called. My method name is %s"%(str('generate_student_resource_report')))
for ewt in self.browse(cr,uid,ids,context):
    old_history = resource_line_obj.search(cr, uid, [('parent_id','=',ewt.id)],context=context)
    if old_history:
       ewt_line_obj.unlink(cr, uid, old_history,context=context)   
    cr.execute("insert into resource_line(partner_id,seq,base_amount,tax_amount,percent,atc_code,\
                    nature,parent_id,create_date,write_date,create_uid, write_uid)\
                select es.part_id as partner_id,\
                    (case when es.name like '%WC158%' then 1\
                        when es.name like '%WC160%' then 2\
                        when es.name like '%WC010%' then 3\
                        when es.name like '%WC140%' then 4\
                        else 0 end) as seq,\
                    sum(es.base_amt) as base_amount,\
                    sum(es.tax_amt) as tax_amount,\
                    (case when es.name like '%EWT 1\%%' then '1.00'\
                        when es.name like '%EWT 2\%%' then '2.00'\
                        when es.name like '%EWT 3\%%' then '3.00'\
                        when es.name like '%EWT 4\%%' then '4.00'\
                        when es.name like '%EWT 5\%%' then '5.00'\
                        when es.name like '%EWT 6\%%' then '6.00'\
                        when es.name like '%EWT 7\%%' then '7.00'\
                        when es.name like '%EWT 8\%%' then '8.00'\
                        when es.name like '%EWT 9\%%' then '9.00'\
                        when es.name like '%EWT 10\%%' then '10.00'\
                        else null end) as percent,\
                    (case when es.name like '%WC158%' then 'WC158'\
                        when es.name like '%WC160%' then 'WC160'\
                        when es.name like '%WC010%' then 'WC010'\
                        when es.name like '%WC140%' then 'WC140'\
                        else null end) as atc_code,\
                    (case when es.name like '%WC158%' then 'NOTEBOOK'\
                        when es.name like '%WC160%' then 'BACKPACK'\
                        when es.name like '%WC010%' then 'COLOR'\
                        when es.name like '%WC140%' then 'BOOKS' else null end) as nature,\
                    (now()) as create_date,(now()) as write_date,\
                     ? as parent_id,? as create_uid,? as write_uid\
                from student_source es where es.date between ? and ? \
                group by es.partner_id,es.name",(ewt.id,uid,uid,str(date_from),str(date_to)))
    line_list = cr.fetchall()
    _logger.info("\n\t\t\tSource ... %s"%(str(line_list)))   
    list = self.pool.get('student.resource.line').search(cr,uid,[('parent_id','=',ewt.id)])
    lines = [line.id if line.id else False for line in self.pool.get('student.resource.line').browse(cr,uid,list,context=context)]
value = {
         "value" : {
                    'name' : 'Student Resources Report',
                    "ewt_line" : lines
                    }
         }
return value

【问题讨论】:

  • 您似乎正在通过连接构建 SQL 查询,这是一种非常糟糕的形式 - 尝试阅读您正在使用的工具的文档以了解如何正确执行此操作。
  • 当我在 postgres 上使用它时它工作正常我不知道为什么它在 eclipse 上使用时会产生错误。
  • @AnirudhLou 你确定错误就在这一行吗?愿意分享完整的踪迹吗?此外,上面评论中关于串联的说法确实值得关注。
  • 它以前工作过的事实无关紧要;这不是执行此操作的适当方法。我建议您首先查看 Python 的数据库 API(参见例如 python.org/dev/peps/pep-0249/#id15) - 您应该将参数分别提供给实际查询并让数据库引擎适当地插入它们。这有助于保护您免受例如SQL注入攻击,并使您的代码更易于阅读。这可能无法解决您的问题,但希望可以更轻松地提供minimal reproducible example,而不会出现其他问题。
  • 在这种情况下你应该使用ORM methods而不是字符串连接

标签: python python-2.7 date openerp-7


【解决方案1】:

你应该ORM methods。试试这样的:

student_resource_obj = self.pool.get('student.resource')
lines = student_resource_obj.search(cr, uid,
    [('date', '>', date_from), 
    ('date', '<', date_to)]
)
for record in lines:
    # [...]

【讨论】:

  • 对不起,我的 ORM-remark 是错误的,没有注意到 OpenERP 参考。除非您以某种方式编辑您的答案(例如,通过在第一句中添加动词 :)),否则我无法取消反对票。
  • 我更新了我的身体问题。实际上,我也问过与这个问题相关的问题stackoverflow.com/questions/32065162/…,但在那个代码中我使用了 python,我无法得到总和/总计。
  • 顺便说一句,我更新了我的问题以使其适应 OpenERP v7。 date 字段的类型是fields.date() 对吧?
  • 是的。但正如我所说,我无法得到这些字段的总和,这就是我使用 SQL 语句尝试它的原因。
【解决方案2】:

从查询中删除 ? 符号,改为使用 %s 并最后删除 , 并将其替换为 %(ewt.date_from ,ewt.date_to))

简而言之,查询看起来像,

qry = "select * from table where field1 = %s and field2=%s" %(value1, value2)
cr.execute(qry)

你的日期转换看起来像,

cr.execute("select es.* from student_resource es where es.date between '"+ date_from.strftime("%Y-%m-%d") +"' and '"+ date_to.strftime("%Y-%m-%d") +"'")

【讨论】:

    猜你喜欢
    • 2011-10-23
    • 1970-01-01
    • 2015-09-01
    • 2020-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-01
    • 1970-01-01
    相关资源
    最近更新 更多