Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在mysql数据库中插入数据时出错 错误_Python_Mysql_Scrapy - Fatal编程技术网

Python 在mysql数据库中插入数据时出错 错误

Python 在mysql数据库中插入数据时出错 错误,python,mysql,scrapy,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(

我应该如何解决这个问题

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)

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

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")

我将对SQL查询进行分析,并在实际的MySQL中运行它,看看错误到底是什么。这通常是由SQL级别的类型错误(文本转换为int字段)生成的,但被MySqlDb模块中的一般错误掩盖了。@zjian请在问题中添加一些文本。当我在SO:-)query=cursor.mogrify(SQL,params)上寻求帮助时,一些口头解释可能会非常有用。我在列表类型中插入了许多变量。我没有将它们转换为STR类型,因此出现了一个错误。非常感谢您的帮助。@zjian:如果您想对答案发表评论,请在下面的评论框中发表评论,不要编辑答案。另外,既然它解决了你的问题,请。
print(len(param))  # 19
print(insert_sql.count('%s'))  # 20