Python and SQLite: Building a Login System Exercise

Table of Contents

  1. Overview
  2. Prerequisites
  3. Setup
  4. Creating a SQLite Database
  5. Creating User Table
  6. Registering a User
  7. Logging in
  8. Conclusion

Overview

In this tutorial, we will learn how to build a simple login system using Python and SQLite. We will create a SQLite database to store user information, and develop a Python program that allows users to register and login. By the end of this tutorial, you will have a basic understanding of how to build a secure login system using Python and SQLite.

Prerequisites

Before starting this tutorial, you should have a basic understanding of Python programming language and some knowledge of relational databases. Additionally, Python 3.x should be installed on your system.

Setup

To begin, we need to install a Python library called sqlite3. This library comes pre-installed with Python, so you don’t need to install it separately.

Creating a SQLite Database

To store user information, we need to create a SQLite database. In this tutorial, we will name our database login_system.db. To create the database, follow these steps:

  1. Open a new Python file and import the sqlite3 module:
     import sqlite3
    
  2. Connect to the database by creating a connection object:
     conn = sqlite3.connect('login_system.db')
    
  3. Create a cursor object to execute SQL commands:
     cursor = conn.cursor()
    
  4. Now, we can execute SQL commands. Let’s create a table to store user information:
     cursor.execute('''CREATE TABLE IF NOT EXISTS users (
                         id INTEGER PRIMARY KEY AUTOINCREMENT,
                         username TEXT NOT NULL,
                         password TEXT NOT NULL
                       )''')
    
  5. Finally, commit the changes and close the connection:
     conn.commit()
     conn.close()
    

    Congratulations! You have successfully created a SQLite database to store user information.

Creating User Table

In this section, we will learn how to create a user table in the SQLite database. The user table will have columns for user ID, username, and password. Follow these steps:

  1. Open the Python file where you created the SQLite database.

  2. Import the sqlite3 module and connect to the database:
     import sqlite3
    	
     conn = sqlite3.connect('login_system.db')
     cursor = conn.cursor()
    
  3. Execute the following SQL command to create the user table:
     cursor.execute('''CREATE TABLE IF NOT EXISTS users (
                         id INTEGER PRIMARY KEY AUTOINCREMENT,
                         username TEXT NOT NULL,
                         password TEXT NOT NULL
                       )''')
    
  4. Commit the changes and close the connection:
     conn.commit()
     conn.close()
    

    Now, you have successfully created a user table in the SQLite database.

Registering a User

In this section, we will implement the user registration functionality. Follow these steps to allow users to register:

  1. Open the Python file where you created the SQLite database and the user table.

  2. Import the required modules:
     import sqlite3
     import getpass
    
  3. Connect to the SQLite database:
     conn = sqlite3.connect('login_system.db')
     cursor = conn.cursor()
    
  4. Define a function to register a user:
     def register():
         username = input("Enter username: ")
         password = getpass.getpass("Enter password: ")
    	
         cursor.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, password))
         conn.commit()
    	
         print("User registered successfully!")
    	
     register()
    
  5. Close the connection:
     conn.close()
    

    Now, users can register by providing a username and password. The information will be stored in the SQLite database.

Logging in

In this section, we will implement the login functionality. Follow these steps to allow users to log in:

  1. Open the Python file where you created the SQLite database and the user table.

  2. Import the required modules:
     import sqlite3
     import getpass
    
  3. Connect to the SQLite database:
     conn = sqlite3.connect('login_system.db')
     cursor = conn.cursor()
    
  4. Define a function to log in:
     def login():
         username = input("Enter username: ")
         password = getpass.getpass("Enter password: ")
    	
         cursor.execute("SELECT * FROM users WHERE username=? AND password=?", (username, password))
         user = cursor.fetchone()
    	
         if user:
             print("Login successful!")
         else:
             print("Invalid username or password.")
    	
     login()
    
  5. Close the connection:
     conn.close()
    

    Now, users can log in by providing their username and password. The program will check if the information exists in the SQLite database and display a success message or an error message accordingly.

Conclusion

In this tutorial, we have learned how to build a simple login system using Python and SQLite. We created a SQLite database, defined a user table, and implemented the registration and login functionalities. You can further enhance this system by adding features like password hashing, email verification, and session management.

Remember, security is crucial when designing a login system. Always use secure practices like password hashing and encryption to protect user data.

Feel free to explore more advanced concepts and libraries to expand the functionality of your login system.