Please convert this file into class Database: class Database: def __init__(self): self.db = sqliteConnection() ...
Please convert this file into class Database: class Database: def __init__(self): self.db = sqliteConnection() ...
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
Related questions
Question
from io import BytesIO
import sqlite3
import pandas as pd
import os
import pickle
sqliteConnection = None
class student:
def __init__(self,first,last):
self.first = first
self.last = last
def to_bytes(self):
import pickle
return pickle.dumps(self)
def convert2Blog(filename):
with open(filename, 'rb') as file:
blobData = file.read()
return blobData
def connect(dbname):
global sqliteConnection
try:
sqliteConnection = sqlite3.connect(dbname)
cursor = sqliteConnection.cursor()
print("Database created and Successfully Connected to SQLite")
select_Query = "select sqlite_version();"
print("Search query: ",select_Query)
cursor.execute(select_Query)
record = cursor.fetchall()
print("SQLite Database Version is: ", record)
except sqlite3.Error as error:
print("Error while connecting to sqlite", error)
def deleteRecord(id):
global sqliteConnection
try:
cursor = sqliteConnection.cursor()
delete_query = "DELETE from Database where id = "+str(id)
print("Delete query: ",delete_query)
cursor.execute(delete_query)
sqliteConnection.commit()
print("Record deleted successfully ")
except sqlite3.Error as error:
print("Failed to delete record from sqlite table", error)
def Search(value):
global sqliteConnection
cursor = sqliteConnection.cursor()
sel = 'SELECT id FROM Database WHERE name == "{0}"'.format(value)
print("Search query: ",sel)
cursor.execute(sel)
result = cursor.fetchall()
return result
def insert(query,tup):
global sqliteConnection
try:
cursor = sqliteConnection.cursor()
cursor.execute(query, tup)
print("Search query: ",query)
sqliteConnection.commit()
print("Inserted successfully into table")
except sqlite3.Error as error:
print("Failed to insert: ", error)
def readTable():
global sqliteConnection
records = None
try:
cursor = sqliteConnection.cursor()
sqlite_select_query = """SELECT * from Database"""
print("Search query: ",sqlite_select_query)
cursor.execute(sqlite_select_query)
records = cursor.fetchall()
print("Total rows are: ", len(records))
except sqlite3.Error as error:
print("Failed to read data from sqlite table", error)
return records
def table(query):
global sqliteConnection
try:
cursor = sqliteConnection.cursor()
cursor.execute(query)
print("Table query: ",query)
sqliteConnection.commit()
print("SQLite table created")
except sqlite3.Error as error:
print("Table exists: ", error)
def update(id, htext):
try:
cursor = sqliteConnection.cursor()
update_query = """Update Database set html = ? where id = ?"""
data = (htext, id)
cursor.execute(update_query, data)
print("Update query: ",update_query)
sqliteConnection.commit()
print("Record Updated successfully")
except sqlite3.Error as error:
print("Failed to update sqlite table", error)
def dbTest():
#connect
database = "CIS41b_.db"
connect(database)
#table
table_query = '''CREATE TABLE Database (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
photo text NOT NULL UNIQUE,
html text NOT NULL UNIQUE)'''
#columns = collections.nametuple('columns', ['id','name','photo','html'])
table(table_query)
#insert
insert_query = """INSERT INTO Database (id, name, photo, html) VALUES (?, ?, ?, ?)"""
data_t = (1, "Temperature", convert2Blog("Temperature.png"),convert2Blog("Temperature.html"))
insert(insert_query,data_t)
data_t = (2, "CO2", convert2Blog("CO2.png"), convert2Blog("CO2.html"))
insert(insert_query,data_t)
data_t = (3, "Temperature", convert2Blog("SeaLevel.png"), convert2Blog("SeaLevel.csv"))
insert(insert_query,data_t)
dframe = pd.DataFrame([1,2,3,4])
data_t = (4, "DataFrame", convert2Blog("Dataframe.jpg"), dframe.to_json())
insert(insert_query,data_t)
mad = student("Newman","Alfred")
bmad = mad.to_bytes()
print(mad.__class__)
print(type(bmad))
data_t = (5, "Alfred Newman", convert2Blog("Alfred.png"), bmad)
insert(insert_query,data_t)
mad = pickle.loads(bmad)
print(type(mad))
#read
records = readTable()
[print(r) for r in records]
#search
record = Search("CO2")
[print(r) for r in record]
#update
update(2, "OS.html")
#delete
deleteRecord(2)
#shut-down
sqliteConnection.close()
os.remove(database)
#
dbTest()
import sqlite3
import pandas as pd
import os
import pickle
sqliteConnection = None
class student:
def __init__(self,first,last):
self.first = first
self.last = last
def to_bytes(self):
import pickle
return pickle.dumps(self)
def convert2Blog(filename):
with open(filename, 'rb') as file:
blobData = file.read()
return blobData
def connect(dbname):
global sqliteConnection
try:
sqliteConnection = sqlite3.connect(dbname)
cursor = sqliteConnection.cursor()
print("
select_Query = "select sqlite_version();"
print("Search query: ",select_Query)
cursor.execute(select_Query)
record = cursor.fetchall()
print("SQLite Database Version is: ", record)
except sqlite3.Error as error:
print("Error while connecting to sqlite", error)
def deleteRecord(id):
global sqliteConnection
try:
cursor = sqliteConnection.cursor()
delete_query = "DELETE from Database where id = "+str(id)
print("Delete query: ",delete_query)
cursor.execute(delete_query)
sqliteConnection.commit()
print("Record deleted successfully ")
except sqlite3.Error as error:
print("Failed to delete record from sqlite table", error)
def Search(value):
global sqliteConnection
cursor = sqliteConnection.cursor()
sel = 'SELECT id FROM Database WHERE name == "{0}"'.format(value)
print("Search query: ",sel)
cursor.execute(sel)
result = cursor.fetchall()
return result
def insert(query,tup):
global sqliteConnection
try:
cursor = sqliteConnection.cursor()
cursor.execute(query, tup)
print("Search query: ",query)
sqliteConnection.commit()
print("Inserted successfully into table")
except sqlite3.Error as error:
print("Failed to insert: ", error)
def readTable():
global sqliteConnection
records = None
try:
cursor = sqliteConnection.cursor()
sqlite_select_query = """SELECT * from Database"""
print("Search query: ",sqlite_select_query)
cursor.execute(sqlite_select_query)
records = cursor.fetchall()
print("Total rows are: ", len(records))
except sqlite3.Error as error:
print("Failed to read data from sqlite table", error)
return records
def table(query):
global sqliteConnection
try:
cursor = sqliteConnection.cursor()
cursor.execute(query)
print("Table query: ",query)
sqliteConnection.commit()
print("SQLite table created")
except sqlite3.Error as error:
print("Table exists: ", error)
def update(id, htext):
try:
cursor = sqliteConnection.cursor()
update_query = """Update Database set html = ? where id = ?"""
data = (htext, id)
cursor.execute(update_query, data)
print("Update query: ",update_query)
sqliteConnection.commit()
print("Record Updated successfully")
except sqlite3.Error as error:
print("Failed to update sqlite table", error)
def dbTest():
#connect
database = "CIS41b_.db"
connect(database)
#table
table_query = '''CREATE TABLE Database (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
photo text NOT NULL UNIQUE,
html text NOT NULL UNIQUE)'''
#columns = collections.nametuple('columns', ['id','name','photo','html'])
table(table_query)
#insert
insert_query = """INSERT INTO Database (id, name, photo, html) VALUES (?, ?, ?, ?)"""
data_t = (1, "Temperature", convert2Blog("Temperature.png"),convert2Blog("Temperature.html"))
insert(insert_query,data_t)
data_t = (2, "CO2", convert2Blog("CO2.png"), convert2Blog("CO2.html"))
insert(insert_query,data_t)
data_t = (3, "Temperature", convert2Blog("SeaLevel.png"), convert2Blog("SeaLevel.csv"))
insert(insert_query,data_t)
dframe = pd.DataFrame([1,2,3,4])
data_t = (4, "DataFrame", convert2Blog("Dataframe.jpg"), dframe.to_json())
insert(insert_query,data_t)
mad = student("Newman","Alfred")
bmad = mad.to_bytes()
print(mad.__class__)
print(type(bmad))
data_t = (5, "Alfred Newman", convert2Blog("Alfred.png"), bmad)
insert(insert_query,data_t)
mad = pickle.loads(bmad)
print(type(mad))
#read
records = readTable()
[print(r) for r in records]
#search
record = Search("CO2")
[print(r) for r in record]
#update
update(2, "OS.html")
#delete
deleteRecord(2)
#shut-down
sqliteConnection.close()
os.remove(database)
#
dbTest()
Please convert this file into class Database:
class Database:
def __init__(self):
self.db = sqliteConnection()
...
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 3 steps
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY