import sqlite3
my_conn = sqlite3.connect('test_db')
print("Connected to database successfully")
try:
my_conn.execute('''DROP table student;''')
print("student table deleted")
except sqlite3.Error as my_error:
print("error: ",my_error)
my_conn.commit()
try:
my_conn.execute('''
CREATE TABLE IF NOT EXISTS student(id integer primary key,
name text,
class text,
mark integer,
sex text,
CONSTRAINT my_Check CHECK(mark >= 10)
);''')
my_conn.commit()
print("Student Table created successfully")
except sqlite3.Error as my_error:
print("error: ",my_error)
my_conn.commit()
try:
my_conn.execute(" DELETE FROM student ")
my_conn.commit()
print("All records of Student Table deleted")
except sqlite3.Error as my_error:
print("error: ",my_error)
my_conn.commit()
# mark column value your can change to check the constraint
try:
my_conn.execute("INSERT INTO student (id,name,class,mark,sex) VALUES \
(1,'John Deo','Four',10,'female')")
my_conn.commit()
print("Student added successfully")
except sqlite3.Error as my_error:
print("error: ",my_error)
try:
my_conn.execute("UPDATE student SET mark=mark-90")
my_conn.commit()
print("Student updated successfully")
except sqlite3.Error as my_error:
print("error: ",my_error)
my_conn.commit()
# displaying record from student table
r_set=my_conn.execute('''select count(*) from student ''')
for row in r_set:
print(row)
r_set=my_conn.execute('''SELECT * from student''');
for row in r_set:
print(row)
# Changing CHECK constraint by creating new table
# Copying data from old table to new table
# Deleting old table
# Renaming new table to old table.
try:
my_conn.execute("BEGIN")
my_conn.execute("CREATE TABLE IF NOT EXISTS \
student_new(id integer primary key, \
name text, \
class text, \
mark integer, \
sex text,\
CONSTRAINT my_Check CHECK(mark >= 10 AND mark <=100))")
my_conn.execute("INSERT INTO student_new SELECT * FROM student")
my_conn.execute("DROP table student")
my_conn.execute("ALTER TABLE student_new RENAME TO student")
my_conn.commit()
print("Student constraint updated successfully")
except sqlite3.Error as my_error:
print("error: ",my_error)
my_conn.commit()
# mark=100 is allowed but mark =101 is not allowed.
try:
r_set=my_conn.execute("UPDATE student SET mark=101")
print("Records Updated : ",r_set.rowcount)
my_conn.commit()
#print("Student updated successfully")
except sqlite3.Error as my_error:
print("error: ",my_error)
my_conn.commit()
r_set=my_conn.execute('''SELECT * from student''');
for row in r_set:
print(row)
# List all tables
r_set=my_conn.execute('''select name from sqlite_master where type = 'table' ''')
for row in r_set:
print(row)
# Changing CHECK constraint by creating new table
# Copying data from old table to new table
# Deleting old table
# Renaming new table to old table.
try:
my_conn.execute("BEGIN")
my_conn.execute("CREATE TABLE IF NOT EXISTS \
student_new(id integer primary key, \
name text, \
class text, \
mark integer, \
sex text,\
CONSTRAINT my_Check CHECK(mark >= 10 AND mark <=100),\
CONSTRAINT my_check2 CHECK(class in('One','Two','Three')))")
my_conn.execute("INSERT INTO student_new SELECT * FROM student")
my_conn.execute("DROP table student")
my_conn.execute("ALTER TABLE student_new RENAME TO student")
my_conn.commit()
print("Student constraint updated successfully")
except sqlite3.Error as my_error:
print("error: ",my_error)
my_conn.commit()
my_data=('Deo Greek','Six',35,'male') # tuple to pass values to execute method
q="INSERT INTO student (name,class,mark,sex) values (?,?,?,?)"
try:
my_cursor=my_conn.execute(q,my_data)
my_conn.commit()
for row in my_cursor:
print(row[0],row[1],row[2],row[3],row[4])
except sqlite3.Error as my_error:
print("error: ",my_error)