Database Connection 1
Database Connection 1
===================================================================================
===================================================================================
============================================================1======================
========
#!/bin/python3
import sys
import os
import sqlite3
def main():
conn = sqlite3.connect('SAMPLE.db')
#create connection cursor
cursor = conn.cursor()
sql2 = '''
CREATE TABLE ITEMS (
item_id INT(6) NOT NULL,
item_name CHAR(20) NOT NULL,
item_description CHAR(20),
item_category CHAR(5),
quantity_in_stock INT(10)
)
'''
cursor.execute(sql2)
conn.close()
#create table ITEMS using the cursor
#commit connection
#close connection
=====================================================2=============================
==============================
#!/bin/python3
import sys
import os
import sqlite3
def main():
conn = sqlite3.connect('SAMPLE.db')
cursor = conn.cursor()
cursor.execute("drop table if exists ITEMS")
cursor.execute(sql_statement)
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()
if __name__ == "__main__":
==============================================================3====================
======================================
#!/bin/python3
import sys
import os
import sqlite3
def main():
conn = sqlite3.connect('SAMPLE.db')
cursor = conn.cursor()
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 where item_id < 103')
rowout=[]
for row in cursor.fetchall():
rowout.append(row)
print(row)
return rowout
conn.close()
=====================================================4=============================
========================
#!/bin/python3
import sys
import os
import sqlite3
def main():
conn = sqlite3.connect('SAMPLE.db')
cursor = conn.cursor()
cursor.execute("drop table if exists ITEMS")
cursor.execute(sql_statement)
except:
'Unable to perform the transaction.'
rowout=[]
for row in cursor.fetchall():
rowout.append(row)
return rowout
conn.close()
===========================================================5=======================
===========================================
#!/bin/python3
import sys
import os
import sqlite3
def main():
conn = sqlite3.connect('SAMPLE.db')
cursor = conn.cursor()
cursor.execute(sql_statement)
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
cursor.execute("DELETE FROM ITEMS WHERE item_id = 105")
===================================================================================
===================================================================================
===================================================================================
===================================================================================
====================================================