Python, store a dict in a database

前端 未结 4 1069
青春惊慌失措
青春惊慌失措 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:31

    "Best" is debatable.
    If you need a DBMS, sqlite is built in; so it might be considered an easier method than some of the other ways mentioned.

    import sqlite3
    conn = sqlite3.connect(':memory:')
    c = conn.cursor()
    c.execute("create table kv (key text, value integer);")
    # 
    d = {'a':1,'b':2}
    c.executemany("insert into kv values (?,?);", d.iteritems())
    # 
    c.execute("select * from kv;").fetchall()
    # [(u'a', 1), (u'b', 2)]
    

提交回复
热议问题