我正在编写一个存储密码的程序,但它似乎没有写入数据库。当我“添加密码”时,它没有显示错误,但是当我“找到现有密码”时,我得到的只是一个空数组(即[])。请注意,由于我直到文件的最后才提交更改,因此我退出,重新加载,然后搜索现有密码。
在我的文件夹中,我已经有一个空文件'passwords.db'。
这是我的代码:
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()
违规计划:
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
[]
答案 0 :(得分:1)
您的更新SQL应该看起来像这样
cur.execute("UPDATE passwords SET username=?, password=? WHERE site=?", (name, passwd, s,))
这是我得到的输出
>>>
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.
>