[英]Python mysql error #1064
我試圖通過編寫管道將數據存儲到MySQL中,但是發生了:
_mysql_exceptions.ProgrammingError:(1064,“您的SQL語法有錯誤;請查看與您的MySQL服務器版本相對應的手冊,以獲取正確的語法以使用接近'desc,pic)值('xe4 \\ xbe \\ x9b \\ xe5 \\ xba .......'在第1行“)
這是我的代碼:
def __init__(self):
#self.file = codecs.open('tutorial_data.json','wb',encoding='utf-8')
self.dbpool = adbapi.ConnectionPool('MySQLdb',
host = 'localhost',
port = 3306,
db = 'project',
user = 'root',
passwd = '',
cursorclass = MySQLdb.cursors.DictCursor,
charset = 'utf8',
use_unicode = True)
def process_item(self, item, spider):
query = self.dbpool.runInteraction(self._conditional_insert, item)
query.addErrback(self.handle_error)
return item
def _conditional_insert(self,tx,item):
tx.execute(
"INSERT INTO raw (title,area,date,sclass,desc,pic )\
VALUES (%s, %s, %s, %s, %s, %s)",
(item['title'][0],
item['area'][0],
item['date'][0],
item['sclass'][0],
item['desc'][0],
item['pic'][0])
)
我的MySQL版本是5.6.17,mysql-python版本是1.2.5_
DESC
是MySQL中的保留關鍵字 。 如果DESC
名列標識符DESC
,則必須將其包裝在刻度中:
tx.execute(
"INSERT INTO raw (title,area,date,sclass,`desc`,pic )\
VALUES (%s, %s, %s, %s, %s, %s)",
但是,更好的解決方案是不使用保留關鍵字作為列標識符,因為它可能在其他地方引起混亂和潛在的錯誤(加上完整的“描述”一詞更加清晰,並使代碼更易於維護)。
tx.execute(
"INSERT INTO raw (title,area,date,sclass,description,pic )\
VALUES (%s, %s, %s, %s, %s, %s)",
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.