【发布时间】:2016-09-16 07:42:24
【问题描述】:
所以我有以下命名元组,包含多个项目:
[item(company='MARINE AND GENERAL MUTUAL LIFE ASSURANCE SOCIETY', google_name='no results', place_id='no results', formatted_address='no results'),
item(company='KENTSTONE PROPERTIES LIMITED', google_name='no results', place_id='no results', formatted_address='no results'),
item(company='ASHFORD CATTLE MARKET COMPANY LIMITED(THE)', google_name=u'The Ashford Cattle Market Co Ltd', place_id=u'ChIJRSxF4gbb3kcRCjmXJcSWOrI', formatted_address=u'The New Ashford Market Monument Way, Orbital Park, Ashford TN24 0HB, United Kingdom'),
item(company='ORIENTAL GAS COMPANY, LIMITED(THE)', google_name=u'Orient Express Hotels', place_id=u'ChIJffRJYVVYwokReF_qwmzMgh0', formatted_address=u'1155 Ave of the Americas, New York, NY 10036, United States'),
item(company='BRITISH INDIA STEAM NAVIGATION COMPANY LIMITED', google_name=u'British-India Steam-Navigation Co Ltd', place_id=u'ChIJe6yzIVN2AjoRZdGKagFvkvs', formatted_address=u'54/7, Bisha Lakshmitala Road, Parnashree, Kolkata, West Bengal 700060, India')]
我想将这些项目添加到 MySQL 数据库中,添加到名为 place_id 的表中,这是我目前得到的:
cursor.execute("INSERT INTO place_id (company, google_name, place_id, formatted_address) VALUES (%(company)s, %(google_name)s, %(place_id), %(formatted_address)s" ....
我不知道从那里去哪里。
我想通过 for 循环(或 executemany,但我不太熟悉)添加项目。我已经通过 MySQLdb 模块连接到数据库了。
感谢您的帮助。
【问题讨论】:
-
有理由坚持使用for循环吗?为什么不
executemany?有关提示,请参阅 stackoverflow.com/questions/10180335/…。 -
刚查了executemany,对它不熟悉,对语法有点困惑。
标签: python mysql loops tuples mysql-python