1

I am trying to save the values from a raw_input inside a DB but I am facing the error below.
I tried to navigate through a lot of similar topics here on Stackoverflow and the sintax of the code below seems correct.
Could you please help me understanding where I am wrong?
Below my code and the traceback.

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

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

1 Answer 1

2

This error occurs because of this:
sql = """INSERT INTO accounts (website, username, password) VALUES (%s, %s, %s)""",(wbs, usr, psw) you trying to set sql and its data to sql var and sql vars type will be tuple to be avoid this you need to use 2 variable like:

sql = """INSERT INTO accounts (website, username, password) VALUES (%s, %s, %s)"""
data = (wbs, usr, psw)
cursor.execute(sql, data)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the explanation. The code works and the concept is clearer for me!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.