0

I have created a database in sqlite which looks like this

employeeID    Name      Last    email                Age
1             Darshan   Malu    darshan@example.com  24
2             Anja      Schmidt anja@example.com     22
3             Neeta     Rajan   neeta@gmail.com      28
4             Michelle  Muller  m@gmail.com          32

So for enquiring by searching for ID I use this code using input as first and last name

c.execute("SELECT employeeID FROM employee WHERE first=:first AND last=:last",{"first":first,"last":last})
print('Your Employee ID is', c.fetchone())
conn.close 

Which does it work.

It returns me "Your Employee ID is (1,)"

So question is

  1. Why is there a ',' after the ID
  2. How to get rid of both , and ()? so that it just prints out "Your Employee ID is 1"
  • 4
    Because queries always return _tuples_ of results. Just `print(c.fetchone()[0])`. Also, not that `conn.close` does not close the connection because you didn't call the method – roganjosh Sep 02 '20 at 09:53
  • Thanks a lot. It worked. Can you please write this as answer so that I would mark the question as solved – Darshan Malu Sep 02 '20 at 09:55
  • 1
    I'm currently trying to find a dupe because this is a common issue. If someone else wants to answer, fine – roganjosh Sep 02 '20 at 09:58

1 Answers1

1

There are several ways to fetch the results. featchall() will return a list of tuple like [(1,)], and fetchone() will return only the first value of the list which is (1,) so if you just want to get 1 you need to use call fetchone()[0].

And, conn.close() not just conn.close

For inserting or deleting call this:

conn.commit()
conn.close()
Jacob
  • 602
  • 4
  • 25