我完全迷路了。这是我的管道。我说我跑错了File "c:\python27\lib\site-packages\twisted\internet\defer.py", line 588, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "C:\Python27\bff\bff\pipelines.py", line 42, in process_item
cursor.execute(add_Product)
File "c:\python27\lib\site-packages\mysql\connector\cursor.py", line 492, in execute
stmt = operation.encode(self._connection.python_charset)
AttributeError: 'tuple' object has no attribute 'encode'
正如您在注释代码中看到的,我尝试了几种不同的方法。一开始,正如我在示例中看到的那样做,但是我得到一个错误:当我把(item['StoreName'])放在值行中而不是在上面定义它为Name=item['StoreName']时,sadi item没有被定义
一、 我使用mySQL.connector我从mqsql.org网站网站。提前谢谢
^{pr2}$
编辑。这是我的新代码`
from __future__ import print_function
from datetime import date, datetime, timedelta
import mysql.connector
#from scrapy.extensions import DropItem
#from bff.items import ItemInfo
class mySQLPipeline(object):
def process_item(self, item, spider):
Product = item['ProdName']
Path = item['ProdPath']
UPC = item['ProdUPC']
Model = item['ProdModel']
Desc = item['ProdDesc']
Price = item['ProdPrice']
Stock = item['InStock']
#Ships = item['Ships']
Name = item['StoreName']
cnx = mysql.connector.connect(user='****', password='****',
host='127.0.0.1',
port='****',
database='****')
cursor = cnx.cursor()
# add_Product = ("INSERT INTO walmart_products (ProdName, StoreName) VALUES (%s, %s,)", Product, Name,)
# add_Product = ("INSERT INTO walmart_products, (ProdName)"
# "VALUES (%s)", (Name))
# "VALUES (%(Name)s)")
add_Product = ("INSERT INTO walmart_products "
"(ProdName, ProdPath, ProdUPC, ProdModel, ProdDesc, ProdPrice, InStock, StoreName) "
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s)")
#item['Ships'],
data_Product = (Product, Path, UPC, Model, Desc, Price, Stock, Name)
#Add new product
cursor.execute(add_Product, data_Product)
# Make sure data is committed to the database
cnx.commit()
cursor.close()
cnx.close()
return item
`