Python Practice › SQLite Programs › Program 1
Create a table
Write a Python program to create a SQLite table and list the tables in the database.
Output
[('students',)]Approach
CREATE TABLE, then querysqlite_master.
Solution
import sqlite3 conn = sqlite3.connect(":memory:")conn.execute("CREATE TABLE students (id INTEGER PRIMARY KEY, name TEXT NOT NULL, marks INTEGER)")print(conn.execute("SELECT name FROM sqlite_master WHERE type = 'table'").fetchall())Output
[('students',)]Line by line
Line 3
:memory: creates a temporary database that disappears when the program ends.
Learn the concept
New to this topic? Read the lesson, then come back.