python 3.5 分析后的字典數(shù)據(jù)怎么存入數(shù)據(jù)庫
問題描述
字典是keywords_dict{}{’茉莉花茶泡多久’: 10, ’苦丁長什么樣子圖片’: 5, ’茶葉茶加上蜂糖能喝嗎’: 1, ’古丈毛尖多少錢一斤’: 29, ’干玫瑰花過期’: 1}
我用的是pymysql鏈接的數(shù)據(jù)庫
之前統(tǒng)計的數(shù)據(jù)是單行的,所以用這個
cur.execute(’insert into pctongji(yesterday_str, item, question, topic, people, celebrity, brand, category, article) values(%s,%s,%s,%s,%s,%s,%s,%s,%s)’, (yesterday_str, category_dict[’item’], category_dict[’question’], category_dict[’topic’], category_dict[’people’], category_dict[’celebrity’], category_dict[’brand’], category_dict[’category’], category_dict[’article’]))
現(xiàn)在是多行的要怎么存入到數(shù)據(jù)庫,數(shù)據(jù)庫有兩個字段,分別是keywords、跟count,分別對應(yīng)上面字典中的內(nèi)容,sql語句要怎么寫,想要以天數(shù)的形式存入
問題解答
回答1:for key in keywords_dict.keys():cur.execute(’insert into keywords(yesterday_str, keywords, count) values(%s,%s,%s)’, (yesterday_str, key, keywords_dict[key]))
自己動手豐衣足食
回答2:建議使用 executemany
相關(guān)文章:
1. docker-compose中volumes的問題2. mysql 一個sql 返回多個總數(shù)3. CSS3 畫如下圖形4. 在mac下出現(xiàn)了兩個docker環(huán)境5. 如何用筆記本上的apache做微信開發(fā)的服務(wù)器6. android - rxjava merge 返回Object對象數(shù)據(jù)如何緩存7. javascript - 螞蟻金服里的react Modal方法,是怎么把元素插入到頁面最后的8. python - Scrapy存在內(nèi)存泄漏的問題。9. docker 下面創(chuàng)建的IMAGE 他們的 ID 一樣?這個是怎么回事????10. angular.js - ionic2 瀏覽器跨域問題
