Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tuple and formating problem
#1
Hi
Im using sqlite3 for a lite project and im getting this error

Error:
conn.execute('SELECT * FROM anställda WHERE ID={0},{1},{2},{3}'.format(uid[:4])) IndexError: tuple index out of range
i have no idea how to fix it. I think the fault lies in the formating or something. Could anyone take a look at the code and help pls.

    # Database  stuff
    #dataid = (uid[:4])
    conn.execute('SELECT * FROM anställda WHERE ID={0},{1},{2},{3}'.format(uid[:4]))
    data = cursor.fetchall()
    #print(data)
    for row in data:
        version = (row[0]) #<----siffran bestämmer column
        print(version)
Reply
#2
sql = "SELECT * FROM anställda WHERE ID = ?"
data = (uid[:4],) # you need the comma as it expects a tuple!

conn.execute(sql, data)

does this work? I am unsure how uid is used and why you have 4 values in the "Where" part.
Reply
#3
Hey
The uid is a rfid tag code when i read the tag i get a nummber like this 150, 110, 1, 164 .
So what im trying to do is to scan the tag and then look up the nummber in the database.

Here is how i do it when im not using a db.

    # UID stuff
    if status == MIFAREReader.MI_OK:
     # Print UID
      print "Card read UID: {0},{1},{2},{3}".format(*uid)
      print (st)
      if uid[:4] == [150,110,1,164]:
        Print ("Hi there")
      time.sleep(2)
Reply
#4
How is the uid stored in the database? is it one column of varchar, or four separate numbers?
Reply
#5
Its stored in one column of TEXT.
Reply
#6
If it is in the same form as "150, 110, 1, 164":

(assumes uid is a string)

sql = "SELECT * FROM anställda WHERE ID = ?"
data = (uid,) # you need the comma as it expects a tuple!

conn.execute(sql, data)

Does this work?
Reply
#7
I dont understand the code you wrote.
Reply
#8
Okay, so the uid is a list of numbers, right? In which case we need to make them a string and check it against the db, so to adjust the above code:

for e.g. uid = [150,110,1,164]

uid_str = ','.join([str(i) for i in uid])

then we create the sql for the query:

sql_str = "SELECT * FROM anställda WHERE ID = ?"

and the data for the query is now the uid_str we created from the list of numbers (i.e. "150,110,1,64"

so we use that as the data and pass it to the execute function:

conn.execute(sql_str, uid_str)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Formating generated .data file to XML malcoverc 3 1,315 Apr-14-2022, 09:41 PM
Last Post: malcoverc
  propper formating paracelsusx 2 1,861 Jul-16-2021, 09:17 AM
Last Post: perfringo
  Adding graph points and formating project_science 4 2,345 Jan-24-2021, 05:02 PM
Last Post: project_science
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,725 Nov-04-2020, 11:26 AM
Last Post: Aggam
  How to get first line of a tuple and the third item in its tuple. Need Help, Anybody? SukhmeetSingh 5 3,117 May-21-2019, 11:39 AM
Last Post: avorane
  pymysql: formating ouput of query wardancer84 18 8,176 Oct-04-2018, 01:54 PM
Last Post: wardancer84
  Problem between list and tuple PierreSoulier 2 2,756 Jul-19-2018, 12:40 PM
Last Post: PierreSoulier
  Tuple Space with lindypy Problem draems 0 2,539 Sep-19-2017, 07:00 AM
Last Post: draems
  output formating mcmxl22 2 3,044 Jun-19-2017, 07:21 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020