Python Loop for mysql statement

Posted by user552974 on Stack Overflow See other posts from Stack Overflow or by user552974
Published on 2010-12-24T03:56:03Z Indexed on 2010/12/24 4:53 UTC
Read the original article Hit count: 241

Filed under:
|

Hi,

I have a project that i need to compile number of cities in each state and make an insert statement for mysql database. I think the easiest way to do it is via python but since i m a complete noob i would like to reach out all the python gurus here. Here is what the input looks like. Example below is for Florida.

cities = ['Boca Raton', 'Boynton Beach', 'Bradenton', 'Cape Coral', 'Deltona']

and this what the output should be.

INSERT INTO `oc_locations` (`idLocation`, `name`, `idLocationParent`, `friendlyName`) VALUES
(1, 'Florida', 0, 'Florida'),
(2, 'Boca Raton', 1, 'Boca Raton'),
(3, 'Boynton Beach', 1, 'Boynton Beach'),
(4, 'Bradenton', 1, 'Bradenton'),
(5, 'Cape Coral', 1, 'Cape Coral'),
(6, 'Deltona', 1, 'Deltona'),

If you look at carefully the "idLocationParent" for "Florida" value is "0" so which means it is a top level value. This will be done for 50 states so ability to plug the state name into the mysql statement would be icing on the cake if there is a easy way to do it. Also alphabetical order and auto increment for the idLocation would be great.

Here is an example of what i m trying to achieve concatenation is the part i need to figure out.

for city in cities: print (1, 'city', 0, 'city'), city

© Stack Overflow or respective owner

Related posts about python

Related posts about loops