1

I have a user input where I want to enter a userID and retrieve the songID's that are asscociated with that user. I'm having trouble with this because of errors around the cursor.

enter image description here

Database that im trying to retrieve from.

import pyodbc    
import MySQLdb
import sqlite3 

MusicData = sqlite3.connect("D:\lastfm-dataset-360K\msd.sqlite3")

randomVariable = raw_input('Enter something: ')
cursor = MusicData.cursor()
MusicData = "SELECT songID FROM train WHERE userID=?"
result = cursor.execute(MusicData,randomVariable)
print result

When i enter the userID i expect to get the SongID's associated with that user however I get:

---------------------------------------------------------------------------
ProgrammingError                          Traceback (most recent call last)
<ipython-input-16-edd40392d38f> in <module>()
     25 cursor = MusicData.cursor()
     26 MusicData = "SELECT songID FROM train WHERE userID=?"
---> 27 result = cursor.execute(MusicData,randomVariable)
     28 print result
     29 

ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 40 supplied.
rioV8
  • 24,506
  • 3
  • 32
  • 49
T.Gilmour
  • 43
  • 2
  • 12

1 Answers1

3

Should already be answered here: sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 74 supplied

The short of it though, is the second parameter in your execute call is expected to be a tuple instead of just the value, so wrap randomVariable as follows:

cursor.execute(MusicData,(randomVariable,))

Hope this helps!

Why it thinks that you supplied 40 parameters is because it is iterating over your input string thinking each character is a parameter you meant to fill in.

OsmosisJonesLoL
  • 244
  • 1
  • 4
  • Thankyou for the help and explanation! the only problem is now that I get the error: "AttributeError: 'str' object has no attribute 'cursor'". Im using jupyter notebook and it doesnt give the error the first time I freshly run the program and i can query the database but if i input it again it throws the error. – T.Gilmour Apr 05 '19 at 14:41
  • 2
    @T.Gilmour Using the same variable for the database connection and a string is a bad idea... – Shawn Apr 05 '19 at 15:41