You are on page 1of 3

1) Database Connectivity 1-connect to Database and create table

def main():
conn = sqlite3.connect('SAMPLE.db')
#create connection cursor
cursor=conn.cursor()

#create table ITEMS using the cursor


t1='''
(Create table
ITEMS(item_id,item_name,item_description,item_category,quantity_in_stock))
'''

#commit connection
conn.commit()

#close connection
conn.close()

2)Insert records in table

def main():
conn = sqlite3.connect('SAMPLE.db')
cursor = conn.cursor()

cursor.execute("drop table if exists ITEMS")

sql_statement = '''CREATE TABLE ITEMS


(item_id integer not null, item_name varchar(300),
item_description text, item_category text,
quantity_in_stock integer)'''

cursor.execute(sql_statement)
items = [(101, 'Nik D300', 'Nik D300', 'DSLR Camera', 3), (102, 'Can 1300',
'Can 1300', 'DSLR Camera', 5), (103, 'gPhone 13S', 'gPhone 13S', 'Mobile', 10),
(104, 'Mic canvas', 'Mic canvas', 'Tab', 5), (105, 'SnDisk 10T', 'SnDisk 10T',
'Hard Drive', 1) ]

try:
cursor.executemany('Insert into ITEMS values (?,?,?,?,?)', items)
print('hello')
conn.commit()
#Add code to select items here
except:
return 'Unable to perform the transaction.'
cursor.execute('select * from ITEMS')
rowout=[]
for row in cursor.fetchall():
rowout.append(row)
print(row)
return rowout
conn.close()

3)Select record from table


cursor.execute("select * from ITEMS where item_id <103")

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----
1)Higher Order function -Closure 2
def factory(n):

def current():

return n

def counter():

n=int(current())
n=n+1
return n

return current, counter


n=0
f_current, f_counter = factory((input()))

print(f_current())
print(f_counter())

--------------------Closure 1--------
def detecter(element):
e=element
def isIn(sequence):
a=0
for i in sequence:
if int(i)==e :
a=1
if a==1:
return True
else:
return False
return isIn

detect30 = detecter(30)
detect45 = detecter(45)
print(detect30)
-------------------------------------------------------
----------Decorators-2-----------
#Add greet function definition here
@log
def average(n1,n2,n3):
return (n1+n2+n3)/3
print(detect30)
-----------------------------------------------------------------------------------
--------------------------------------------------------
------------Descripter---------

class Celsius:
def __get__(self):
return float(self._celsius)
def __set__(self, value):
self._celsius = value
# Add temperature class implementation below.
class Temperature:
def __init__(self, fahrenheit):
self._fahrenheit = fahrenheit
self._celsius = self.to_celsius(fahrenheit)
def to_celsius(self, f):
return (f-32) * (5/9)
def getFahrenheit(self):
return self._fahrenheit
def set_fahrenheit(self, value):
self._fahrenheit = value
self._celsius = self.to_celsius(value)
def set_celsius(self,value):
self._fahrenheit = self.to_fahrenheit(value)
Celsius.__set__(self,value)
def to_fahrenheit(self, c):
return (9/5) * c + 32
fahrenheit = property(getFahrenheit,set_fahrenheit)
celsius = property(Celsius.__get__,set_celsius)

You might also like