代码改变世界

数据库批量插入数据的shell脚本

2013-07-19 16:49 梁小白 阅读(...) 评论(...) 编辑 收藏

测试用,先来一个简单的,这个是国产神通数据库的,用isql命令:

!/bin/bash

if [ $1 == "-h" ]
then
        echo "USAGE: $0 table_name max_insert_row field_a (field_b ...)"
        exit
fi

if [ $# -lt 3 ]
then
        tbl_name="test_tbl"
        field_a=1
        max_insert=1
else
        tbl_name=$1
        max_insert=$2
        field_a=$3
fi

id=1

while test $id -le $max_insert
do
        isql -c "insert into $tbl_name values($id,$field_a);"
        let id=$id+1
        let field_a=$field+1
done

echo "insert bash has completed. total: $id."

 

 如果是mysql 将相应的sql语句替换即可

mysql -uroot -ppassword -e "insert into $tbl_nam values($id,$field_a);"