Ror*_*ach 20 python postgresql dictionary psycopg2
我想将一个python字典作为json插入到我的postgresql数据库中(通过python和psycopg2).我有:
thedictionary = {'price money': '$1', 'name': 'Google', 'color': '', 'imgurl': 'http://www.google.com/images/nav_logo225.png', 'charateristics': 'No Description', 'store': 'google'}
cur.execute("INSERT INTO product(store_id, url, price, charecteristics, color, dimensions) VALUES (%d, %s, %s, %d, %s, %s)", (1, 'http://www.google.com', '$20', thedictionary, 'red', '8.5x11'))
Run Code Online (Sandbox Code Playgroud)
它给出了错误消息:
thedictionary = {'price money': '$1', 'name': 'Google', 'color': '', 'imgurl': 'http://www.google.com/images/nav_logo225.png', 'charateristics': 'No Description', 'store': 'google'}
cur.execute("INSERT INTO product(store_id, url, price, charecteristics, color, dimensions) VALUES (%d, %s, %s, %d, %s, %s)", (1, 'http://www.google.com', '$20', thedictionary, 'red', '8.5x11'))
Run Code Online (Sandbox Code Playgroud)
我不知道如何从这里开始.我在互联网上找不到关于如何做这种事情的事情,我对psycopg2很新.
hd1*_*hd1 42
cur.execute("INSERT INTO product(store_id, url, price, charecteristics, color, dimensions) VALUES (%s, %s, %s, %s, %s, %s)", (1, 'http://www.google.com', '$20', json.dumps(thedictionary), 'red', '8.5x11'))
Run Code Online (Sandbox Code Playgroud)
这将解决您的问题.但是,您确实应该将键和值存储在各自独立的列中.要检索字典,请执行以下操作:
cur.execute('select charecteristics from product where store_id = 1')
dictionary = json.loads(cur.fetchone()[0])
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你.
Fel*_*sto 13
注意您可以使用 register_adapter() 将任何 Python 字典调整为 JSON,注册 Json 或任何创建兼容适配器的子类或工厂:
psycopg2.extensions.register_adapter(dict, psycopg2.extras.Json)但是这个设置是全局的,所以它与类似的适配器不兼容,比如由 register_hstore() 注册的适配器。任何其他 JSON 支持的对象都可以用同样的方式注册,但这会破坏默认的适配规则,所以要小心不必要的副作用。
所以,就我而言,我所做的是:
from psycopg2.extensions import register_adapter
register_adapter(dict, Json)
Run Code Online (Sandbox Code Playgroud)
它就像一个魅力。
您可以使用psycopg2.extras.Json将dict转换为postgre接受的json.
from psycopg2.extras import Json
thedictionary = {'price money': '$1',
'name': 'Google', 'color': '', 'imgurl': 'http://www.google.com/images/nav_logo225.png', 'charateristics': 'No Description', 'store': 'google'}
item ={
"store_id":1,
"url": 'http://www.google.com',
"price":'$20',
"charecteristics":Json(thedictionary),
"color":'red',
"dimensions":'8.5x11'
}
def sql_insert(tableName, data_dict):
'''
INSERT INTO product (store_id, url, price, charecteristics, color, dimensions)
VALUES (%(store_id)s, %(url)s, %(price)s, %(charecteristics)s, %(color)s, %(dimensions)s );
'''
sql = '''
INSERT INTO %s (%s)
VALUES (%%(%s)s );
''' % (tableName, ', '.join(data_dict), ')s, %('.join(data_dict))
return sql
tableName = 'product'
sql = sql_insert(tableName, item)
cur.execute(sql, item)
Run Code Online (Sandbox Code Playgroud)
有关更多信息,您可以查看office文档.
class psycopg2.extras.Json(adapted, dumps=None)
An ISQLQuote wrapper to adapt a Python object to json data type.
Json can be used to wrap any object supported by the provided dumps function. If none is provided, the standard json.dumps() is used (simplejson for Python < 2.6; getquoted() will raise ImportError if the module is not available).
dumps(obj)
Serialize obj in JSON format.
The default is to call json.dumps() or the dumps function provided in the constructor. You can override this method to create a customized JSON wrapper.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31250 次 |
| 最近记录: |