Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to solve TypeError: cannot concatenate 'str' and 'int' objects?

By
Anirudh Lou
on 8/18/15, 6:01 AM 2,336 views

I am trying to retrieve a list in my database. My statement is this:

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

where date_from and date_to are of type fields.date but when when executed, that is i am trying to access my view, it says:

TypeError: cannot concatenate 'str' and 'int' objects

What did i miss with my statement?

1

Emipro Technologies Pvt. Ltd.

--Emipro Technologies Pvt. Ltd.--
Tiny ERP Pvt Ltd
http://www.odoo.com
5975
| 6 7 8
Rajkot, India
--Emipro Technologies Pvt. Ltd.--

Being an emerging leader in IT market since 2011, Emipro Technologies Pvt. Ltd. has been providing a wide range of business solutions in Odoo & Magento. We are pleased to have a large pool of contented customers with our meticulous work in the domain of ERP & e-Commerce. A ray of relief & satisfaction to  our customers heart by our successful deployment in their organization, purely represents our success in Odoo platform. Hence, we take pride for being an Odoo partner with a vision of expanding our strategic alliances with our customers to offer them high value-added, trustworthy & cost effective solutions.

 

Since establishment, our business has span across 11 countries of the world. Our customers are companies of all sizes ranging from start-ups to large enterprises, who realize that they need a professional business solutions to generate revenue streams, establish proper communication channels or streamline business operations. Standing with strong determination of customer satisfaction, observing each minute detail of their business processes, providing proper guidance and moving forth to develop product accordingly adds our value and reliability among our customers. 

 

We are blessed with efficient, passionate & eclectic young developers who have come across almost all kinds of business profiles, working with full dedication, applying creativity & new features in existing modules and completing customer's projects successfully on predefined target. Our in-depth knowledge while giving business solutions in Odoo allows us to offer following services to our customers :

 

  • Consulting
  • Installation, Configuration & Customization
  • Training & Support
  • Maintenance
  • Upgradation
  • Documentation
  • Crafting community modules
  • Drafting videos on demand
  • Smart Apps development

We will be :) to welcome you @ www.emiprotechnologies.com to solve any kind of your business needs around Odoo. However we are just an inch away from you by email info@emiprotechnologies.com or a tweet to @EmiproTech

Emipro Technologies Pvt. Ltd.
On 8/18/15, 8:13 AM

Hello,

As your fields are fields.date then you do not need to convert into string. Please write your query as like below.

cr.execute("select es.* from student_resource es where es.date between '" + date_from + "' and '" + date_to + "' ")
or
cr.execute("select es.* from student_resource es where es.date between '%s' and '%s' " %(date_from, date_to) )
Update : or
query = "select es.* from student_resource es where es.date between '%s' and '%s' " %(date_from, date_to)
cr.execute(query)

When you retrieve any date/datetime value from data base into any python variable it is always in string s

o no need to convert into string. 

I hope it will resole your issue.

Thanks. 

I've tried your suggestion sir but I get "ValueError: unsupported format character 'W' (0x57) at index 545" when i try "...between ? and ? ",(date_from, date_to) )" i get "IndexError: tuple index out of range"

Anirudh Lou
on 8/19/15, 8:34 PM

Just try as I have updated my answer. I have test it at my side it is executed perfectly.

Emipro Technologies Pvt. Ltd.
on 8/20/15, 6:25 AM
0
Marvin Taboada
On 8/18/15, 9:19 PM

Quoting Psycopg official references (see here) :

Psycopg casts Python variables to SQL literals by type. Many standard Python types are already adapted to the correct SQL representation.

Don't manually add single quotes depending of the data type, let Psycopg do that for you.

cr.execute( 
"""INSERT INTO some_table (an_int, a_date, a_string) VALUES (%s, %s, %s);""", (11, datetime.date(2007, 07, 27), "Odoo rocks!"))

0

Akhil P Sivan

--Akhil P Sivan--
3423
| 5 3 6
Kochi, India
--Akhil P Sivan--

Interested in exploring Odoo both technically & functionality wise.

email-id : akhilpsivan01@gmail.com

Akhil P Sivan
On 8/18/15, 7:29 AM

I think you can use the to_date() in psql for that.

Try like this:

cr.execute("select es.* from student_resource es where es.date between to_date(date_from, 'YYYY/MM/DD') and to_date(date_to, 'YYYY/MM/DD')")  
OR
cr.execute("select es.* from student_resource es where es.date between to_date("+str(date_from)+", 'YYYY/MM/DD') and to_date("+str(date_to)+", 'YYYY/MM/DD') ")

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 8/18/15, 6:01 AM
Seen: 2336 times
Last updated: 8/28/15, 1:37 AM