I have a Python dictionary that I'm trying to insert into mysql. Problem is one of the Keys of the dictionary is longer that 64 characters (the max length of a column in mysql). So I need to truncate all dictionary keys to 64 characters.
The code below works in all respects except for the one key that is longer than 64 characters = location_of_building_on_the_lot_if_garage_change_type_code_to_bgd_
data = {'x_coordinate': '1158020.73068669',
'any_people_using_property_homeless_childen_gangs_': True,
'police_district': '8',
'location_of_building_on_the_lot_if_garage_change_type_code_to_bgd_': 'Front',
'service_request_number': '14-00630589',
'address_street_suffix': 'AVE',
'y_coordinate': '1866585.99638448',
'date_service_request_was_received': '2014-05-01T00:00:00',
'address_street_number': '5719',
'longitude': '-87.69612590561026',
'latitude': '41.78965826126179',
'address_street_name': 'FRANCISCO',
'address_street_direction': 'S',
'location': {'latitude': '41.78965826126179', 'needs_recoding': False, 'longitude': '-87.69612590561026'},
'service_request_type': 'Vacant/Abandoned Building',
'community_area': '63',
'is_the_building_currently_vacant_or_occupied_': 'Vacant',
'ward': '16',
'is_building_open_or_boarded_': 'Open',
'is_the_building_vacant_due_to_fire_': True,
'zip_code': '60629'}
placeholders = ', '.join(['%s'] * len(data))
columns = ', '.join(data.keys())
sql = "INSERT INTO vacant_buildings (%s) VALUES (%s)" % (columns, placeholders)
I tried to change:
columns = ', '.join(data.keys())
to
columns = ', '.join(data[:64].keys())
but get the following error: TypeError: unhashable type
Thoughts?