metalx1000

MySql Database creator/updater

Oct 25th, 2021
805
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. ######################################################################
  3. #Copyright (C) 2021  Kris Occhipinti
  4. #https://filmsbykris.com
  5.  
  6. #This program is free software: you can redistribute it and/or modify
  7. #it under the terms of the GNU General Public License as published by
  8. #the Free Software Foundation, either version 3 of the License, or
  9. #(at your option) any later version.
  10.  
  11. #This program is distributed in the hope that it will be useful,
  12. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. #GNU General Public License for more details.
  15.  
  16. #You should have received a copy of the GNU General Public License
  17. #along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18. ######################################################################
  19.  
  20. mkdir -p "$HOME/.fbk"
  21. conf="$HOME/.fbk/database.conf"
  22.  
  23. function create_config(){
  24.   read -p "Database User: " user
  25.   read -s -p "Database Password: " password
  26.   echo "user=$user" > "$conf"
  27.   echo "password=$password" >> "$conf"
  28.   echo ""
  29. }
  30.  
  31. [[ ! -f "$conf" ]] && create_config
  32.  
  33. source "$conf"
  34.  
  35. db="$(mysql -u $user -p$password -e "show databases;"|awk '{print $1}')"
  36. db="$(echo "new $db"|tr " " "\n"|fzf --prompt "Select Database:")"
  37.  
  38. if [[ "$db" == "new" ]] || [[ "$db" == "" ]]
  39. then
  40.   read -p "Enter the name of your Database: " db
  41.   mysql -u $user -p$password -e "CREATE DATABASE $db;"
  42. fi
  43.  
  44. table="$(mysql -u $user -p$password -e "USE $db;SHOW TABLES;"|awk '{print $1}')"
  45. table="$((echo "new";echo $table|tr " " "\n")|fzf --prompt "Select Table:")"
  46.  
  47. if [[ "$table" == "new" ]] || [[ "$table" == "" ]]
  48. then
  49.   read -p "Enter the name of your table: " table
  50. fi
  51.  
  52. cmd="USE $db;"
  53. cmd="${cmd}CREATE TABLE IF NOT EXISTS $table ( id INT AUTO_INCREMENT PRIMARY KEY )"
  54. mysql -u $user -p$password -e "$cmd"
  55.  
  56. while [ 1 ]
  57. do
  58.   read -p "Item Name: " item
  59.   [[ "$item" == "" ]] && break
  60.   cmd="USE $db;ALTER TABLE $table ADD COLUMN $item TEXT";
  61.  
  62.   mysql -u $user -p$password -e "$cmd"
  63. done
  64.  
  65. cmd="USE $db;SHOW COLUMNS FROM $table"
  66. mysql -u $user -p$password -e "$cmd"
  67.  
RAW Paste Data