Python, store a dict in a database

前端 未结 4 1080
青春惊慌失措
青春惊慌失措 2021-02-05 16:14

What\'s the best way to store and retrieve a python dict in a database?

4条回答
  •  广开言路
    2021-02-05 16:46

    If the dict directly corresponds to a database table, then you should set up a table with a schema containing two columns - key and value. Then for each element of the dict, just insert it into the database, or update it if the key is already in place, etc.

    Otherwise you could use pickle to serialize the dict to a string, and then you could just save the string to the DB. But in general I would not recommend this solution since you cannot query the serialized data.

提交回复
热议问题