-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.sh
executable file
·75 lines (63 loc) · 2.01 KB
/
db.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env bash
# Configuration
DATABASE="/home/pi/ClimaViewer/sensor_data.db"
TABLE_NAME="sensor_readings"
# Function to create the SQLite database and table
create_database() {
sqlite3 "$DATABASE" <<EOF
CREATE TABLE IF NOT EXISTS "$TABLE_NAME" (
id INTEGER PRIMARY KEY AUTOINCREMENT,
channel INTEGER,
name TEXT,
temperature REAL,
humidity REAL,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
);
EOF
if [ $? -ne 0 ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') - Error: Failed to create the table."
exit 1
fi
}
# Function to insert data into the database
insert_data() {
local json_input="$1"
# Use transaction to optimize multiple inserts
sqlite3 "$DATABASE" <<EOF
BEGIN TRANSACTION;
$(echo "$json_input" | jq -c '.[]' | while read -r record; do
local channel=$(echo "$record" | jq -r '.Channel')
local name=$(echo "$record" | jq -r '.Name')
local temperature=$(echo "$record" | jq -r '.Temperature')
local humidity=$(echo "$record" | jq -r '.Humidity')
echo "INSERT INTO \"$TABLE_NAME\" (channel, name, temperature, humidity) VALUES ($channel, '$name', $temperature, $humidity);"
done)
COMMIT;
EOF
if [ $? -ne 0 ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') - Error: Failed to insert data into the database."
exit 1
fi
}
# Main script
JSON_INPUT=""
# Check if JSON data is provided via stdin
if [ -p /dev/stdin ]; then
JSON_INPUT=$(cat /dev/stdin)
elif [ -n "$1" ]; then
# Check if a file path is provided as a parameter
if [ -f "$1" ]; then
JSON_INPUT=$(cat "$1")
else
echo "$(date '+%Y-%m-%d %H:%M:%S') - Error: The file '$1' does not exist."
exit 1
fi
else
echo "$(date '+%Y-%m-%d %H:%M:%S') - Error: No input provided. Provide JSON data via stdin or as a file parameter."
exit 1
fi
# Create the database and table
create_database
# Insert the data into the database
insert_data "$JSON_INPUT"
echo "$(date '+%Y-%m-%d %H:%M:%S') - Data successfully inserted into the database."