我正在写一个小的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循环。你知道吗
我在哪里犯错?请引导我穿过。。。你知道吗
sys.argv
中的第一个元素是脚本的名称,因此实际上您可能希望if语句包含以下内容:出现此错误是因为第一个参数实际上是文件名。你知道吗
一个简单的测试文件显示了其工作原理:
您还应该修复SQL/查询
参见sqlite api docs。你知道吗
相关问题 更多 >
编程相关推荐