GithubHelp home page GithubHelp logo

sh_mysql_wrapper's Introduction

BASH MySQL Wrapper

Examples

#!/bin/bash

#
# include wrapper
#

source "$(dirname "$0")/mysql_wrapper.sh";

#
# connect data
#

con_user="root";
con_pass="test";
con_host="localhost";
con_port="3306";
con_db="test";

#
# example escape string
#

echo $(escape "'");

#
# example query
#

query 'select * from test;';

#
# example fetch query
#

i=0;
query 'select * from test;' | while read -r line; do
	if [ $i -gt 0 ]; then
		echo ----------------------------------[ Row $i ]
		echo "Column 1: "$(echo "$line" | cut -f1) # column 1
		echo "Column 2: "$(echo "$line" | cut -f2) # column 2
		echo "Column 3: "$(echo "$line" | cut -f3) # column 3
		# column ...
	fi
	i=`expr $i + 1`;
done

#
# example fetch 1st result from query
#

fetch1st "select * from test";

echo ${matrix[id]} 
echo ${matrix[firstname]} 
echo ${matrix[surname]} 

echo "There is "$num_rows" row and "$num_columns" columns."

#
# example fetch2array
#

fetch2array "select * from test limit 5";

echo $num_rows "rows"
echo $num_columns "cols"

for (( j=1; j <= num_columns; j++ )); do
    for (( i=1; i <= num_rows; i++ )); do
		# $i - row
		# $j - col
        echo ${matrix[$i,$j]}
    done
done

#
# example array2insert
#

declare -A data
data=([id]='::null' [firstname]=Radovan [surname]=Janjic)
array2insert 'test' "$(declare -p data)"
echo $last_insert_id

#
# example array2update
#

declare -A data
data=([firstname]=Rade [surname]=Janjic)
id=3;
array2update 'test' "$(declare -p data)" "id < $id"
echo $affected_rows

#
# example of query2csv
#

query2csv 'select * from test' 'test-1.csv';
query2csv 'select firstname,surname from test limit 2,4' 'test-2.csv';

#
# example table2csv
#

table2csv 'test' 'test.csv'

#
# example importcsv2table
#

declare -A update
update=([id]='NULL' [firstname]="'Radovan'");
importcsv2table 'test.csv' 'test' "$(declare -p update)";
echo $affected_rows;
#update=([date]='STR_TO_DATE(@date, "%d/%m/%Y")');

#
# example importupdatecsv2table
#

declare -A update
update=([id]='NULL' [firstname]="'Radovan'");
importupdatecsv2table 'test.csv' 'test' "$(declare -p update)";
echo $affected_rows;
#update=([date]='STR_TO_DATE(@date, "%d/%m/%Y")');

#
# example importdump
#

importdump "20140612.test.sql"; 

#
# example backup
#

backup                        # backup all databases
backup 'testdb'               # backup testdb
backup 'testdb' 'test_table'  # backup test_table

sh_mysql_wrapper's People

Contributors

rjanjic avatar

Watchers

 avatar

Forkers

siryaro

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.