-1

im trying parse XMLfile to mysqldb. Here's xml

<categories>
<category id="28">Woman</category>
<category id="277" parentId="28">T-Shirts</category>
<category id="140" parentId="277">shorts</category>
</category>

.py

for category in categories:
        for item in category.getElementsByTagName("category"):
            category_name = item.childNodes[0].nodeValue.encode("utf-8")
            category_id = int(item.getAttribute('id'))
            category_parentId = item.getAttribute('parentId')
#connect etc
sqlFillCategories = "INSERT INTO categories(category_id, category_parentId, shop_id, category_name) VALUES ('"+category_id + "', '" + category_parentId + "', '" + TREND_BRANDS_SHOPID + "', '" + category_name + "');"

Error:

Traceback (most recent call last):
  File "E:/python1/fanswell/parse.py", line 23, in <module>
    sqlFillCategories = "INSERT INTO categories(category_id, category_parentId, shop_id, category_name) VALUES ('"+category_id + "', '" + category_parentId + "', '" + TREND_BRANDS_SHOPID + "', '" + category_name + "');"

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

Why is that so? What's wrong?

Spaceghost
  • 6,835
  • 3
  • 28
  • 42
Ilnar Karimov
  • 339
  • 2
  • 6
  • 19
  • 3
    Using parameterized queries would solve this completely *and* avoid getting into the bad habit of building SQL by concatenating strings, which will get you hacked one day. – Wooble Oct 09 '13 at 23:07
  • duplicate of [Python: TypeError: cannot concatenate 'str' and 'int' objects](http://stackoverflow.com/questions/11844072/python-typeerror-cannot-concatenate-str-and-int-objects) ? – David Cary Jan 31 '14 at 23:21

2 Answers2

5

int and str are different types.

To concatenate an int to a str,

You need to do a int to str conversion. That is, eg:

"Hello World " + str(1)

So you probably want:

 sqlFillCategories = "INSERT INTO categories(category_id, category_parentId,
 shop_id, category_name) VALUES ('"+str(category_id) + "', '" +
 str(category_parentId) + "', '" + str(TREND_BRANDS_SHOPID) + "', '" 
 + category_name + "');"

EDIT: Your insert statement is outside the loop, try this:

sqlFillCategories =''
for category in categories:
        for item in category.getElementsByTagName("category"):
            category_name = item.childNodes[0].nodeValue.encode("utf-8")
            category_id = int(item.getAttribute('id'))
            category_parentId = item.getAttribute('parentId') 
            sqlFillCategories += 
            "INSERT INTO categories(category_id, category_parentId, shop_id, 
            category_name) VALUES ('"+category_id + "', '" + 
            category_parentId + "','" + TREND_BRANDS_SHOPID + "',
             '" + category_name + "');"

When sqlFillCategories it will execute a bunch of inserts.

Lews Therin
  • 10,907
  • 4
  • 48
  • 72
0

Your final comment seems to be asking about adding only ONE record to your db.... You'll need to build a longer string within the loop to

VALUES ('category', 'catid', 'catParentId', store, 'category'), 
('category', 'catid', 'catParentId', store, 'category'), 
('category', 'catid', 'catParentId', store, 'category')



allValues = []
for category in categories:
    for item in category.getElementsByTagName("category"):
        category_name = item.childNodes[0].nodeValue.encode("utf-8")
        category_id = int(item.getAttribute('id'))
        category_parentId = item.getAttribute('parentId')
        myValue = "('"+str(category_id) + "', '" + str(category_parentId) + "', 
        '" + str(TREND_BRANDS_SHOPID) + "', '" + category + "')"
        allValues.append(myValue)

comma_separated = ', '.join(allValues)
sqlFillCategories = "INSERT INTO categories(category_id, category_parentId,
 shop_id,
 category_name) VALUES " + comma_separated + ";"
Grizz
  • 320
  • 1
  • 11