简体   繁体   中英

Array values and insert in ksh

I've got an array which returns the data_type of a table read from a file. For example:

filed1 VARCHAR2
filed2 INTEGER

etc.

Now I have to do an insert in the same table for a backup. This procedure would be dynamic so I came up with this:

ENDROW=""
while read ENDROW;
do  
    tmp=${ENDROW##*@#;}
          field1=`printf "%s\n" "${tmp%%@#;*}"`
    tmp=${ENDROW##*@#;}
          field2=`printf "%s\n" "${tmp%%@#;*}"`

    *and here the insert in the table*
done

I created the array because I need it to find the data_type of each field so I can understand if the values need to be quoted ( '' ) or not. How can I "connect" the array to this? The shell would be able to understand that the first value of the array is VARCHAR2 and quote with '' as part of the insert .


Oh ok now i understand.. Thanks! However..if my array is like:

field1                VARCHAR2
field2                NUMBER
field3                VARCHAR2
field4                DATE
field5                TIMESTAMP(6)
field6                DATE
field7                DATE
field8                CHAR

the function will be:

function format {
case "${typeof[$1]}" in
    VARCHAR2) printf '"%s"\n' "$2" ;;
    NUMBER)  printf "%d\n" "$2" ;;
    DATE)  printf "%d\n" "$2" ;;
    CHAR)  printf "%d\n" "$2" ;;
    *) print -u2 "don't know about field '$1'" ;;
    esac
}

?

i'd do something like this -- i assume you have an array named typeof that maps the field name to the datatype:

function format {
    case "${typeof[$1]}" in
        VARCHAR2) printf '"%s"\n' "$2" ;;
        INTEGER)  printf "%d\n" "$2" ;;
        *) print -u2 "don't know about field '$1'" ;;
    esac
}

while read ENDROW;
do  
    tmp=${ENDROW##*@#;}
    field1=$( format field1 "${tmp%%@#;*}" )
    tmp=${ENDROW##*@#;}
    field2=$( format field2 "${tmp%%@#;*}" )
    # and here the insert in the table
done

well, first of all i made a select with a spool to extract what i need. Then i made the array from that file created from the spool in this way:

i=0
while read str
do
arr[$((i=i+1))]=$str
done < ${file}

The array contains :

field1                VARCHAR2
field2                NUMBER
field3                VARCHAR2
field4                DATE
field5                TIMESTAMP(6)
field6                DATE
field7                DATE
field8                CHAR

Ok?


I was thinking the array would be:

arr[field1]="VARCHAR2"
arr[field2]="NUMBER"
arr[field3]="VARCHAR2"
arr[field4]="DATE"
arr[field5]="TIMESTAMP(6)"
arr[field6]="DATE"
arr[field7]="DATE"
arr[field8]="CHAR"

This is an associative array, using a string instead of an integer as the index. You would do

typeset -A arr
while read key value; do
    arr[$key]=$value
done < ${file}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM