In [88]:
import sqlite3
my_conn = sqlite3.connect('test_db')
print("Connected to database successfully")
Connected to database successfully
In [141]:
try:
  my_conn.execute('''DROP table student;''')
  print("student table deleted")
except sqlite3.Error as my_error:
  print("error: ",my_error) 
my_conn.commit()
student table deleted
In [142]:
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()
Student Table created successfully
In [138]:
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()
All records of Student Table deleted
In [143]:
# 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) 
Student added  successfully
In [106]:
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()
error:  CHECK constraint failed: my_Check
In [107]:
# 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)
(1,)
(1, 'John Deo', 'Four', 10, 'female')
In [130]:
# 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()
Student constraint updated successfully
In [111]:
# 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()
error:  CHECK constraint failed: my_Check
In [135]:
r_set=my_conn.execute('''SELECT * from student''');
for row in r_set:
    print(row)
(1, 'Deo Greek', 'Six', 35, 'male')
(2, 'Deo Greek', 'Six', 35, 'male')
In [136]:
# List all tables 
r_set=my_conn.execute('''select name from sqlite_master where type = 'table' ''')
for row in r_set:
    print(row)
('student',)
In [144]:
# 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()
error:  CHECK constraint failed: my_check2
In [140]:
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)
error:  CHECK constraint failed: my_check2