Library Management


In [1]:
import sqlite3

In [2]:
conn= sqlite3.connect('ibrary_data.sqlite3')

In [3]:
cur= conn.cursor()

In [4]:
cur.execute('CREATE TABLE books(id integer primary key, name text,isbn text)')


---------------------------------------------------------------------------
OperationalError                          Traceback (most recent call last)
<ipython-input-4-6f65c23c29b4> in <module>()
----> 1 cur.execute('CREATE TABLE books(id integer primary key, name text,isbn text)')

OperationalError: table books already exists

In [5]:
cur.execute('INSERT INTO books(id,name,isbn)VALUES(1,"Basic Mathematics","abc-123")')


---------------------------------------------------------------------------
IntegrityError                            Traceback (most recent call last)
<ipython-input-5-f5163bbfa313> in <module>()
----> 1 cur.execute('INSERT INTO books(id,name,isbn)VALUES(1,"Basic Mathematics","abc-123")')

IntegrityError: UNIQUE constraint failed: books.id

In [7]:
cur.fetchall()


Out[7]:
[]

In [8]:
cur.execute('SELECT * FROM books')


Out[8]:
<sqlite3.Cursor at 0x174fc68ff80>

In [9]:
cur.fetchall()


Out[9]:
[(1, 'Basic Mathematics', 'abc-123'),
 (2, 'Programming With C', 'a-1234'),
 (3, 'Basic Electrical Engineering', 'bee-865'),
 (4, 'Applied Mechanics', 'appm-986')]

In [10]:
cur.execute('CREATE TABLE students(id integer primary key, name text, faculty text)')


---------------------------------------------------------------------------
OperationalError                          Traceback (most recent call last)
<ipython-input-10-79cee48148eb> in <module>()
----> 1 cur.execute('CREATE TABLE students(id integer primary key, name text, faculty text)')

OperationalError: table students already exists

In [11]:
cur.execute('INSERT INTO students(id,name,faculty)VALUES(71086,"Suravi Regmi","BCT")')


---------------------------------------------------------------------------
IntegrityError                            Traceback (most recent call last)
<ipython-input-11-530eb3ea465f> in <module>()
----> 1 cur.execute('INSERT INTO students(id,name,faculty)VALUES(71086,"Suravi Regmi","BCT")')

IntegrityError: UNIQUE constraint failed: students.id

In [12]:
cur.execute('SELECT * FROM students')
cur.fetchall()


Out[12]:
[(71086, 'Suravi Regmi', 'BCT')]

In [13]:
cur.execute('INSERT INTO students(id,name,faculty)VALUES(71086,"Suravi Regmi","BCT")')


---------------------------------------------------------------------------
IntegrityError                            Traceback (most recent call last)
<ipython-input-13-530eb3ea465f> in <module>()
----> 1 cur.execute('INSERT INTO students(id,name,faculty)VALUES(71086,"Suravi Regmi","BCT")')

IntegrityError: UNIQUE constraint failed: students.id

In [14]:
cur.execute('DELETE from books')
cur.fetchall()


Out[14]:
[]

In [15]:
conn.commit()
cur.execute('CREATE TABLE teachers(id integer primary key, name text,faculty text)')

In [16]:
cur.execute('CREATE TABLE teachers(id integer primary key, name text,faculty text)')


---------------------------------------------------------------------------
OperationalError                          Traceback (most recent call last)
<ipython-input-16-f8ffc5b70a68> in <module>()
----> 1 cur.execute('CREATE TABLE teachers(id integer primary key, name text,faculty text)')

OperationalError: table teachers already exists

In [17]:
cur.execute('CREATE TABLE staff(id integer primary key, name text)')


---------------------------------------------------------------------------
OperationalError                          Traceback (most recent call last)
<ipython-input-17-2de107db134e> in <module>()
----> 1 cur.execute('CREATE TABLE staff(id integer primary key, name text)')

OperationalError: table staff already exists

In [18]:
sql='''INSERT INTO books (id,name,isbn) VALUES (?,?,?)'''
cur.executemany(sql,[(2,'Programming With C','a-1234'),
                     (3,'Basic Electrical Engineering','bee-865'),
                     (4,'Applied Mechanics','appm-986')])


Out[18]:
<sqlite3.Cursor at 0x174fc68ff80>

In [19]:
cur.execute('SELECT * FROM books')
cur.fetchall()


Out[19]:
[(2, 'Programming With C', 'a-1234'),
 (3, 'Basic Electrical Engineering', 'bee-865'),
 (4, 'Applied Mechanics', 'appm-986')]

In [20]:
cur.execute('DROP TABLE books')


Out[20]:
<sqlite3.Cursor at 0x174fc68ff80>

In [21]:
class Book:
    def __init__(self,bid,name,isbn):
        self.bid = bid
        self.name = name
        self.isbn = isbn
    def save(self):
        if self.id:
            cur.execute('UPDATE books SET name =?, isbn =? WHERE id =? ',(self.name, self.isbn,self.id))
        else:
            cur.execute('INSERT INTO books(id,name,isbn)VALUES(?,?,?)',(self.bid, self.name, self.isbn))
    @staticmethod
    def get_book_by_name(name):
        cur.execute('SELECT * FROM books WHERE name LIKE"%?%"', (name,))
        return cur.fetchall()
    @staticmethod
    def get_all_books():
        pass
    
    @staticmethod
    def get_book_by_id(_id):
        cur.execute ('SELECT * FROM books WHERE id=?',(_id,))
        result = cur.fetchone()
        if resut:
            book = Book()
            book.id, book.name,book.isbn = result
            return book
        return None

In [ ]:
Book.get_book_by_name('Learning')

In [ ]:
conn.commit()

In [ ]:
book1=Book(5,'learn nepali','ln-145')
book1.save()

In [ ]:
book2 = Book.get_book_by_id(4)
book2.name = 'New Name'
book2.save()

In [ ]:
cur.execute('SELECT * FROM books')
cur.fetchall()

In [ ]:
class Student:
    def __init__(self,stid,name,faculty):
        self.stid = stid
        self.name = name
        self.faculty = faculty
    def save(self):
        cur.execute('INSERT INTO students(id,name,faculty)VALUES(?,?,?)',(self.stid, self.name, self.faculty))
    
    def get_book_by_name(self):
        cur.execute('SELECT * FROM books WHERE name LIKE"%?%"', (name,))
        return cur.fetchall()
class Temperature:
    temp=10

    def __init__(self, temp):
        self.temp =temp

    def  get_temperature(self):
        return self.temp

    @classmethod
    def get_class_temperature(cls):
        return cls.temp

    @staticmethod
    def get_added_temparature(inst):
        return inst.temp + 10
        @staticmethod
    def get_added_temparature(inst):
        return inst.temp + Temperature.temp
t =Temperature(20)
# t=Temperature()--> Temperature.__init__(t,20)
t.get_temperature() #20
# Temperature.get_temperature(t)
Temperature.get_class_temperature()
#Temperature.get_class_temperature(Temperature) 10
Temperature.get_added temperature(t)
#Temperature.get_added temperature(t)# 30

In [ ]: