我正在写一个像这样的小python脚本:
#!/usr/bin/env python
from sqlite3 import dbapi2 as sqlite
from sys import argv,exit
db_name = "hashez.db"
def define_db():
try:
conn = sqlite.connect(db_name)
except IOError as e:
print "problem while creating/connecting the db:",e.args[0]
exit(1)
return conn
def write_db(conn,cursor,na,ha):
conn.execute("CREATE TABLE IF NOT EXISTS user (name TEXT UNIQUE, hash TEXT UNIQUE)")
query = "INSERT OR REPLACE INTO user VALUES($name,$hash)"
cursor.execute(query,[na],[ha])
cursor.close()
conn.commit()
conn.close()
exit(0)
if __name__ == "__main__":
if len(argv) == 2:
na,ha = argv[1]
#ha = argv[2]
else:
print "no argument given - stopping now"
exit(1)
conn = define_db()
cursor = conn.cursor()
write_db(conn,cursor,na,ha)
当我尝试接受一个输入时我没有问题
python user.py blah
但是当我尝试处理多个时,它会进入 else 循环。
我在哪里做错误?请指导我通过...