mysql table structure to Microsoft word
# convert mysql table structure to doc
sql2doc() {
[ "$#" -ne "3" ] && echo "usage:$0 [host] [password] [db]" && return
local tmp_file="/tmp/sql2doc.md"
local host="$1"
local passwd="$2"
local db="$3"
# template string
local table_header_tmp="## %s\n\n| | | | | | |\n|:-:|:-:|:-:|:-:|:-:|:-:|\n"
local sqlcmd_tmp="mysql -uroot -h %s --password=%s -e \"%s\""
local sql_tmp="SELECT COLUMN_NAME as '字段名',COLUMN_DEFAULT as '默认值',COLUMN_TYPE as '数据类型',COLUMN_KEY as 'KEY',IS_NULLABLE as '是否为空',COLUMN_COMMENT as '说明' FROM information_schema.columns WHERE table_schema = '%s' AND table_name = '%s'"
# get all table
local table_sql="use $db;show tables"
local sqlcmd=`printf $sqlcmd_tmp $host $passwd $table_sql`
local table_list=`eval $sqlcmd 2>/dev/null | tr -d '-' | tail -n +2` && echo "table_list:\n$table_list\n"
rm -rf $tmp_file
for item in `echo $table_list`
do
local sql=`printf $sql_tmp $db $item`
local sqlcmd=`printf $sqlcmd_tmp $host $passwd $sql`
local res=`eval $sqlcmd | tr '\t' '|' | sed -z 's/\n/|\n/g' | sed -e 's/^/|/g'`
printf $table_header_tmp $item >> $tmp_file
echo "$res\n" >> $tmp_file
done
pandoc -o sql2doc.docx -f markdown -t docx $tmp_file
rm -rf $tmp_file # remove tmp file
}