search

Home  >  Q&A  >  body text

How to store the analyzed dictionary data in the database in Python 3.5

字典是
keywords_dict{}
{'茉莉花茶泡多久': 10, '苦丁长什么样子图片': 5, '茶叶茶加上蜂糖能喝吗': 1, '古丈毛尖多少钱一斤': 29, '干玫瑰花过期': 1}

I am using the database linked to pymysql

The previous statistical data was a single line, so use this

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']))
  

Now is how to save multiple rows into the database.
The database has two fields, namely keywords and count, which correspond to the contents in the dictionary above.
How to write the sql statement? What do you want? Deposit as number of days

ringa_leeringa_lee2811 days ago780

reply all(2)I'll reply

  • ringa_lee

    ringa_lee2017-05-18 11:01:27

        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]))

    Make enough food and clothing by yourself

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-18 11:01:27

    It is recommended to use executemany

    reply
    0
  • Cancelreply