0

I am writing a program to store passwords, but it doesn't seem to be writing to the database. When I "add a password," it doesn't show an error, but when I "find existing password," all I get is an empty array (i.e. [ ]). Note that since I don't commit the changes until the very end of the file, I quit, reload, and then search for the existing password.

In my folder I already have an empty file, 'passwords.db'.

Here is my code:

import sys
import sqlite3 as lite

con = lite.connect("passwords.db")
cur = con.cursor()

cur.execute("CREATE TABLE if not EXISTS passwords(site VARCHAR(50), username VARCHAR(20), password VARCHAR(20));")

print "Welcome to Passwords.py."

choice = None

while choice == None:
    print "What would you like to do?"
    print "1.) Add new password."
    print "2.) Find existing password."
    print "3.) Update existing password."
    print "4.) Quit."
    choice = raw_input("> ")

    if choice == "1":
        s = raw_input("Which website to add? ")
        name = raw_input("Username to add? ")
        passwd = raw_input("Password to add? ")

        cur.execute("INSERT INTO passwords VALUES (?,?,?)", (s,name,passwd))

        choice = None

    elif choice == "2":
        s = raw_input("Find info for which website? ")

        cur.execute("SELECT * FROM passwords WHERE site=?", (s,))

        print cur.fetchall()

        choice = None

    elif choice == "3":
        s = raw_input("Update info for which website? ")
        name = raw_input("New username? ")
        passwd = raw_input("New password? ")

        cur.execute("UPDATE passwords SET username, password WHERE site=?", (s,))

        choice = None

    elif choice == "4":
        quit()

    else:
        print "Enter 1, 2, or 3."
        choice = None

### cleaning up.

if con:
    con.commit()
    con.close()

The offending program:

laura@laura-AO722:~/Desktop/passwords$ python passwords.py
Welcome to Passwords.py.
What would you like to do?
1.) Add new password.
2.) Find existing password.
3.) Update existing password.
4.) Quit.
> 1
Which website to add? a
Username to add? b
Password to add? c
What would you like to do?
1.) Add new password.
2.) Find existing password.
3.) Update existing password.
4.) Quit.
> 4
laura@laura-AO722:~/Desktop/passwords$ python passwords.py
Welcome to Passwords.py.
What would you like to do?
1.) Add new password.
2.) Find existing password.
3.) Update existing password.
4.) Quit.
> 2
Find info for which website? a
[]
2
  • Just wondering if you need last comma in this statement: cur.execute("SELECT * FROM passwords WHERE site=?", (s,)). It does not seem right Commented Jun 24, 2013 at 23:09
  • 2
    Your cleaning up code never gets executed, in choice 4, use the break statement, not the quit() statement. Commented Jun 24, 2013 at 23:12

1 Answer 1

1

Your update SQL should look something like this

cur.execute("UPDATE passwords SET username=?, password=? WHERE site=?", (name, passwd, s,))

Here is the output I get

>>> 
Welcome to Passwords.py.
What would you like to do?
1.) Add new password.
2.) Find existing password.
3.) Update existing password.
4.) Quit.
> 1
Which website to add? google.com
Username to add? john
Password to add? pass
What would you like to do?
1.) Add new password.
2.) Find existing password.
3.) Update existing password.
4.) Quit.
> 2
Find info for which website? google.com
[(u'google.com', u'john', u'pass')]
What would you like to do?
1.) Add new password.
2.) Find existing password.
3.) Update existing password.
4.) Quit.
> 3
Update info for which website? google.com
New username? john2
New password? pass2
What would you like to do?
1.) Add new password.
2.) Find existing password.
3.) Update existing password.
4.) Quit.
> 2
Find info for which website? google.com
[(u'google.com', u'john2', u'pass2')]
What would you like to do?
1.) Add new password.
2.) Find existing password.
3.) Update existing password.
4.) Quit.
> 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.