Adding dict object to postgresql

后端 未结 2 1546
醉话见心
醉话见心 2020-12-09 06:34

So I am using psycopg2 on Python3.5 to insert some data into a postgresql database. What I would like to do is have two columns that are strings and have the last column jus

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-09 06:45

    You can serialize the data using JSON before storing the data:

    import json
    
    data = json.dumps({'id':'122','name':'test','number':'444-444-4444'})
    

    Then when retrieving the code you deserialize it:

    cur.execute('SELECT dict from ....')
    res = cur.fetchone()
    
    dict = json.loads(res['dict'])
    print(dict['number'])
    

提交回复
热议问题