插入数据mysql数据库时发生错误

时间:2018-04-06 15:35:04

标签: python mysql scrapy

class MysqlPipeline(object): 
        def __init__(self):
            **Connect the mysql**
            self.conn = MySQLdb.connect('localhost','root','root','zhihu',
            charset='utf8')
            self.cursor = self.conn.cursor()
        def process_item(self, item, spider):
            **insert**
            insert_sql = """
            insert into 
            users_info(img_url,user_name,business,user_followingCount,
            user_followerCount,idea_num,gender,favoriteCount,voteupCount,
            followingColumnsCount,participatedLiveCount,followingFavlistsCount,
            favoritedCount,uid,school_list,
            job_list,place_list,major_list,company_list,url_token)
            VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
            """
            param= (item["img_url"],item["user_name"],item["business"],
                    item["user_followingCount"],item["user_followerCount"],
                    item["idea_num"],item["gender"],item["favoriteCount"],
                    item["voteupCount"],item["followingColumnsCount"],
                    item["participatedLiveCount"],
                    item["followingFavlistsCount"],
                    item["favoritedCount"],item["uid"],item["school_list"],
                    item["job_list"],item["place_list"],item["major_list"],
                    item["company_list"],item["url_token"]
                    )
           self.cursor.execute(insert_sql,param)

错误

我该如何解决这个问题?

Traceback (most recent call last):
 File "C:\Users\Administrator.JQ8B7UF6EZUHOOH\Envs\article_spider\lib\site-packages\twisted\internet\defer.py", line 653, in _runCallbacks
    current.result = callback(current.result, *args, **kw)
  File "D:/分布式爬虫相关测试/Zhihu\Zhihu\pipelines.py", line 38, in process_item
    self.cursor.execute(insert_sql,param)
  File "C:\Users\Administrator.JQ8B7UF6EZUHOOH\Envs\article_spider\lib\site-packages\MySQLdb\cursors.py", line 250, in execute
    self.errorhandler(self, exc, value)
  File "C:\Users\Administrator.JQ8B7UF6EZUHOOH\Envs\article_spider\lib\site-packages\MySQLdb\connections.py", line 50, in defaulterrorhandler
    raise errorvalue
  File "C:\Users\Administrator.JQ8B7UF6EZUHOOH\Envs\article_spider\lib\site-packages\MySQLdb\cursors.py", line 247, in execute
    res = self._query(query)
  File "C:\Users\Administrator.JQ8B7UF6EZUHOOH\Envs\article_spider\lib\site-packages\MySQLdb\cursors.py", line 411, in _query
    rowcount = self._do_query(q)
  File "C:\Users\Administrator.JQ8B7UF6EZUHOOH\Envs\article_spider\lib\site-packages\MySQLdb\cursors.py", line 374, in _do_query
    db.query(q)
  File "C:\Users\Administrator.JQ8B7UF6EZUHOOH\Envs\article_spider\lib\site-packages\MySQLdb\connections.py", line 277, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.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 '),(),(),(),(),'qiu-shuo-47')' at line 4")

1 个答案:

答案 0 :(得分:1)

您正尝试在20列中插入19个值:

print(len(param))  # 19
print(insert_sql.count('%s'))  # 20