MBA 683 E-Business Technology --

csv-2.adp
Home    Site Map    Search    EhrlichOrg.com

Syllabus    Classes    References  Examples  Student Info    Guest Info

 

<html>
<head>
<title>Comma Separated Value Loader page 2</title>
</head>
<body>
It is now <%= [ns_fmttime [ns_time] "%A, %B %e, %Y at %r" ] %><p><hr><p>
<%
set_the_usual_form_variables
# sets account, file, table
if { ![info exists account] } { 
ns_adp_puts "You must enter an account name!<br>"
} elseif { $account=="" } {
ns_adp_puts "Account must not be blank!<br>"
} elseif { [catch {open /home/web/usf-acs/www/student/$account/$file r} fid ] } {
ns_adp_puts "open failed<br>$fid<p>"
} else {
ns_adp_puts "File $account/$file opened<p>"
set db [ns_db gethandle]
#force account to upper case for Oracle
set account [string toupper $account ]
set count 0
set nitems [ns_getcsv $fid list]
if { $nitems <= 1 } {
ns_adp_puts "First line must be list of column names<p>"
} else {
set cols [join $list ", "]
ns_adp_puts "Data for columns: $cols<p>"
while { [ns_getcsv $fid list] >= 0} {
# getcsv returns one line from the file as a TCL list
incr count
set nitems [llength $list]
set vstring [join $list {}]
# vstring contains only the data values to check for an empty line
if { [string length $vstring] == 0} {
set nitems 0 }
ns_adp_puts "line $count, $nitems items: "
# skip empty y lines
if { $nitems == 0 } {
ns_adp_puts "<p>"
} else {
set QQlist {}
foreach value $list {
regsub -all {'} $value {''} QQvalue
lappend QQlist $QQvalue
}
set string [join $QQlist "', '"]
# concatenates the list elements separated by ', '
set string "'$string'"
# puts a ' at the beginning and end of the values string
ns_adp_puts "$string "
set insert_sql "insert into \"$account\".$table ($cols) values ($string)"
if [catch {ns_db dml $db $insert_sql} dberr ] {
ns_adp_puts "<br><b>insert error<br>$dberr</b><p>"
} else {
ns_adp_puts "--Added<p>"
}
}
}
}
}
ns_adp_puts "<p>Finished loading file.<p>"
%>
<p><hr>
</body></html>

 

© 2001 by Chuck Ehrlich, all rights reserved.  Comments to webmaster.  Updated on November 08, 2000.