Database Creation

 

  • This program enunciates how to create database in simple way
  •  

  • Sqlite3 is a pacakge which enables the python to provide the connection with SQL Database
  •  

    import sqlite3
    conn = sqlite3.connect('SchoolName.db')
    print ("Database Created")
    conn.execute('CREATE TABLE IF NOT EXISTS SemiAnnual(RollNo integer, Class integer, SName TEXT, Science integer,'
    'Social integer, Maths integer,GK integer)')
    print ("Table Structure Created ")
    conn.close()

     

     

     

    Database Creation - with ENCAPSULATION

     

    Code:

    import sqlite3
    class sqlconnections:
    def __init__(self, database,tablename):
    self.database = database
    self.tablename = tablename
    self.conn = sqlite3.connect(database)
    self.cur = self.conn.cursor()
    print("Connection established with database")
    def CreateTable(self):
    self.cur.execute('CREATE TABLE IF NOT EXISTS {}(RollNo integer,'
    'Class integer, SName TEXT, Science integer,'
    'Social integer, Maths integer,GK integer)'.format(self.tablename))
    print("Table with the name of " + self.tablename + ' has been created')
    #---------------------------------------------------------
    sql = sqlconnections('schoolName.db','semiannual')
    sql.CreateTable()