簡體   English   中英

如何將html數據存儲在mysql數據庫中?

[英]How do I store html data in a mysql database?

我有一個用python編寫的sql。

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數據以存儲在db中。 但是每當我遇到錯誤的時候。

(<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.

您應該在問題中包括product_details的結構。

從錯誤判斷,您沒有正確引用要存儲的HTML字符串。

您需要將數據作為.execute()作為第二個參數傳遞。

所以就像

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

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

在這里,您已經正確引用了要存儲到數據庫中的html字符串。

您可以使用conn.escape_string()來轉義值

在這里看看

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM