2014-10-04 163 views
0

我有一个用python编写的sql。如何将html数据存储在mysql数据库中?

cur.execute("INSERT INTO products_details(
title, 
description, 
price, 
currency, 
sku, 
brand, 
colors, 
sizes, 
actual_url, 
meta_title, 
meta_keywords, 
meta_description, 
sizefitcontainer, 
designercontainer, 
wearwith, 
colorthumb, 
colorbig, 
colormedium, 
discount_price, 
cat_name) 
VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s') ", (
context['product_title'], 
context['product_description'], 
context['price'], 
context['currency'], 
context['productCode'], 
context['brand_name'], 
context['colors'], 
context['sizes'], 
context['actual_url'], 
context['title'], 
context['meta_keywords'], 
context['meta_description'], 
context['sizefitcontainer'], 
context['designercontainer'], 
context['wearwith'], 
context['colorthumb'], 
context['colorbig'], 
context['colormedium'], 
context['discount_price'], 
context['cat_name'])) 

在上面的查询中有两个字段designercontainer , and sizefitcontainer在这我传递一些HTML数据在数据库存储。 但每次我收到一些错误。

(<class '_mysql_exceptions.ProgrammingError'>, ProgrammingError(1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Cosmic Leggings'',''These cropped KORAL ACTIVEWEAR leggings have iridescent cont' at line 1"), <traceback object at 0x2bc2638>). 

我试图UTF编码也是这也解决不了这个问题。请告诉我如何写查询,以便这两个领域都可以接受HTML值(用js和css嵌入)。

context is a python dict. 

回答

0

您需要传递数据作为第二个参数,如.execute()

所以它会像

sql = '''insert into headline (column,column1) 
        values ("%s","%s","%s","%s","%s");''' 

cursor.execute(sql, (values,values1))

在这里,您havnt正确引用您要保存到数据库中的HTML字符串..

您可以conn.escape_string()逃脱值

看一看here

1

您应该在您的问题中包含product_details的结构。

根据错误判断,您没有正确引用要存储的HTML字符串。

+0

嗨检查输入 – user1481793 2014-10-04 16:12:12

相关问题