'TypeError: can only concatenate str (not "dict") to str - Cinema
This is my code. and it isn't working. Actually i get this error:
line 43, in cursor.execute(""" TypeError: can only concatenate str (not "dict") to str
When i remove the str add cinema
it gaves the following error: line 43, in cursor.execute(""" TypeError: can only concatenate str ("not typed") to str
Who can hepl me with my code?
# Retrieve data from API
response = requests.get(api_url)
# Print(json.dumps(response.json() , sort_keys=True, indent=4))
processed_response = response.json()
# Establish the connection to the Database
db = pymysql.connect(host='localhost',
user='root',
password='root',
database='bioscoop_meru')
cursor = db.cursor()
# Process every record we receive back from the API
for cinema in processed_response:
# "Bioscoop" dimension
cinema_location = str(cinema["cinema_location"])
cinema_name = str(cinema["cinema"])
print (cinema_location, cinema_name)
# Check whether this record already exists in the dimension we created.
# We do this, because we don't have to insert it twice.
cursor.execute("""
SELECT * FROM dim_cinema
WHERE cinema_name = '"""+cinema+"""'
AND cinema_location = '"""+cinema_location+"""'
""")
resultcount = cursor.rowcount # <-- Here we count the amount of rows returned from the database.
if resultcount == 0:
# We don't have this combination in our dimension. Lets insert it!
sql = """
INSERT INTO dim_cinema (cinema_name, cinema_location)
VALUES ('"""+cinema+"""'), '"""+cinema_location+"""')
"""
try:
# Execute the SQL command and commit our changes to the database...
cursor.execute(sql)
db.commit()
except:
# Unless something goes wrong, in which case we do a rollback on our changes.
db.rollback()
db.close()
###
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
