I am writing a small python script like this:
#!/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)
I have no problem when I try to take in one input
python user.py blah
but When I try to do with more than one,It goes into the else loop.
where am I doing the errors? Please guide me thru…
You get this error because the first argument is actually the file name.
A simple test file shows how this works:
You should also fix your SQL/query
See the sqlite api docs.