Python/MySQLdb:从 raw_input 向数据库中插入值

Python/MySQLdb: Insert values in a DB from raw_input

我正在尝试将 raw_input 中的值保存在数据库中,但我遇到了以下错误。
我试图在 Whosebug 上浏览很多类似的主题,下面代码的语法似乎是正确的。
你能帮我理解我错在哪里吗?
在我的代码和回溯下方。

def SaveIntoDb(self):
    wbs = raw_input("Account?")
    usr = raw_input("Username?")
    psw = raw_input("Password?")
    # Open database connection
    db = MySQLdb.connect("localhost", "root", "pass", "PManDB")
    # prepare a cursor object using cursor() method
    cursor = db.cursor()
    # Insert values in a table
    sql = """INSERT INTO accounts (website, username, password) VALUES (%s, %s, %s)""",(wbs, usr, psw)
    cursor.execute(sql)

回溯

Traceback (most recent call last):
   File "/home/marco/PycharmProjects/PasswordManager/main.py", line 142, in <module>
     PasswordManager().run()
   File "/usr/lib/python2.7/dist-packages/kivy/app.py", line 828, in run
     runTouchApp()
   File "/usr/lib/python2.7/dist-packages/kivy/base.py", line 487, in runTouchApp
     EventLoop.window.mainloop()
   File "/usr/lib/python2.7/dist-packages/kivy/core/window/window_sdl2.py", line 619, in mainloop
     self._mainloop()
   File "/usr/lib/python2.7/dist-packages/kivy/core/window/window_sdl2.py", line 362, in _mainloop
     EventLoop.idle()
   File "/usr/lib/python2.7/dist-packages/kivy/base.py", line 330, in idle
     self.dispatch_input()
   File "/usr/lib/python2.7/dist-packages/kivy/base.py", line 315, in dispatch_input
     post_dispatch_input(*pop(0))
   File "/usr/lib/python2.7/dist-packages/kivy/base.py", line 221, in post_dispatch_input
     listener.dispatch('on_motion', etype, me)
   File "_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy/_event.c:7146)
   File "/usr/lib/python2.7/dist-packages/kivy/core/window/__init__.py", line 1030, in on_motion
     self.dispatch('on_touch_down', me)
   File "_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy/_event.c:7146)
   File "/usr/lib/python2.7/dist-packages/kivy/core/window/__init__.py", line 1046, in on_touch_down
     if w.dispatch('on_touch_down', touch):
   File "_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy/_event.c:7146)
   File "/usr/lib/python2.7/dist-packages/kivy/uix/screenmanager.py", line 1070, in on_touch_down
     return super(ScreenManager, self).on_touch_down(touch)
   File "/usr/lib/python2.7/dist-packages/kivy/uix/widget.py", line 432, in on_touch_down
     if child.dispatch('on_touch_down', touch):
   File "_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy/_event.c:7146)
   File "/usr/lib/python2.7/dist-packages/kivy/uix/relativelayout.py", line 278, in on_touch_down
     ret = super(RelativeLayout, self).on_touch_down(touch)
   File "/usr/lib/python2.7/dist-packages/kivy/uix/widget.py", line 432, in on_touch_down
     if child.dispatch('on_touch_down', touch):
   File "_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy/_event.c:7146)
   File "/usr/lib/python2.7/dist-packages/kivy/uix/widget.py", line 432, in on_touch_down
     if child.dispatch('on_touch_down', touch):
   File "_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy/_event.c:7146)
   File "/usr/lib/python2.7/dist-packages/kivy/uix/behaviors/button.py", line 110, in on_touch_down
     self.dispatch('on_press')
   File "_event.pyx", line 714, in kivy._event.EventDispatcher.dispatch (kivy/_event.c:7105)
   File "_event.pyx", line 1224, in kivy._event.EventObservers.dispatch (kivy/_event.c:12330)
   File "_event.pyx", line 1108, in kivy._event.EventObservers._dispatch (kivy/_event.c:11370)
   File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 1557, in custom_callback
     exec(__kvlang__.co_value, idmap)
   File "<string>", line 22, in <module>
   File "/home/marco/PycharmProjects/PasswordManager/main.py", line 96, in SaveIntoDb
     cursor.execute(sql)
   File "/usr/local/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 198, in execute
     self.errorhandler(self, TypeError, m)
   File "/usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
     raise errorclass, errorvalue
 TypeError: query() argument 1 must be string or read-only buffer, not tuple

出现此错误的原因是:
sql = """INSERT INTO accounts (website, username, password) VALUES (%s, %s, %s)""",(wbs, usr, psw) 您尝试将 sql 及其数据设置为 sql var 并且 sql vars 类型将是元组 为了避免这种情况,您需要使用 2 个变量,例如:

sql = """INSERT INTO accounts (website, username, password) VALUES (%s, %s, %s)"""
data = (wbs, usr, psw)
cursor.execute(sql, data)