A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-sqlite-create-table/ below:

Python SQLite - Create Table

Python SQLite - Create Table

Last Updated : 01 Jul, 2025

In this article, we will discuss how can we create tables in the SQLite database from the Python program using the sqlite3 module. 

Syntax

CREATE TABLE table_name (

column1 datatype PRIMARY KEY,
column2 datatype,
column3 datatype,
...
columnN datatype

);

Steps to Create a Table in SQLite using Python
  1. Import the SQLite3 Module: Use import sqlite3 to access SQLite functionality in Python.
  2. Establish Connection: Use the connect() method to establish a connection to your SQLite database.
  3. Create a Cursor Object: The cursor() method creates a cursor object that allows you to execute SQL commands.
  4. Execute SQL Query: The execute() method of the cursor object is used to run the SQL CREATE TABLE command.
  5. Close the Connection: After executing the required commands, it is essential to close the connection to the database.

Implementation:

Python
import sqlite3

# Connect to the SQLite database (or create it if it doesn't exist)
connection_obj = sqlite3.connect('geek.db')

# Create a cursor object to interact with the database
cursor_obj = connection_obj.cursor()

# Drop the GEEK table if it already exists (for clean setup)
cursor_obj.execute("DROP TABLE IF EXISTS GEEK")

# SQL query to create the table
table_creation_query = """
    CREATE TABLE GEEK (
        Email VARCHAR(255) NOT NULL,
        First_Name CHAR(25) NOT NULL,
        Last_Name CHAR(25),
        Score INT
    );
"""

# Execute the table creation query
cursor_obj.execute(table_creation_query)

# Confirm that the table has been created
print("Table is Ready")

# Close the connection to the database
connection_obj.close()

Output:

Explanation:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4