-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidea
57 lines (47 loc) · 2.05 KB
/
idea
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
#!/bin/bash
DB_HOST="***" #Enter the database ip
DB_USER="***" #Enter the mysql database username
DB_PASS="***" #Enter the mysql database passcode
DB_NAME="***" #Enter the database name
if [[ "$1" == "-all" ]]; then
# SQL query to retrieve all rows from the Ideas table
all=$(mysql -h $DB_HOST -u $DB_USER -p$DB_PASS $DB_NAME -e "SELECT * FROM Ideas;" 2> /dev/null)
# Print output
echo "$all"
# Save output to a text file
echo "$all" > all_ideas.txt
elif [[ "$1" == "-backup" ]]; then
# SQL query to retrieve all rows from the Ideas table
all=$(mysql -h $DB_HOST -u $DB_USER -p$DB_PASS $DB_NAME -e "SELECT * FROM Ideas;" 2> /dev/null)
# Save output to a text file
echo "$all" > all_ideas.txt
elif [[ "$1" == "-help" ]]; then
echo "Usage: idea [option]"
echo "Options:"
echo " -all : Retrieve and display all ideas from the database."
echo " -backup : Backup all ideas from the database to a text file."
echo " -add : Add a new idea to the database."
echo " -help : Display this help message."
elif [[ "$1" == "-add" ]]; then
# Check if all required arguments are provided
if [ -z "$2" ] || [ -z "$3" ]; then
echo "Usage: idea -add <name> <title> <description>"
exit 1
fi
# Assign provided arguments to variables
name="$2"
title="$3"
description="$4"
# SQL query to add the new idea to the database
query="INSERT INTO Ideas (Name, IdeaTitle, IdeaDescription) VALUES ('$name', '$title', '$description');"
# Execute the SQL query
mysql -h $DB_HOST -u $DB_USER -p$DB_PASS $DB_NAME -e "$query" 2> /dev/null
echo "New idea added to the database."
else
# SQL query to retrieve the last row based on timestamp and extract a variable
query="SELECT Name, IdeaTitle, IdeaDescription FROM Ideas ORDER BY IdeasID DESC LIMIT 1;"
# Execute the SQL query and store the result in a variable
result=$(mysql -h $DB_HOST -u $DB_USER -p$DB_PASS $DB_NAME -se "$query" 2> /dev/null)
# Output the result
echo "The Latest Idea Is : $result"
fi