Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pulling username from Tuple
#1
Hi,
Im connecting to a DB in AWS and want to check if a certain username is on the server

cursor.fecthall () copies the usernames and stores them as a tuple
Is there a way I can search for the username in the Tuple
user in userNames prints out the username 'support' but Im not sure the best way to then search for them
Ive tried a couple of options but none work for me. Being new to Python I thought Id ask on here

If I could search the tuple for a user called 'support' can anyone tell me how its done?

Im assuming fectchall () returns a Tuple although some docs say its a list
I get this returned

('mysql.infoschema', 'localhost')
('mysql.session', 'localhost')
('mysql.sys', 'localhost')
('admin', 'localhost')
('support', 'localhost')

Ive tried

for userName in users:
     if 'support' in users:
          print('name is there')
    else:
          print('user isnt here')
However that just returns the else statement even though the user is there





    
    connection = pymysql.connect(
    host=db_secrets['host'],
    user=db_secrets['username'],
    password=db_secrets['password'],
    database=db_secrets['dbname']
    )
    cursor = connection.cursor()
    logger.info("SUCCESS: Connection to RDS MySQL instance succeeded")
   
    
    sql_GetUser = "select user, host from mysql.user;"
    cursor.execute(sql_GetUser)
    logger.info("Got a list of users")
    users= cursor.fetchall ()
    print("User List:")
    for userName in users:
        print(userName)
        
    connection.close()
    print("The connection is closed")
    cursor.close()
    logger.info("Successfully closed the cursor")
Reply
#2
(Oct-06-2022, 01:57 PM)pajd Wrote: Ive tried
for userName in users:
    if 'support' in users:
        print('name is there')
    else:
        print('user isnt here')
However that just returns the else statement even though the user is there

Untested, but...
With that for loop, it seems to me that userName is the object to test: if userName == 'support':
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
[/quote]

Untested, but...
With that for loop, it seems to me that userName is the object to test: if userName == 'support':
[/quote]

Didnt work with this

sql_GetUser = "select user, host from mysql.user;"
    cursor.execute(sql_GetUser)
    logger.info("Got a list of users")
    users= cursor.fetchall ()
    print("User List:")
    for userName in users:
        if userName == 'support':
            print('name is there')
        else:
            print('user isnt here')
Result was

User List:
user isnt here
user isnt here
user isnt here
user isnt here
user isnt here
user isnt here
user isnt here
user isnt here
user isnt here
user isnt here
user isnt here
Reply
#4
What's the output of print(users)
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#5
No need to use a for loop. Use in.
if userName in users:
    print('name is there')
else:
    print('user isnt here')
Reply
#6
print(users)
The ouput is a loop (7 lines) with all users in each line
Reply
#7
(Oct-06-2022, 02:23 PM)deanhystad Wrote: No need to use a for loop. Use in.
if userName in users:
    print('name is there')
else:
    print('user isnt here')
If you want to use a for loop, you need to check all the names before printing 'user isn\'t here' (or "user isn't here").
for userName in users:
    if 'support' in users:
        print("name is there")
        break
else:   # This else is associated with the "for", not the "if".
    print("user isn't here")

I used this and still get the else result even though the username is in the list

    sql_GetUser = "select user from mysql.user;"
    cursor.execute(sql_GetUser)
    logger.info("Got a list of users")
    users= cursor.fetchall ()
    if 'support' in users:
        print('name is there')
    else:
        print('user isnt here')
and

for userName in users:

is invalid syntax
Reply
#8
Okay; lets put it another way:

What's the output for...
for user in users:
    print(user)
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#9
(Oct-06-2022, 02:31 PM)rob101 Wrote: Okay; lets put it another way:

What's the output for...
for user in users:
    print(user)

It lists all the users


and the else gets returned by this

for user in users:
       print(user)
    if user == 'support':
        print("user is here")
    else:
        print("user is not here")
Reply
#10
(Oct-06-2022, 02:36 PM)pajd Wrote: It lists all the users

Well in that case, there's no reason for if user == 'whatever': not to work.

users = ('bob','mike','admin','support')

for user in users:
    if user == 'support':
        print(f"user {user} found.")
Output:
user support found.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 1,758 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
  Pulling Specifics Words/Numbers from String bigpapa 2 773 May-01-2023, 07:22 PM
Last Post: bigpapa
  Having trouble installing scikit-learn via VSC and pulling my hair out pythonturtle 1 768 Feb-07-2023, 02:23 AM
Last Post: Larz60+
  (Python) Pulling data from UA Google Analytics with more than 100k rows into csv. Stockers 0 1,241 Dec-19-2022, 11:11 PM
Last Post: Stockers
  Hiding username and password on sql tantony 10 2,953 Oct-21-2022, 07:58 PM
Last Post: wavic
  pulling multiple lines from a txt IceJJFish69 3 2,593 Apr-26-2021, 05:56 PM
Last Post: snippsat
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,847 Nov-04-2020, 11:26 AM
Last Post: Aggam
  Pulling Information Out of Dictionary Griever 4 2,903 Aug-12-2020, 02:34 PM
Last Post: Griever
  Trying to create a conditional with a username Realen 2 1,834 Jun-20-2020, 12:44 AM
Last Post: Realen
  Client OS username ImPyBoy17 5 2,708 Sep-24-2019, 10:18 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020