Changing the name of the table

The syntax of ALTER TABLE to change the name of the table in SQLite is given below:

ALTER TABLE table_name RENAME TO newTableName;

We will use the same GEEK table that we created above:

Python3




import sqlite3
 
# Connecting to sqlite
connection_obj = sqlite3.connect('geek.db')
 
# cursor object
cursor_obj = connection_obj.cursor()
 
# select from sqlite_master
cursor_obj.execute("SELECT * FROM sqlite_master")
 
table = cursor_obj.fetchall()
print("Before changing the name of Table")
print("The name of the table:", table[0][2])
 
# Rename the SQLite Table
renameTable = "ALTER TABLE GEEK RENAME TO GFG"
cursor_obj.execute(renameTable)
 
 
# select from sqlite_master
cursor_obj.execute("SELECT * FROM sqlite_master")
 
table = cursor_obj.fetchall()
 
print("After changing the name of Table")
print("The name of the table:", table[0][2])
 
connection_obj.commit()
 
connection_obj.close()


Output:



How to Alter a SQLite Table using Python ?

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

We can do this by using ALTER statement. It allows to:

  • Add one or more column to the table

Similar Reads

Change the name of the table.

Adding a column to a table...

Changing the name of the table

...