many refinements
This commit is contained in:
@ -4,14 +4,19 @@ import json
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
# Function to get all drive records from the database
|
||||
def get_all_drive_records():
|
||||
# sqlite query function
|
||||
def query_db(sql_query):
|
||||
conn = sqlite3.connect('drive_records.db')
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT * FROM drive_records")
|
||||
cursor.execute(sql_query)
|
||||
rows = cursor.fetchall()
|
||||
conn.close()
|
||||
|
||||
return rows
|
||||
|
||||
# Function to return all drive records in database
|
||||
def get_all_drive_records():
|
||||
get_all_drives = "SELECT * FROM drive_records"
|
||||
rows = query_db(get_all_drives)
|
||||
drives = []
|
||||
for row in rows:
|
||||
drive = {
|
||||
@ -28,13 +33,8 @@ def get_all_drive_records():
|
||||
|
||||
# Function to check if a serial number exists in the database
|
||||
def check_serial_exists(serial):
|
||||
conn = sqlite3.connect('drive_records.db')
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT * FROM drive_records WHERE serial=?", (serial,))
|
||||
row = cursor.fetchone()
|
||||
conn.close()
|
||||
return bool(row)
|
||||
|
||||
return bool(query_db("SELECT * FROM drive_records WHERE serial=?", (serial,)))
|
||||
|
||||
# Route to check if a serial number exists in the database
|
||||
@app.route('/check', methods=['GET'])
|
||||
def check():
|
||||
|
||||
@ -1,66 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Function to display usage information
|
||||
usage() {
|
||||
echo "Usage: $0 -a \"serial,model,capacity,TBW,smart\""
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check if the correct number of arguments is provided
|
||||
if [ "$#" -ne 2 ]; then
|
||||
usage
|
||||
fi
|
||||
|
||||
# Parse command-line arguments
|
||||
while getopts ":a:" opt; do
|
||||
case ${opt} in
|
||||
a)
|
||||
# Extract the comma-separated list of attributes
|
||||
IFS=',' read -ra ATTRS <<< "$OPTARG"
|
||||
;;
|
||||
\?)
|
||||
echo "Invalid option: $OPTARG" 1>&2
|
||||
usage
|
||||
;;
|
||||
esac
|
||||
done
|
||||
shift $((OPTIND -1))
|
||||
|
||||
# Check if the database file exists, otherwise create it and initialize the table
|
||||
DB_FILE="drive_records.db"
|
||||
if [ ! -f "$DB_FILE" ]; then
|
||||
sqlite3 "$DB_FILE" <<EOF
|
||||
CREATE TABLE drive_records (id INTEGER PRIMARY KEY, serial TEXT NOT NULL, model TEXT NOT NULL, capacity TEXT NOT NULL, TBW TEXT NOT NULL, smart TEXT NOT NULL);
|
||||
EOF
|
||||
fi
|
||||
|
||||
# Read the input data from stdin
|
||||
while read -r line; do
|
||||
# Extract values for each attribute and insert into the database
|
||||
VALUES=()
|
||||
IFS=' ' read -ra PARTS <<< "$line"
|
||||
for ((i = 0; i < ${#ATTRS[@]}; i++)); do
|
||||
case "${ATTRS[i]}" in
|
||||
serial)
|
||||
VALUES+=("${PARTS[0]}")
|
||||
;;
|
||||
model)
|
||||
VALUES+=("${PARTS[1]}")
|
||||
;;
|
||||
capacity)
|
||||
VALUES+=("${PARTS[2]}")
|
||||
;;
|
||||
TBW)
|
||||
VALUES+=("${PARTS[3]}")
|
||||
;;
|
||||
smart)
|
||||
VALUES+=("${PARTS[4]}")
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Insert the values into the database
|
||||
sqlite3 "$DB_FILE" <<EOF
|
||||
INSERT INTO drive_records (serial, model, capacity, TBW, smart) VALUES ('${VALUES[0]}', '${VALUES[1]}', '${VALUES[2]}', '${VALUES[3]}', '${VALUES[4]}');
|
||||
EOF
|
||||
done <<< "$(cat -)"
|
||||
Reference in New Issue
Block a user