我正在编写一个程序来存储密码,但它似乎并没有写入数据库。当我“添加密码”时,它不会显示错误,但是当我“找到现有密码”时,我得到的只是一个空数组(即 [ ])。请注意,由于我直到文件末尾才提交更改,所以我退出,重新加载,然后搜索现有密码。
在我的文件夹中,我已经有一个空文件“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
[]