Thursday, December 18, 2025
HomeLanguagesPython SQLite – Create Table

Python SQLite – Create Table

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

In SQLite database we use the following syntax to create a table:

CREATE TABLE database_name.table_name(

                                       column1 datatype PRIMARY KEY(one or more columns),

                                       column2 datatype,

                                       column3 datatype,

                                       …..

                                       columnN datatype

);

Now we will create a table using Python:

Approach:

Import the required module

  • Establish the connection or create a connection object with the database using the connect() function of the sqlite3 module.
  • Create a Cursor object by calling the cursor() method of the Connection object.
  • Form table using the CREATE TABLE statement with the execute() method of the Cursor class.

Implementation:

Python3




import sqlite3
 
# Connecting to sqlite
# connection object
connection_obj = sqlite3.connect('geek.db')
 
# cursor object
cursor_obj = connection_obj.cursor()
 
# Drop the GEEK table if already exists.
cursor_obj.execute("DROP TABLE IF EXISTS GEEK")
 
# Creating table
table = """ CREATE TABLE GEEK (
            Email VARCHAR(255) NOT NULL,
            First_Name CHAR(25) NOT NULL,
            Last_Name CHAR(25),
            Score INT
        ); """
 
cursor_obj.execute(table)
 
print("Table is Ready")
 
# Close the connection
connection_obj.close()


Output:

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32455 POSTS0 COMMENTS
Milvus
108 POSTS0 COMMENTS
Nango Kala
6823 POSTS0 COMMENTS
Nicole Veronica
11958 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12035 POSTS0 COMMENTS
Shaida Kate Naidoo
6957 POSTS0 COMMENTS
Ted Musemwa
7203 POSTS0 COMMENTS
Thapelo Manthata
6910 POSTS0 COMMENTS
Umr Jansen
6890 POSTS0 COMMENTS