I've read other posts in this form, but still do not know why I get this error:

File "/home/amr/bin/mysql_lib.py", line 196, in createNewCsRowSQL
    db_date + ");"
TypeError: cannot concatenate 'str' and 'long' objects

from this code

sql_cmd = \
            "INSERT INTO customer_synch " + \
            "VALUES ( " + \
            "'A', " + \
            incoming_data[0] + "," + \
            incoming_data[1] + "," + \
            incoming_data[2] + "," + \
            incoming_data[3] + "," + \
            incoming_data[4] + "," + \
            incoming_data[4] + "," + \
            incoming_data[4] + "," + \
            lnam_fnam_l[0]   + "," + \
            lnam_fnam_l[1]   + "," + \
            incoming_data[11] + "," + \
            incoming_data[5] + "," + \
            incoming_data[10] + "," + \
            incoming_data[12] + "," + \
            incoming_data[13] + "," + \
            incoming_data[14] + "," + \
            incoming_data[15] + "," + \
            incoming_data[16] + "," + \
            incoming_data[17] + "," + \
            ","                     + \
            ","                     + \
            str(0)            + "," + \
            str(incoming_data[4])  + "," + \
            db_date + ");"

I've tried putting db_date inside a str() call, and also have tried adding '" + to the string. db_date is a date derived from

def getMySQLDateTime():
    now = datetime.today()
    db_date = now.strftime("%Y-%m-%d %H:%M:%S")
    return db_date

The rest of incoming_data is a mix of different types, including strings, integers, and decimals.

I would love some ideas on how to fix this.

Recommended Answers

All 5 Replies

This shows that your data inside the incoming_data list is of type long. You can just 'add' (+) strings and longs together. You can do one of two things.
You can do something like this using the comma

>>>print "Hello this uses a comma to add an int to the end",15
Hello this uses a comma to add an int to the end 15

Or you can use the str() method to change the int/long to a string and the add it together using the + sign.

>>> print "This uses the str method: "+str(15)
This uses the str method:15

I would reccomend either changing your code to

sql_cmd = \
        "INSERT INTO customer_synch " + \
        "VALUES ( " + \
        "'A', " , \
        incoming_data[0] , "," , \
        incoming_data[1] , "," , \
        incoming_data[2] , "," , 
        #you get the point by now

or

sql_cmd = \
        "INSERT INTO customer_synch " + \
        "VALUES ( " + \
        "'A', " + \
       str( incoming_data[0]) + "," + \
       str (incoming_data[1]) + "," + \
        str(incoming_data[2]) + "," + \

I hope that helps you out :)

BTW: one thousand posts!!!! YAY :)

commented: congrats for the 1000 posts +3

Thanks for replying. I'll let you know how it goes, when I start back on it.

The way you derive db_date, it clearly is a string. Your error comes from before that item. After all, sql_cmd = ... is just one line of code. Simply put all incoming_data[x] into str(incoming_data[x]) that should fix it.

You can also make a function

def gen_args(incoming_data, lnam_fnam, db_date):
    yield "'A'"
    for i in list(range(5)) + [4,4]:
        yield incoming_data[i]
    for i in range(2):
        yield lnam_fnam[i]
    for i in [11, 5, 10] + list(range(12, 18)):
        yield incoming_data[i]
    for i in range(2):
        yield ""
    yield 0
    yield incoming_data[4]
    yield db_date

def make_request(incoming_data, lnam_fnam, db_date):
    args = ",".join(str(x) for x in gen_args(incoming_data, lnam_fnam, db_date))
    request = "INSERT INTO customer_synch VALUES (%s);" % args
    return request

This shows that your data inside the incoming_data list is of type long. You can just 'add' (+) strings and longs together. You can do one of two things.
You can do something like this using the comma

>>>print "Hello this uses a comma to add an int to the end",15
Hello this uses a comma to add an int to the end 15

Or you can use the str() method to change the int/long to a string and the add it together using the + sign.

>>> print "This uses the str method: "+str(15)
This uses the str method:15

I would reccomend either changing your code to

sql_cmd = \
        "INSERT INTO customer_synch " + \
        "VALUES ( " + \
        "'A', " , \
        incoming_data[0] , "," , \
        incoming_data[1] , "," , \
        incoming_data[2] , "," , 
        #you get the point by now

or

sql_cmd = \
        "INSERT INTO customer_synch " + \
        "VALUES ( " + \
        "'A', " + \
       str( incoming_data[0]) + "," + \
       str (incoming_data[1]) + "," + \
        str(incoming_data[2]) + "," + \

I hope that helps you out :)

BTW: one thousand posts!!!! YAY :)

You can also make a function

def gen_args(incoming_data, lnam_fnam, db_date):
    yield "'A'"
    for i in list(range(5)) + [4,4]:
        yield incoming_data[i]
    for i in range(2):
        yield lnam_fnam[i]
    for i in [11, 5, 10] + list(range(12, 18)):
        yield incoming_data[i]
    for i in range(2):
        yield ""
    yield 0
    yield incoming_data[4]
    yield db_date

def make_request(incoming_data, lnam_fnam, db_date):
    args = ",".join(str(x) for x in gen_args(incoming_data, lnam_fnam, db_date))
    request = "INSERT INTO customer_synch VALUES (%s);" % args
    return request

end quote.

Wound up wrapping str() function around those fields that were numeric. Thanks for all the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.