Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sqlite3
- from http.server import HTTPServer
- from http.server import CGIHTTPRequestHandler
- import cgi
- try:
- kit = '''CREATE TABLE IF NOT EXISTS kit (
- id_kit INTEGER PRIMARY KEY,
- id_CPU INTEGER,
- id_videocard INTEGER,
- id_motherboard INTEGER,
- id_power_unit INTEGER,
- id_RAM INTEGER,
- id_storage INTEGER,
- OS TEXT,
- FOREIGN KEY (id_CPU) REFERENCES CPU(id_CPU),
- FOREIGN KEY (id_videocard) REFERENCES videocard(id_videocard),
- FOREIGN KEY (id_motherboard) REFERENCES motherboard(id_motherboard),
- FOREIGN KEY (id_power_unit) REFERENCES power_unit(id_power_unit),
- FOREIGN KEY (id_RAM) REFERENCES RAM(id_RAM),
- FOREIGN KEY (id_storage) REFERENCES storage(id_storage)
- );'''
- CPU = '''CREATE TABLE IF NOT EXISTS CPU (
- id_CPU INTEGER PRIMARY KEY,
- model TEXT NOT NULL UNIQUE,
- price INTEGER NOT NULL,
- cores INTEGER NOT NULL,
- frequency INTEGER NOT NULL
- );'''
- videocard = '''CREATE TABLE IF NOT EXISTS videocard (
- id_videocard INTEGER PRIMARY KEY,
- model TEXT NOT NULL UNIQUE,
- price INTEGER NOT NULL,
- storage INTEGER NOT NULL
- );'''
- motherboard = '''CREATE TABLE IF NOT EXISTS motherboard (
- id_motherboard INTEGER PRIMARY KEY,
- model TEXT NOT NULL UNIQUE,
- price INTEGER NOT NULL,
- ram_limit INTEGER NOT NULL,
- connectors INTEGER NOT NULL
- );'''
- power_unit = '''CREATE TABLE IF NOT EXISTS power_unit (
- id_power_unit INTEGER PRIMARY KEY,
- model TEXT NOT NULL UNIQUE,
- price INTEGER NOT NULL,
- power INTEGER NOT NULL
- );'''
- RAM = '''CREATE TABLE IF NOT EXISTS RAM (
- id_RAM INTEGER PRIMARY KEY,
- model TEXT NOT NULL UNIQUE,
- price INTEGER NOT NULL,
- frequency INTEGER NOT NULL
- );'''
- storage = '''CREATE TABLE IF NOT EXISTS storage (
- id_storage INTEGER PRIMARY KEY,
- model TEXT NOT NULL UNIQUE,
- price INTEGER NOT NULL,
- GB INTEGER NOT NULL
- );'''
- sqlite_connection = sqlite3.connect('sqlite_python.db')
- cursor = sqlite_connection.cursor()
- print("База данных подключена к SQLite")
- cursor.execute(kit)
- cursor.execute(CPU)
- cursor.execute(videocard)
- cursor.execute(motherboard)
- cursor.execute(power_unit)
- cursor.execute(RAM)
- cursor.execute(storage)
- sqlite_connection.commit()
- print("Таблица SQLite создана")
- sqlite_insert_CPU = """INSERT INTO CPU
- (model, price, cores, frequency) VALUES ('1abc', 1200, 4, 8709)"""
- count = cursor.execute(sqlite_insert_CPU)
- sqlite_insert_videocard = """INSERT INTO videocard
- (model, price, storage) VALUES ('1def', 1500, 45)"""
- count = cursor.execute(sqlite_insert_videocard)
- sqlite_insert_motherboard = """INSERT INTO motherboard
- (model, price, ram_limit, connectors) VALUES ('1ghi', 2500, 8, 2)"""
- count = cursor.execute(sqlite_insert_motherboard)
- sqlite_insert_power_unit = """INSERT INTO power_unit
- (model, price, power) VALUES ('37804f4FN', 4000, 2500)"""
- count = cursor.execute(sqlite_insert_power_unit)
- sqlite_insert_RAM = """INSERT INTO RAM
- (model, price, frequency) VALUES ('g04d437', 2500, 9600)"""
- count = cursor.execute(sqlite_insert_RAM)
- sqlite_insert_storage = """INSERT INTO storage
- (model, price, GB) VALUES ('g4d04052', 1999, 45)"""
- count = cursor.execute(sqlite_insert_storage)
- count = cursor.execute("""INSERT INTO kit (id_CPU, id_videocard, id_motherboard)
- SELECT id_CPU, id_videocard, id_motherboard
- FROM CPU, videocard, motherboard
- WHERE CPU.model = '1abc' AND videocard.model = '1def' AND motherboard.model = '1ghi'"""
- )
- print("Запись успешно вставлена в таблицу sqlitedb", cursor.rowcount)
- sqlite_connection.commit()
- cursor.execute("SELECT * FROM RAM")
- rows = cursor.fetchall()
- for row in rows:
- print("row")
- print(row)
- cursor.close()
- server_address = ("localhost", 8000)
- http_server = HTTPServer(server_address, CGIHTTPRequestHandler)
- http_server.serve_forever()
- handler = HTTPServer.CGIHTTPRequestHandler
- handler.cgi_directories = [""]
- handler.cgi_directories = ["/"]
- form = cgi.FieldStorage()
- text1 = form.getfirst("Author", "не задано")
- text2 = form.getfirst("Book", "не задано")
- print("Content - type: text/html\n")
- print("""<!DOCTYPE HTML>
- <html>
- <head>
- <meta charset = "cp1251">
- <title>storage</title>
- </head>
- <body>
- <form action = "/cgi/bin/view02.py">
- Model <input type = "text" name = "model">
- Price <input type = "text" name = "model">
- <div>
- <input type="checkbox" name="GB" value="4GB">
- <label for="4GB">4 GB</label>
- </div>
- <div>
- <input type="checkbox" name="GB" value="8GB">
- <label for="8GB">8 GB</label>
- </div>
- <div>
- <input type="checkbox" name="GB" value="16GB">
- <label for="16GB">16 GB</label>
- </div>
- <input type = "submit">
- </form>
- </body>
- </html>""")
- except sqlite3.Error as error:
- print("Ошибка при подключении к sqlite", error)
- finally:
- if (sqlite_connection):
- sqlite_connection.close()
- print("Соединение с SQLite закрыто")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement