Вопрос

checkSql = 'SELECT userid FROM bs_members WHERE userid = :checkUser'
doesUserExist = False
while True:
    doesUserExist = False
    newUser.userID = ga.getInput('Enter userID: ', "\w+$")
    checkUserID = ds.execute(checkSql,checkUser=newUser.userID)
    for row in ds:
        if row == checkUserID:
            doesUserExist = True
            print 'That user name is already in use.  Please enter a new username.'
            break
    if doesUserExist == False:
        break
    else:
        continue

I am using the cx_Oracle module with Python 2.7. I am trying to prompt the user to enter a userID. The program will then check if the userID already exists and if it does prompt the user for a different userID. The execute method is a helper method that uses the execute method from cx_Oracle to interact with the Oracle database. The getInput method prompts the user for input that is then checked against the regular expression.

I know I have this wrong but I believe the while loop starts the first action that is taken is the user is prompted for a userID. Then the userID is checked against the database. The for loop starts and checks if the row returned by ds.execute() is the same as the userID provided by the user. If it is the user is told to use another user name and the break exits the for loop. The if statement then checks if a user exists and if it doesn't it breaks the while loop. If not then the while loop iterates so the user is prompted to enter a non-existent userID.

What happens is the user is prompted for the userID then none of the checking ever appears to happen to the user and the program moves on to the next piece of code. What am I missing here? I have included a link to the docs for execute(). The execute method in the above code is part of the following helper method:

def execute(self, statement, **parameters):
    if parameters is None:
        self._curs.execute(statement)
    else:
        self._curs.execute(statement,parameters)

If I need to provide more information let me know.

edit: I forgot the line doesUserExist = False immediately after the beginning of the while loop so I added that.

Это было полезно?

Решение

Your custom execute method doesn't return anything meaning that checkUserID in your code will be None.

Furthermore, what you're interested in is if there's at least one row returned by the query. If there are none then the userID should be available.

The docs say that calling .fetchone() returns None if no more rows are available. You can use that.

checkSql = 'SELECT userid FROM bs_members WHERE userid = :checkUser'
while True:
    newUser.userID = ga.getInput('Enter userID: ', "\w+$")
    ds.execute(checkSql,checkUser=newUser.userID)
    if ds.fetchone() is None:
        # This userID is available.
        break
    else:
        print 'That user name is already in use.  Please enter a new username.'

I'm assuming here that ds is an instance of Cursor, or subclass thereof.

Другие советы

At least you should have line doesUserExist = False at the beginning of the while loop. Otherwise, if user enters an existing ID once then it will keep looping forever.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top