2014-10-04 161 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

相關問題