Adding dict object to postgresql

后端 未结 2 1545
醉话见心
醉话见心 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:43

    If your PostgreSQL version is sufficiently new (9.4+) and psycopg version is >= 2.5.4 all the keys are strings and values can be represented as JSON, it would be best to store this into a JSONB column. Then, should the need arise, the column would be searchable too. Just create the table simply as

    CREATE TABLE thetable (
        uuid TEXT,
        dict JSONB
    );
    

    (... and naturally add indexes, primary keys etc as needed...) When sending the dictionary to PostgreSQL you just need to wrap it with the Json adapter; when receiving from PostgreSQL the JSONB value would be automatically converted into a dictionary, thus inserting would become

    from psycopg2.extras import Json, DictCursor
    
    cur = conn.cursor(cursor_factory=DictCursor)
    
    cur.execute('INSERT into thetable (uuid, dict) values (%s, %s)',
        ['testName', Json({'id':'122','name':'test','number':'444-444-4444'})])
    

    and selecting would be as simple as

    cur.execute('SELECT dict FROM thetable where uuid = %s', ['testName'])
    row = cur.fetchone()
    print(row['dict']) # its now a dictionary object with all the keys restored
    print(row['dict']['number']) # the value of the number key
    

    With JSONB, PostgreSQL can store the values more efficiently than just dumping the dictionary as text. Additionally, it becomes possible to do queries with the data, for example just select the some of the fields from the JSONB column:

    >>> cur.execute("SELECT dict->>'id', dict->>'number' FROM thetable")
    >>> cur.fetchone()
    ['122', '444-444-4444']
    

    or you could use them in queries if needed:

    >>> cur.execute("SELECT uuid FROM thetable WHERE dict->>'number' = %s',
        ['444-444-4444'])
    >>> cur.fetchall()
    [['testName', {'id': '122', 'name': 'test', 'number': '444-444-4444'}]]
    
    0 讨论(0)
  • 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'])
    
    0 讨论(0)
提交回复
热议问题