除了MySQLdb,我正在接收错误'。错误,e的语法错误-当我试图写我的剪贴簿到mysql时,无效的语法,

时间:2022-09-22 22:54:43

Here is my pipelines.py and I'm receiving an error on line 18.

这是我的管道。我在第18行收到一个错误。

import sys;sys.path.append("/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages")
import MySQLdb
import hashlib
from scrapy.exceptions import DropItem
from scrapy.http import Request

class TestPipeline(object):

    def __init__(self):
        self.conn = MySQLdb.connect(user='test', passwd='password', db='c1024403', host='ephesus.cs.cf.ac.uk', charset='utf8', use_unicode=True)
        self.cursor = self.conn.cursor()

    def process_item(self, item, spider):
        try:
            self.cursor.execute("""INSERT INTO test (test1, test2) VALUES (%s, %s)""", (item['Country'], item['Qualification']))
            self.conn.commit()

        except MySQLdb.Error, e:
            print 'Error %d: %s' % (e.args[0], e.args[1])
            sys.exit(1)

        return item

Heres the error -

这是错误的,

File "project\pipelines.py", line 18
except MySQLdb.Error, e:
     ^
SyntaxError: invalid syntax

I've installed mysql-python and visual c++ 2008 express, I don't understand what the error means as I can't find anything about it anywhere else on the internet.

我已经安装了mysql-python和visual c++ 2008 express,我不明白这个错误意味着什么,因为我在互联网上的任何地方都找不到它。

1 个解决方案

#1


4  

You are running your code with Python 3.x, but your code scheme for try.. except section is for Python 2.X.

您正在使用Python 3运行代码。x,但是你的密码方案可以试试。除了部分是Python 2.X。

If you want to run your code with Python 3.x, then change this line:

如果你想用python3运行你的代码。x,然后改变这条线:

except MySQLdb.Error, e:

To:

:

except MySQLdb.Error as e:

And if you want this section of code works with Python 2.x and also Python 3.x, then change it to:

如果你想让这段代码和Python 2一起工作。还有Python 3。x,然后更改为:

except MySQLdb.Error:
    e = sys.exc_info()[1]

Read more.

阅读更多。

But according to your print statement, you write your script for Python 2.x, so it's better to run your code with Python 2.x, instead of Python 3.x

但是根据您的print语句,您为Python 2编写脚本。x,所以最好使用Python 2运行代码。x,而不是Python 3。x

Also this sys.path.append("../python2.7/site-packages") line is strange in first line of your script.

同样,这个system .path.append(“.. ./python2.7/site-packages”)行在脚本的第一行是奇怪的。

Also your indention of your first code that you pasted was wrong, and i think your are still using that, please use current edited version that is now in your question.

你粘贴的第一个代码的缩进也是错误的,我认为你还在使用它,请使用当前编辑过的版本。

#1


4  

You are running your code with Python 3.x, but your code scheme for try.. except section is for Python 2.X.

您正在使用Python 3运行代码。x,但是你的密码方案可以试试。除了部分是Python 2.X。

If you want to run your code with Python 3.x, then change this line:

如果你想用python3运行你的代码。x,然后改变这条线:

except MySQLdb.Error, e:

To:

:

except MySQLdb.Error as e:

And if you want this section of code works with Python 2.x and also Python 3.x, then change it to:

如果你想让这段代码和Python 2一起工作。还有Python 3。x,然后更改为:

except MySQLdb.Error:
    e = sys.exc_info()[1]

Read more.

阅读更多。

But according to your print statement, you write your script for Python 2.x, so it's better to run your code with Python 2.x, instead of Python 3.x

但是根据您的print语句,您为Python 2编写脚本。x,所以最好使用Python 2运行代码。x,而不是Python 3。x

Also this sys.path.append("../python2.7/site-packages") line is strange in first line of your script.

同样,这个system .path.append(“.. ./python2.7/site-packages”)行在脚本的第一行是奇怪的。

Also your indention of your first code that you pasted was wrong, and i think your are still using that, please use current edited version that is now in your question.

你粘贴的第一个代码的缩进也是错误的,我认为你还在使用它,请使用当前编辑过的版本。