使用linux shell命令将CSV数据插入DB2

时间:2019-02-25 10:01:18

标签: linux csv db2 db2-luw

在使用Shell脚本将数据从CSV文件插入CUSTOMER表中时,我需要一些帮助。是否可以通过逗号而不是数据的固定位置检索CSV数据?例如,“我的备注”数据没有固定位置,即可能包含10个字符或15个字符,因此变量会更改。

#!/bin/bash

PASSFILE=/credentials/systemcredential.properties
USERID=$(cat $PASSFILE | grep UserID | cut -f2 -d=)
PASSWORD=$(cat $PASSFILE | grep Pwd | cut -f2 -d=)

# connect to database
source /opt/db2home/db2profile
db2 connect to DBRPTGU user $USERID using $PASSWORD

#--------------------------------------------------#
# TABLE: CUSTOMER
#--------------------------------------------------#

#db2 "select count(*) from udbcuser.CUSTOMER"

db2 "load from /batchload/data/CUSTOMER.csv of asc
method L(1 7, 9 23, 25 39, 41 47, 49 68)
insert_update into udbcuser.CUSTOMER(CUSTOMER_ID,CUSTOMER_NAME,ITEM_PURCHASED,AMOUNT_PURCHASED,REMARKS)"

按要求采样数据:
9000001,Michael Tan,Wallet,$ 30,首次使用客户
9000002,Sally Gomez,夹克,$ 90,
9000003,成宁,靴子,$ 80,会员
9000004,Richard Chin,太阳镜,$ 30,成员

谢谢!

1 个答案:

答案 0 :(得分:0)

这是示例脚本。

public class BootService extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
            Toast.makeText(context, "Boot Completed", Toast.LENGTH_SHORT).show();
            //write code here
        }
    }
}

运行时,最后一个echo '9000001,Michael Tan,Wallet,$30,First time customer' > dat.csv echo '9000002,Sally Gomez,Jacket,$90,' >> dat.csv echo '9000003,Cheng Ning,Boots,$80,Member' >> dat.csv echo '9000004,Richard Chin,Sunglasses,$30,Member' >> dat.csv db2 -v drop db db1 db2 -v create db db1 db2 -v connect to db1 db2 -v "create table db2inst1.customer (num int, name char (30), item char (20), price char(10), remark char(20))" db2 -v "load from dat.csv of del insert into db2inst1.customer" db2 -v "select * from db2inst1.customer" 返回为:

select * from db2inst1.customer

加载命令有很多选项,这是V11.5手册页供您参考:

LOAD命令
https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.5.0/com.ibm.db2.luw.admin.cmd.doc/doc/r0008305.html

希望这会有所帮助。