Python, SQL syntax error but I don´t see it

I´m working on an SQL/Python exercise from one of the supplemental resources and I´m running into a syntax error:

Traceback (most recent call last):
  File "c:\Users\Admin\Desktop\PY4E\fcc\seq_practice.py", line 19, in <module>
    cur.execute('''INSERT INTO Counts (email, count) 
sqlite3.OperationalError: near ")": syntax error

BUT I don´t see the error. Everything seems fine. However, VSCode doesn´t lie, so I need some help in figuring this out. I´m pasting my code below as well as a link to the YouTube video of the supplement resource exercise I´m working from.

Thank you for any help you can provide.

Exercise: Email

import sqlite3

conn = sqlite3.connect('emaildb.sqlite')
cur = conn.cursor()

cur.execute('DROP TABLE IF EXISTS Counts')
cur.execute('CREATE TABLE Counts (email TEXT, count INTEGER)')

fname = input('Enter file name: ')
if len(fname) < 1: fname='mbox.txt'
fh = open(fname)
for line in fh:
    if not line.startswith('From '):
        pieces = line.split()
        email = pieces[1]
        cur.execute('SELECT count FROM Counts WHERE email = ?', (email,))
        row = cur.fetchone()
        if row is None:
            cur.execute('''INSERT INTO Counts (email, count) 
            VALUES(?,)''', (email,))
        else:
            cur.execute('UPDATE Counts SET count = count + 1 WHERE email = ?'), conn.commit()
        
        #http://www.sqlite.org/lang_select.html
        sqlstr = 'SELECT email, count FROM Counts ORDER BY count DESC LIMIT 10'
        for row in cur.execute(sqlstr):
            print(row[0], row[1])
        
cur.close()

INSERT INTO Counts (email, count) VALUES(?,)
What’s the value inserted into the count field?

I agree. I’m following the lead of Charles Severance in the video resource for the course. His explanation was that this (email,)) was a tuple; yet, because the search was only for emails it has a blank after the comma. This I don´t understand and that is what my Python keeps returning a traceback on. I’m not sure where to go from here. The video example returns all the email addresses in the .txt file with a count beside of them.

I don’t know why I’m not getting the same result.

continue has been added but the console still returns a traceback. This time, it is the following:

 File "c:\Users\Admin\Desktop\PY4E\fcc\seq_practice.py", line 22, in <module>
    cur.execute('UPDATE Counts SET count = count + 1 WHERE email = ?'), conn.commit()
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 0 supplied.

I think it´s referring to the UPDATE Counts section but I’m not sure where to go from here.

import sqlite3

conn = sqlite3.connect('emaildb.sqlite')
cur = conn.cursor()

cur.execute('DROP TABLE IF EXISTS Counts')
cur.execute('CREATE TABLE Counts (email TEXT, count INTEGER)')

fname = input('Enter file name: ')
if len(fname) < 1: fname='mbox.txt'
fh = open(fname)
for line in fh:
    if not line.startswith('From '): continue
    pieces = line.split()
    email = pieces[1]
    cur.execute('SELECT count FROM Counts WHERE email = ? ', (email,))
    row = cur.fetchone()
    if row is None:
        cur.execute('''INSERT INTO Counts (email, count) 
            VALUES(?,1)''', (email,))
    else:
        cur.execute('UPDATE Counts SET count = count + 1 WHERE email = ?'), conn.commit()
        
        #http://www.sqlite.org/lang_select.html
sqlstr = 'SELECT email, count FROM Counts ORDER BY count DESC LIMIT 10'
for row in cur.execute(sqlstr):
    print(str(row[0], row[1]))

        
cur.close()

I keep getting this traceback:

  File "c:\Users\Admin\Desktop\PY4E\fcc\seq_practice.py", line 22, in <module>
    cur.execute('UPDATE Counts SET count = count + 1 WHERE email = ?', email,)
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 15 supplied.

What is it referring to when it says “bindings”?

import sqlite3

conn = sqlite3.connect('emaildb.sqlite')
cur = conn.cursor()

cur.execute('DROP TABLE IF EXISTS Counts')
cur.execute('CREATE TABLE Counts (email TEXT, count INTEGER)')

fname = input('Enter file name: ')
if len(fname) < 1: fname='mbox.txt'
fh = open(fname)
for line in fh:
    if not line.startswith('From '): continue
    pieces = line.split()
    email = pieces[1]
    cur.execute('SELECT count FROM Counts WHERE email = ? ', (email,))
    row = cur.fetchone()
    if row is None:
        cur.execute('''INSERT INTO Counts (email, count) 
            VALUES(?,1)''', (email,))
    else:
        cur.execute('UPDATE Counts SET count = count + 1 WHERE email = ?', email,)
        conn.commit()
        
        #http://www.sqlite.org/lang_select.html
sqlstr = 'SELECT email, count FROM Counts ORDER BY count DESC LIMIT 10'
for row in cur.execute(sqlstr):
    print(str(row[0], row[1]))

        
cur.close()

Okay. It was an insufficient amount of parantheses.

It’s working. I just wasn’t paying close enough attention. Thanks for your help.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.