Cube
Cube

Reputation: 37

Create function to retrieve ID from table

I'm trying to create a function that retrieves a student's id, as far as I know sqlite doesn't support the creation of functions natively so I used this as a reference, however I can't quite understand how the code works.

Suppose I have the following table: students

id | name
--------------------
1  | Jane Appleseed
2  | John Doe

I tried the following and, of course, it fails:

def getCourseID(s):
    return cur.execute('SELECT id FROM students WHERE name = ? ', (s, ))

What would be the correct way to implement a function such as this?

Thanks for your help.

Upvotes: 0

Views: 73

Answers (1)

Yoav Glazner
Yoav Glazner

Reputation: 8066

Try something like this

def getCourseID(s):
    cur.execute('SELECT id FROM students WHERE name = ? ', (s, ))
    results = list(cur)
    if results:
        return results[0][0]
    return None # nothing was found...

Upvotes: 2

Related Questions