Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7675177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:54:09+00:00 2026-05-31T16:54:09+00:00

At the moment, I have a database which contains username, password, etc. I am

  • 0

At the moment, I have a database which contains username, password, etc.
I am wishing to look into the database to check if duplicates are in there.

con = lite.connect('userInfo.db')
with con:
    cur = con.cursor()
    cur.execute("SELECT * FROM Users WHERE LOWER(Username) = LOWER(?)", (newID,))
    rows = cur.fetchall()
    if len(rows)!=0:
        return "Duplicate detected"

Here is my code at the moment. newID is a new name and I wish to check if there are any existing entries in the database with the same name.

My question is – is the way I am doing it in my code a good idea? I’m mainly concerned with my approach. Should I be using something other than fetchall() ?

Thank you for your time! 🙂
P.S. This is for a website application.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:54:10+00:00Added an answer on May 31, 2026 at 4:54 pm

    Here’s a way to do exactly what you asked for – find out if a given username already exists:

    import sqlite3
    conn = sqlite3.connect(":memory:")
    conn.execute ("""
                  CREATE TABLE users (
                      uid INTEGER PRIMARY KEY AUTOINCREMENT,
                      username TEXT UNIQUE,
                      email TEXT UNIQUE );
                 """)
    
    test_users = (
        {'username':"Alice",    'email':"Alice@mail.com"},
        {'username':"Billy",    'email':"Billy@mail.com"},
        {'username':"Charles",  'email':"Charles@mail.com"},
        {'username':"Dick",     'email':"Dick@mail.com"},
        {'username':"Emily",    'email':"Emily@mail.com"},
        {'username':"Faramir",  'email':"Faramir@mail.com"},
    )
    
    for user in test_users:
        conn.execute("INSERT INTO users (username, email) VALUES (?,?)",
                     (user['username'],user['email'])
                    )
    
    result = conn.execute("SELECT COUNT(*) FROM users WHERE username='Alice'")
    number_of_Alices = result.next()[0] # number_of_Alices will be 1
    

    Since all you want is a COUNT this is adequate.


    Really, though, you shouldn’t be enforcing the uniqueness of the usernames yourself. Let the database do that for you by specifying the field to be either UNIQUE or PRIMARY KEY.

    If you try to insert "Alice", "alice@wonderland.com" after creating the database like above, this will get you an sqlite3.IntegrityError:

    >>> conn.execute("""INSERT INTO users (username, email)
    ...                     VALUES ("Alice", "alice@wonderland.com");""")
    Traceback (most recent call last):
      File "<stdin>", line 2, in <module>
    sqlite3.IntegrityError: column username is not unique
    

    To detect this, try to run the INSERT and detect whether it fails.

    try:
        conn.execute("""INSERT INTO users (username, email)
                        VALUES ("Alice", "alice@wonderland.com");""")
    except sqlite3.IntegrityError:
        print ("Username 'Alice' was already taken.")
    

    Incidentally, be very careful with using the upper/lowercase functions. Does "Главное в новостях".lower() mean what you think it means?


    Since you mention this is for a webapp, I’ll just remind you to store your passwords as salted hashes of the password, using unique salts for each user (never as plain text!), and to guard against SQL injection by using the (?,?,?,?,...) placeholders for SQL queries, not the (%s,%s) % (var1, var2) string interpolation method.

    To quote the sqlite3 documentation:

    Usually your SQL operations will need to use values from Python
    variables. You shouldn’t assemble your query using Python’s string
    operations because doing so is insecure; it makes your program
    vulnerable to an SQL injection attack.

    Instead, use the DB-API’s parameter substitution. Put ? as a
    placeholder wherever you want to use a value, and then provide a tuple
    of values as the second argument to the cursor’s execute() method.
    (Other database modules may use a different placeholder, such as %s or
    :1.) For example:

    If you don’t do this, then someone could request the username Robert Menzies; DROP TABLE users; with hilarious results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mysql database which contains several tables. At the moment i'm using
I'm not sure how to do this... I have a database which contains a
At the moment I have a database field which stores passwords like this: TeacherPassword
I have a database (NexusDB (supposedly SQL-92 compliant)) which contains and Item table, a
So, at the moment I have a spinner which is populated from a database
In my database field I have a Positions field, which contains a space separated
I have a database for my personal site and at the moment it is
I have a database containing a single huge table. At the moment a query
At the moment I have a slider and an small input text box which
At the moment I have a jQuery do a POST to a Controller which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.