Python SQLite3 is not working
Question: I am practing using SQLite3 in Python. The version of Python is 2.7 And I am
using mac os.

I cannot find any problem with my code... When I run it, I do not get any
syntax error. My coding suppose to print out every element of database. It
does not print anything at all.

    
    
    import sqlite3
    
    createDb = sqlite3.connect('sample.db')
    
    queryCurs = createDb.cursor()
    
    def createTable():
        queryCurs.execute('''CREATE TABLE customers
        (id INTEGER PRIMARY KEY, name TEXT, street TEXT, city TEXT, state TEXT, balance REAL)''')
    
    def addCust(name, street, city, state, balance):
        queryCurs.execute('''INSERT INTO customers (name, street, city, state, balance))
        VALUES (?,?,?,?,?) ''',(name, street, city, state, balance))
    
    def main():
        createTable()
    
        addCust('test1','123 mel','vancouver','bc', 10000000.00)
        addCust('test2','145 joy','ottawa','on', 10000000.00)
        addCust('test3','521 tick','toronto','on', 10000000.00)
        addCust('test4','5832 tock','burnaby','bc', 10000000.00)
    
        createDb.commit()
    
        queryCurs.execute('SELECT * FROM customers')
    
        for i in queryCurs:
            print "\n"
            for j in i:
                print j
    

Can you tell me what I am doing wrong?


Answer: You never _call_ your `main()` function. Python doesn't automatically call
functions, `main()` has no special meaning. You'd normally add a "script
test":

    
    
    if __name__ == '__main__':
        main()
    

to the end of your module to call a function when the file is run as a script;
when your file is imported as a module instead `__name__` contains the module
name instead of `'__main__'`.

When you do, you'll see you have a SQL syntax error:

    
    
    Traceback (most recent call last):
      File "test.py", line 33, in <module>
        main()
      File "test.py", line 18, in main
        addCust('test1','123 mel','vancouver','bc', 10000000.00)
      File "test.py", line 13, in addCust
        VALUES (?,?,?,?,?) ''',(name, street, city, state, balance))
    sqlite3.OperationalError: near ")": syntax error
    

You have one too many `)` in your SQL following the list of column names,
remove it:

    
    
    def addCust(name, street, city, state, balance):
        queryCurs.execute('''INSERT INTO customers (name, street, city, state, balance)
        VALUES (?,?,?,?,?) ''',(name, street, city, state, balance))
    

