mikeizbicki / cmc-csci040

Computing for the Web
36 stars 59 forks source link

Problem 5c Error #306

Open xutimon opened 1 year ago

xutimon commented 1 year ago

Hi Mike,

When I try to run practice problem 5c in Python, it spits out the following error

MacBook-Pro-8:CS40 timonxu$ /usr/local/bin/python3 /Users/timonxu/Desktop/CS40/problem5c.py
Traceback (most recent call last):
  File "/Users/timonxu/Desktop/CS40/problem5c.py", line 12, in <module>
    cur.execute(sql, username)
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 24 supplied.

I have deleted twitter_clone.db and re-ran the db_create.py multiple times to ensure that the database is being reset, but the error persists. I was just wondering if the error here is a feature of the problem or an actual error that wasn't supposed to arise. Thanks!

xutimon commented 1 year ago

The code I am running is the exact same as problem 5c as shown, but I will also attach it below.

# Problem 5b

import sqlite3

con = sqlite3.connect('twitter_clone.db')
cur = con.cursor()

username = "Mike' OR username='Trump"
sql = """
SELECT id,password FROM users WHERE username=?;
"""
cur.execute(sql, username)
user_ids = []
for row in cur.fetchall():
    user_ids.append(row[0])

for user_id in user_ids:
    sql = """
    SELECT count(*) FROM messages WHERE sender_id=?;
    """
    cur.execute(sql, [user_id])
    for row in cur.fetchall():
        print('row[0]=', row[0])
mikeizbicki commented 1 year ago

Sorry, a fixed version has been uploaded. The problem was that line 12 previously read

cur.execute(sql, username)

and should read

cur.execute(sql, [username])

with square brackets []. Whenever you are passing in the "bindings" for the values represented by ?, the bindings need to be inside of a list even if there is only one value.

xutimon commented 1 year ago

Hi Mike,

Quick follow-up to my previous issue, I have tried to run the new code but there doesn't seem to be any output. I used the same debugging processing as above (resetting the database) and the output is simply blank with no errors. The code for 5c is also directly downloaded from github with no changes, so there shouldn't be any errors there. Just curious if this was the answer or if this is an error. Thanks!

mikeizbicki commented 1 year ago

That's correct. The program has no output. If you look at the username it is searching for, you should see that it is not in the database.