Nothing Special   »   [go: up one dir, main page]

Give A Try - Database Connectivity

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

#!

/bin/python3

import sys
import os
import sqlite3

# Complete the function below.

def main():
conn = sqlite3.connect('SAMPLE.db')
cursor = conn.cursor()
---------------------1----------------------------------
#!/bin/python3

import sys
import os
import sqlite3

# Complete the following function:

def main():
conn = sqlite3.connect('SAMPLE.db')
#create connection cursor
cur = conn.cursor()
#create table ITEMS using the cursor
cur.execute('''CREATE TABLE ITEMS(
item_id text, item_name text, item_description text, item_category text,
quantity_in_stock real)''')
#commit connection
conn.commit()
#close connection
conn.close()

'''To test the code, no input is required'''


if __name__ == "__main__":

------------------------2--------------------------------
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)
]
#Add code to insert records to ITEM table
cursor.executemany("insert into ITEMS values (?, ?, ?, ?, ?)", items)
try:
cursor.execute("select * from ITEMS")
except:
return 'Unable to perform the transaction.'
rowout=[]
for row in cursor.fetchall():
rowout.append(row)
return rowout
conn.close()

'''For testing the code, no input is required'''

if __name__ == "__main__":

----------------------3----------------------

#!/bin/python3

import sys
import os
import sqlite3

# Complete the function below.

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)
conn.commit()
#Add code to select items here
cursor = conn.execute("SELECT * from ITEMS where item_id in ('101','102')")
except:
return 'Unable to perform the transaction.'
rowout=[]
for row in cursor.fetchall():
rowout.append(row)
return rowout
conn.close()

'''For testing the code, no input is required'''

if __name__ == "__main__":

---------------------------4-----------------------------

#!/bin/python3

import sys
import os
import sqlite3

# Complete the function below.

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)

#Add code for updating quantity_in_stock

conn.execute("UPDATE ITEMS set quantity_in_stock = 4 where item_id = 103")


conn.execute("UPDATE ITEMS set quantity_in_stock = 2 where item_id = 101")
conn.execute("UPDATE ITEMS set quantity_in_stock = 0 where item_id = 105")
conn.commit()
cursor.execute("select item_id,quantity_in_stock from ITEMS")
except:
'Unable to perform the transaction.'
rowout=[]
for row in cursor.fetchall():
rowout.append(row)
return rowout
conn.close()

'''For testing the code, no input is required'''

if __name__ == "__main__":

-------------------------5---------------------------

#!/bin/python3

import sys
import os
import sqlite3

# Complete the function below.

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)
cursor.executemany("update ITEMS set quantity_in_stock = ? where item_id
= ?",
[(4, 103),
(2, 101),
(0, 105)])
#Add code below to delete items
conn.execute("DELETE from ITEMS where item_id = 105;")
cursor.execute("select item_id from ITEMS")
except:
return 'Unable to perform the transaction.'
rowout=[]
for row in cursor.fetchall():
rowout.append(row)
return rowout
conn.close()

'''For testing the code, no input is required'''

if __name__ == "__main__":

You might also like