Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pulling username from Tuple
#11
(Oct-06-2022, 02:48 PM)rob101 Wrote:
(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.

Im not sure if its to do with
 users= cursor.fetchall()
and how python lists the users.
when I print(users) it lists them vertically and not horizontally and in their own ( )
Reply
#12
(Oct-06-2022, 03:09 PM)pajd Wrote: when I print(users) it lists them vertically and not horizontally and in their own ( )

That's why I asked: what's the output of print(users). Don't tell me what the output is; show me.
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
#13
(('rds_superuser_role',), ('mysql.infoschema',), ('mysql.session',), ('mysql.sys',), ('rdsadmin',), ('support',))


seven lines of the above when I use this

sql_GetUser = "select user from mysql.user;"
    cursor.execute(sql_GetUser)
    logger.info("Got a list of users")
    users= cursor.fetchall()
    for user in users:
        print(users)
when I use
                          for userName in users:
                            print(userName)
I get this

('rds_superuser_role', '%')
('mysql.infoschema', 'localhost')
('mysql.session', 'localhost')
('mysql.sys', 'localhost')
('rdsadmin', 'localhost')
('support', 'localhost')
Reply
#14
Okay, try this:

for item in users:
    for user in item:
        if user == 'support':
            print(f"user {user} found.")
pajd likes this post
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
#15
(Oct-06-2022, 03:31 PM)rob101 Wrote: Okay, try this:

for item in users:
    for user in item:
        if user == 'support':
            print(f"user {user} found.")

Thanks a lot that worked Smile
I appreciate your help with this!
rob101 likes this post
Reply
#16
(Oct-06-2022, 03:34 PM)pajd Wrote: Thanks a lot that worked Smile
I appreciate your help with this!

No worries.

What you have there is a nested tuple (tuple within a tuple) which is why you need the double for loop.
pajd likes this post
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
#17
(Oct-06-2022, 03:40 PM)rob101 Wrote:
(Oct-06-2022, 03:34 PM)pajd Wrote: Thanks a lot that worked Smile
I appreciate your help with this!

No worries.

What you have there is a nested tuple (tuple within a tuple) which is why you need the double for loop.

Thanks! It must be how data is pulled from database servers. I'll keep that in mind. Thanks again
Reply
#18
Sorry for the cut/paste error in my prior post. The for loop should be written like this:
for userName in users:
    if userName == 'support':
        print("name is there")
        break
else:   # This else is associated with the "for", not the "if".
    print("user isn't here")
But that wouldn't work either since sql returns results as tuples or lists of tuples. To make it work you need to compare tuple to tuple or str to str.
for userName in users:
    if userName[0] == 'support':  # or userName == ('support',)
        print("name is there")
        break
else:   # This else is associated with the "for", not the "if".
    print("user isn't here")
The correct way to find out if a particular name is in a particular field is to use a query that has a condition. I don't use sqlite3 all that much, so I wrote a little test program to see how that would work.
import sqlite3 as sql
import pandas as pd

database_file = "test.db"


def make_table(table:str, index:str, df:pd.DataFrame):
    """ Make a quick and dirty table for the test """
    conn = sql.connect(database_file)
    df.to_sql(table, conn, index=False, if_exists="replace")
    conn.commit()
    conn.close()


def print_table(table:str):
    """ Print all the rows in the table """
    conn = sql.connect(database_file)
    cur = conn.cursor()
    print(f"Print table {table}")
    for row in cur.execute(f"SELECT * from {table}",):  # No fetch required!
        print(row)
    conn.close()


def print_column(table:str, column:str):
    """ Print all the values in a column """
    conn = sql.connect(database_file)
    print(f"Print table {table}[{column}]")
    for row in conn.execute(f"SELECT {column} from {table}",):  # Makes a cursor for you automatically!
        print(row)
    conn.close()


def find(table:str, column:str, value:any):
    """ Return all rows where row[column] == value """
    conn = sql.connect(database_file)
    cur = conn.cursor()
    matches = cur.execute(f"SELECT * FROM {table} WHERE {column}=?", (value,)).fetchall()
    conn.close()
    return matches


make_table("user", "name", pd.DataFrame({"name":["bob", "mike", "admin", "support"], "role":["user", "user", "admin", "admin"]}))
print_table("user")
print_column("user", "name")
print("Finding user[name] == support,", find("user", "name", "support"))
Output:
Print table user ('bob', 'user') ('mike', 'user') ('admin', 'admin') ('support', 'admin') Print table user[name] ('bob',) ('mike',) ('admin',) ('support',) Finding user[name] == support, [('support', 'admin')]
Of particular interest is the find() function which searches for rows whose column value matches a provided value.
def find(table:str, column:str, value:any):
    conn = sql.connect(database_file)
    cur = conn.cursor()
    matches = cur.execute(f"SELECT * FROM {table} WHERE {column}=?", (value,)).fetchall()
    conn.close()
    return matches
If you had a really big table it would take a long time to download all the rows and then check each row to see if a column's value matches your target value. It takes a lot less time to have the highly optimized database server do the search for you.

To apply this to your problem of finding out if you have a particular user in a table:
matches = cursor.execute("SELECT user FROM mysql.user WHERE user=?", ("support",)).fetchone()
if matches:
    print('name is there')
else:
    print('user isnt here')
There are a few other things you might find interesting. Notice that print_column() doesn't make a cursor. As far as I can tell, the only reason for making a cursor is to use fetchone(), fetchmany() or fetchall(). You don't need a cursor to use execute(), and calling execute returns a cursor object. Cursor may provide other benefits, and explicitely making a cursor is certainly the convention, but it doesn't appear necessary. Interesting, but not all that useful.

More useful is that print_table() and print_column() don't use fetchall(). Instead of fetching results, these functions use a lazy iterator provided by Cursor to retrieve values one at a time.
def print_table(table:str):
    conn = sql.connect(database_file)
    cur = conn.cursor()
    print(f"Print table {table}")
    for row in cur.execute(f"SELECT * from {table}",):  # No need to fetch anything
        print(row)
    conn.close()
This certainly looks more Pythonic, and you don't have to create a big list just to have an something that acts like an iterator.
pajd likes this post
Reply
#19
Wow! Thanks thats impressive.
It certainly made things a bit more complicated due to how python pulls the days from SQL and that's what threw me at the start.
Now that I found the user I now want to delete it. Any tips on that?
I could use this
for item in users:
    for user in item:
        if user == 'support':
            print(f"user {user} found.")
And then some sort of delete command but is there a way to write it so that the name is found so do x but if not found do y?
Reply
#20
@pajd

Just thought of another (maybe cleaner) way:

iterator = iter(users)

for user in iterator:
    if 'support' in user:
        print("Found user name: support")
pajd likes this post
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
  Pulling data from mssql to PG DB hartman60 1 482 Jan-31-2025, 12:26 PM
Last Post: hartman60
  checking username newbexx 3 1,087 Jun-17-2024, 11:42 AM
Last Post: Pedroski55
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 5,667 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
  Pulling Specifics Words/Numbers from String bigpapa 2 1,551 May-01-2023, 07:22 PM
Last Post: bigpapa
  Having trouble installing scikit-learn via VSC and pulling my hair out pythonturtle 1 1,493 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,936 Dec-19-2022, 11:11 PM
Last Post: Stockers
  Hiding username and password on sql tantony 10 6,798 Oct-21-2022, 07:58 PM
Last Post: wavic
  pulling multiple lines from a txt IceJJFish69 3 3,400 Apr-26-2021, 05:56 PM
Last Post: snippsat
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 4,253 Nov-04-2020, 11:26 AM
Last Post: Aggam
  Pulling Information Out of Dictionary Griever 4 3,944 Aug-12-2020, 02:34 PM
Last Post: Griever

Forum Jump:

User Panel Messages

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