我想插入一個像這樣的列表,但要大得多。 list=[{"name":"Tom","gender":"male",},{"name":"Jack","gender":"male",},{"name":"Lee","gender":"male",}]
進入MySQL使用python來構建表。我已經導入MySQLdb了,那我該怎麼做?在Python中使用Python插入字典的列表
回答
假設你有一個與name
和gender
列命名爲foo
表,這裏是你如何使用executemany()
插入字典的這個名單到表:
import MySQLdb
db = MySQLdb.connect(...)
cursor = db.cursor()
data = [
{"name":"Tom", "gender":"male"},
{"name":"Jack", "gender":"male"},
{"name":"Lee", "gender":"male"}
]
cursor.executemany("""
INSERT INTO foo (name, gender)
VALUES (%(name)s, %(gender)s)""", data)
db.commit()
謝謝,它很有用,但是當我將它用於我自己的數據時,它不起作用。 – GUOJINGWEI
我自己就像:data = [dict1,dict2,dict3] dict1 = {「name」:「Tom」,「source」:u'240907112_1333816867','Title':u'\ u300a \ u8ba4 \ u771f \ u7684 \ u300b',「_ id」:u'47092'}。在MySQL中,我已經建立了一個表並使用_id作爲主鍵。我認爲這可能是因爲你有值,但我不知道該怎麼做。 – GUOJINGWEI
@GUOJINGWEI請看看是否有意義創建一個單獨的問題,試圖將建議的解決方案應用於您的實際數據。謝謝。 – alecxe
- 1. 使用Python字典/列表
- 2. 將列表中的字典插入python中的數據庫中
- 3. python字典 - 列表字典
- 4. 使用python插入詞典列表到SQL表中
- 5. Python - 列入字典
- 6. python中的字典列表
- 7. Python子字典/字典中的列表
- 8. 如何在Python字典中插入一個列表作爲鍵
- 9. Python:根據匹配插入到字典中的列表中
- 10. python字典mysql不插入
- 11. python字典插入到mysql
- 12. Python字典,動態插入
- 13. Python:插入新元素到排序的字典列表中
- 14. 將字典插入到wx Python列表中Ctrl
- 15. 在Python訪問字典中的列表
- 16. 在python中搜索字典的列表
- 17. Python字典的字典列表
- 18. Python字典裏面的字典列表
- 19. 從Python中的字典的現有列表字典的列表
- 20. Python - 使用列表創建字典
- 21. python字典使用defaultdict(列表)
- 22. 轉換列表以字典使用python
- 23. 使用字典在Python中使用列寫入數據
- 24. Python列表插入
- 25. 如何在列表中使用python插入數字C++樣式
- 26. 在Python中導入字典
- 27. Python列表到Python中的字典,與選定的列表
- 28. 插入詞典在Python
- 29. Psycopg2 Python中插入列表
- 30. Python:列表的列表的字典
你真的不得不縮小這個下。這是從結構到具有匹配列名稱的表嗎? – tadman