Python and SQLite: check if an item exists in the database?

I have a method that writes the username and password of the user who wants to register in the database. Before storing the username and password they provided in the database, I want to check if the username already selected exists in the "pending" list or in the "approved" contacts list.

Here is the code I used to do this:

@cherrypy.expose
def writePending(self, username=None, password=None, message=None, signin=None):
    """ Add request of friendship into a database which stores all
        pending friendships.
    """

    page = get_file(staticfolder + "/html/friendingPage.html")

    if username != "" and password != "" and message !="":
        con = lite.connect('static/database/Friendship.db')
        cur = con.cursor()

        with con:      
            cur.execute("CREATE TABLE IF NOT EXISTS pending(user TEXT, pass TEXT, info TEXT)")
            cur.execute("CREATE TABLE IF NOT EXISTS contacts(user TEXT, pass TEXT)")

            "Check to see if the username is already registered"

            cur.execute("Select * from pending where user = ?", (username, ))
            check1=cur.fetchone()
            cur.execute("Select * from contacts where user = ?", (username, ))
            check2=cur.fetchone()

            if check1[0] != None:
                page = page.replace("$Error", "The ID you used is still pending for friendship")
            elif check2[0] != None:
                page = page.replace("$Error", "The ID you used is already added as a contact")
            else:
                cur.execute("CREATE TABLE IF NOT EXISTS pending(user TEXT, pass TEXT, info TEXT)")   
                cur.execute("INSERT INTO pending VALUES(?, ?, ?)", (username, password, message))               
                page = get_file(staticfolder + "/html/thankYouPage.html")

    else:
        page = get_file(staticfolder + "/html/friendingPage.html")
        page = page.replace("$Error", "You Must fill out all fields to proceed")

    return page

      

However, I get a message that

Traceback (most recent call last):
  File "/usr/lib/pymodules/python2.7/cherrypy/_cprequest.py", line 606, in respond
    cherrypy.response.body = self.handler()
  File "/usr/lib/pymodules/python2.7/cherrypy/_cpdispatch.py", line 25, in __call__
    return self.callable(*self.args, **self.kwargs)
  File "proj1base.py", line 540, in writePending
    if type(check1[0]) != None:
TypeError: 'NoneType' object is not subscriptable

      

I am wondering what can I do to avoid this?

Thank.

+3


source to share


2 answers


Your example check1

will None

, so you can't use [0]

on it. You can do something like this:

if check1 is not None:
    (error response)

      



Or instead, just use cur.rowcount

instead cur.fetchone()

:

if cur.rowcount > 0:
    (error response)

      

+6


source


fetchone()

returns None

if there is no row. You can do the following:



if check1:
    ...do something with check1, like check1[0]...
else:
    .. means no row

      

+1


source







All Articles