ASP.net Web API - все о HTTP и REST, основанных на GET, POST, PUT, DELETE, хорошо знакомы с ASP.NET MVC-стилем программирования и возвратом JSON; веб-API предназначен для всего легкого процесса и чистых компонентов на основе HTTP. Для того, чтобы идти вперед с WCF даже для простого или простого одиночного веб-сервиса, он принесет весь лишний багаж. Для облегчения простого обслуживания для ajax или динамических вызовов WebApi просто решает эту проблему. Это аккуратно дополняет или помогает параллельно ASP.net MVC.
Проверьте подкаст : Hanselminutes Podcast 264 - это не ваш WCF вашего отца - все о WebAPI с Glenn Block Scott Hanselman для получения дополнительной информации.
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'))
Это решит вашу проблему. Однако вы действительно должны хранить ключи и значения в своих отдельных столбцах. Чтобы получить словарь, выполните:
cur.execute('select charecteristics from product where store_id = 1')
dictionary = json.loads(cur.fetchone()[0])
Надеюсь, что это поможет.
Вы можете использовать psycopg2.extras.Json
для преобразования dict в json, который принимает postgre.
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)
Для получения дополнительной информации вы можете увидеть офисный документ .
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.