sql = """
INSERT INTO [SCHOOLINFO]
VALUES(
'""" + self.accountNo + """',
'""" + self.altName + """',
'""" + self.address1 + """',
'""" + self.address2 + """',
'""" + self.city + """',
'""" + self.state + """',
'""" + self.zipCode + """',
'""" + self.phone1 + """',
'""" + self.phone2 + """',
'""" + self.fax + """',
'""" + self.contactName + """',
'""" + self.contactEmail + """',
'""" + self.prize_id + """',
'""" + self.shipping + """',
'""" + self.chairTempPass + """',
'""" + self.studentCount + """'
)
""";
I have the following code and Python keeps throwing the error that it cannon concatenate strings and nonetype objects. The thing is I have verified every variable here is in fact a string and is not null. I have been stuck on this for quite some time today, and any help would be greatly appreciated.
The Python "TypeError: can only concatenate str (not "NoneType") to str" occurs when we try to concatenate a string and a None value. To solve the error, correct the assignment or check if the variable doesn't store a None value before concatenating.
This failure occurs because the concatenate operator (+) for strings in Python doesn't know how to comvert convert numeric types to strings implicitly. There are three straightforward solutions: Convert the integer variable to a string before concatenating. Use a comma in the print() statement to concatenate.
To join multiple strings with possibly None values: Use the join() method with an optional delimiter. Use the filter() function to filter out any None values. The join() method returns a string which is the concatenation of the strings in the iterable.
The Python "TypeError: can only concatenate str (not "list") to str" occurs when we try to concatenate a string and a list. To solve the error, access the list at a specific index to concatenate two strings, or use the append() method to add an item to the list.
Use bind variables instead. Here's the spec for working with DBs in Python: PEP 249: Python Database API Specification v2.0.
UPDATE: Based on the docs for pymssql
, you need something like:
sql = """
INSERT INTO [SCHOOLINFO]
VALUES(
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %d, %s, %s, %d
)"""
cur.execute(sql, self.accountNo, self.altName, self.address1, self.address2, self.city, self.state, self.zipCode, self.phone1, self.phone2, self.fax, self.contactName, self.contactEmail, self.prize_id, self.shipping, self.chairTempPass, self.studentCount)
All these answers so far focus not on your problem but on what is right to do. Yes, yes - bind variables is better and safer. And yes, using % for formatting is faster and likely better.
But on your question what gives you that error - it must be that one of the values is None at some point, there is no other explanation. Just put a debug print in front of that, something like:
for v in 'accountNo altName address1 address2 city state zipCode phone1 phone2 fax contactName contactEmail prize_id shipping chairTempPass studentCount'.split():
if getattr(self, v) is None:
print 'PANIC: %s is None' % v
I bet it will print something at some point ;-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With