[Tutor] List problem

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Thu, 3 May 2001 00:57:36 -0700 (PDT)


On Thu, 3 May 2001, Sharriff Aina wrote:

> Thanks Mr. Yoo for replying

You don't need to call me Mr. Yoo; just Danny is fine for me.  (Plus, if
you saw me, I'm definitely not a "Mr." quite yet.  *grin*)


> connection = odbc.odbc('minicms')
> cur = connection.cursor()
> cur.execute("select * from users where username='%s' and userpwd='%s'"
> %(usernamevalue,passwordvalue))
> allrows = cur.fetchall()

Ah!  But what happens if the user DID misspell either their username or
password?  In SQL, this means that the result set should be of length 0,
because it can't find a user with those qualities.  Let's check the
documentation:

    http://python.org/topics/database/DatabaseAPI-2.0.html

According to the docs:

    "fetchall(): Fetch all (remaining) rows of a query result, returning
    them as a sequence of sequences (e.g. a list of tuples). Note that the
    cursor's arraysize attribute can affect the performance of this
    operation.

    An Error (or subclass) exception is raised if the previous call to
    executeXXX() did not produce any result set or no call was issued
    yet."

Since we aren't getting an exception out of the system, we'll need to
assume that we got an empty result set from the fetchall() instead.  It
appears that the database found no matches for the query, so it assigned

     allrows = []

Can you verify this?  You might want to add a check like:

###
    if allrows == []:
        # output something about "Incorrect password or username.  Try
        # again"
    else:
        # now it's ok to start indicing allrows[0].
###

right before you start indexing your allrows; we can't be certain that
allrows[0] works until we check to see that we have at least one tuple in
the list.  Afterwards, it should be ok to check for allrows[0].

Try it out, and see if this is the bug that you're running into.  If so,
it should be fairly easy to fix.

Good luck!