Insert Values from dictionary into sqlite database

后端 未结 3 2103
孤独总比滥情好
孤独总比滥情好 2021-02-06 10:40

I cannot get my head around it. I want to insert the values of a dictionary into a sqlite databse.

url = \"https://api.flickr.com/services/rest/?method=flickr.ph         


        
3条回答
  •  终归单人心
    2021-02-06 11:00

    If you're using Python 3.6 or above, you can do this for dicts:

    dict_data = {
     'filename' : 'test.txt',
     'size' : '200'
    }
    table_name = 'test_table'
    attrib_names = ", ".join(dict_data.keys())
    attrib_values = ", ".join("?" * len(dict_data.keys()))
    sql = f"INSERT INTO {table_name} ({attrib_names}) VALUES ({attrib_values})"
    cursor.execute(sql, list(dict_data.values()))
    

提交回复
热议问题