Python2.7 - Sqlite3 - 2 inputs
Question: 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...


Answer: You get this error because the first argument is actually the file name.

A simple test file shows how this works:

    
    
    [~]$ cat test.py
    from sys import argv
    
    if __name__ == '__main__':
        print argv
        print len(argv)
    [~]$ python test.py one two
    ['test.py', 'one', 'two']
    3
    [~]$ python test.py one
    ['test.py', 'one']
    2
    

You should also fix your SQL/query

    
    
    query = "INSERT OR REPLACE INTO user VALUES(?,?)"
    cursor.execute(query,[na,ha])
    conn.commit()
    

See the [sqlite api docs](http://docs.python.org/library/sqlite3.html).

