python/mysqldb:从原始输入在数据库中插入值

dced5bon  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(308)

我试图将原始输入的值保存在db中,但是我遇到了下面的错误。
我试着在stackoverflow上浏览了很多类似的主题,下面代码的sintax似乎是正确的。
你能帮我理解我错在哪里吗?
在我的代码和追踪下面。

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
yfwxisqw

yfwxisqw1#

发生此错误的原因如下: 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)

相关问题