How to Create Tables in Python?

Category: PythonHow to Create Tables in Python?
Editor">Editor Staff asked 2 years ago

How to Create Tables in Python?

1 Answers
Editor">Editor Staff answered 2 years ago

sqlite3 is available by default in Python standard library and hence there is no need of installing it separately.
SQLite is a very lightweight database. You can connect to it directly, with minimal settings.
Creating a Sample Table
Example 1
import sqlite3
# establishing a database connection
con = sqlite3.connect(‘D:\\TEST.db’)
# preparing a cursor object
cursor = con.cursor()
# preparing sql statements
sql1 = ‘DROP TABLE IF EXISTS EMPLOYEE’
 

Creating a Sample Table…
Example 1…

sql2 = ”’
CREATE TABLE EMPLOYEE (
EMPID INT(6) NOT NULL,
NAME CHAR(20) NOT NULL,
AGE INT,
SEX CHAR(1),
INCOME FLOAT
)
”’
# executing sql statements
cursor.execute(sql1)
cursor.execute(sql2)

# closing the connection
con.close()
You can run and verify if TEST.db file got created at the specified location.