473,405 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

ERROR WHEN UPLOADING new .CSV

1
hi everybody,

I have a script that inserts .csv data into a table;
I want different users to insert their data in the same table. To differenciate them I make them fill a name (called "nom") field in the html before uploading the .csv.

But when another user uploads data, the previous one disappears! I just use a INSERT INTO values, so it should not happen.

Sorry if there are big mistakes, but I'm newbie in Perl (the script is not mine, just modified). Thanks a lot!


Expand|Select|Wrap|Line Numbers
  1. use CGI qw(:standard);
  2. use DBI;
  3.  
  4. MAIN:{
  5.  
  6.  
  7.     # print webpage headers
  8.     print header;
  9.  
  10.     # variables that will hold form values and other information
  11.      my $query;
  12.     my $nom;
  13.      my $dbname1;
  14.     my $username1;
  15.     my $password1;
  16.       my $port1;
  17.       my $schema1;
  18.     my $host1;
  19.       my $dbh;
  20.     my $sth;
  21.      my $updir = 'C:\\Apache\htdocs\upload';
  22.     my $wfilename;
  23.  
  24.     # read values from html page
  25.       $query=new CGI;
  26.     $dbname1=$query->param("dbname");
  27.     print "dbname=$dbname1";
  28.       $username1=$query->param("username");
  29.       $password1=$query->param("password");
  30.     $nom=$query->param("nom");
  31.     print "NOM=$nom";
  32.  
  33.      $host1=$query->param("host");
  34.      $port1=$query->param("port");
  35.     print "port=$port1";
  36.       $schema1=$query->param("schema");
  37.  
  38.     # connect to database
  39.       $dbh = DBI->connect("dbi:PgPP:dbname=$dbname1;host=$host1;port=$port1",$username1, $password1) or die "Could not connect to database. Error: ".DBI->errstr;
  40.     print "dbh";
  41.  
  42.     $data_filehandle=$query->upload("datafile");
  43.     $wfilename="datafile_ainsertar.csv";
  44.     open(fileout,">$updir/$wfilename");
  45.     while ( <$data_filehandle> ) 
  46.     { 
  47.         print fileout;
  48.  
  49.     }
  50.     close(fileout);
  51.  
  52.     print "Data was successfully uploaded <br>";
  53.     print "NAME OF THE FILE TO PUT ON POSTGIS (CREAT ARA): $wfilename";
  54.     # read and insert data into cases
  55.     open (IN, "$updir\\datafile_ainsertar.csv");
  56.      while (<IN>) {
  57.         #Blank lines can creep into .csv files. Skip them.
  58.         next if (m/^\s*$/);
  59.         chomp;
  60.  
  61.         my ($id, $longitude, $latitude, $species, $genus, $family) = parse_csv($_);
  62.  
  63.     # Make all fields SQL-friendly
  64.  
  65.         $id = nullify_field($id);
  66.         $longitude = nullify_field($longitude);
  67.         $latitude = nullify_field($latitude);
  68.         $species = nullify_field($species);
  69.         $genus = nullify_field($genus);
  70.         $family = nullify_field($family);
  71.  
  72.         $sth=$dbh->prepare('insert into '.public.'.species6 (id, longitude, latitude, species, genus, family) values ('."$id".',\''."$longitude".'\',\''."$latitude".'\',\''."$species".'\',\''."$genus".'\',\''."$family".'\')'); 
  73.  
  74.         $sth->execute or die "Error inserting data into species table";
  75.  
  76.         $count = $count + $sth->rows;
  77.     };    
  78.     close IN;
  79.  
  80.     print "Data was successfully inserted <br />";
  81.  
  82.     # create geometry
  83.     $sth=$dbh->prepare('UPDATE '.public.'.species6 SET  the_geom = PointFromText(\'POINT(\' || longitude || \' \' || latitude || \')\',4326)');
  84.     $sth->execute;
  85.     print $nom;
  86.     print "Geometry was successfully created <br />";
  87.  
  88.     $sth2=$dbh->prepare('UPDATE '.public.'.species6 SET name = ?');
  89.     $sth2->execute($nom) or print "ERRRROR";
  90.  
  91.  
  92.     # ---------------------------------------------- Functions --------------------------------------------
  93.  
  94.     sub parse_csv {
  95.         # The goal here is to just go ahead and split on commas,
  96.         # and then find chunks that start with a " and assume that we've
  97.         # broken apart a field containing commas; re-join the chunks to
  98.         # the chunk beginning with " until we find a chunk ending with ".
  99.         my @chunks;
  100.         my $chunk;
  101.         my @fields;
  102.         my $field;
  103.         my $line = shift;
  104.  
  105.         @chunks = split(/,/, $line);
  106.  
  107.         my $i;
  108.         for ($i = 0; $i <= $#chunks; ++$i) {
  109.             $chunk = $chunks[$i];
  110.             $field = $chunk;
  111.             # If chunk starts wtih a double-quote but does not end with one,
  112.             if (substr($chunk, 0, 1) eq '"' && substr($chunk, -1, 1) ne '"') {
  113.                 # join the current chunk with the next chunk, replacing the
  114.                 # comma that got eliminated during the split on commas
  115.                 ++$i;
  116.                 $chunk = $chunks[$i];
  117.                 $field .= "," . $chunk;
  118.                 # and continue to do so until you find a chunk that ends with "
  119.                 # or you have run out of chunks.
  120.                 while (substr($chunk, -1, 1) ne '"' && $i <= $#chunks) {
  121.                     ++$i;
  122.                     $chunk = $chunks[$i];
  123.                     $field .= "," . $chunk;
  124.                 }
  125.                 # Our goal is to get rid of all field delimitors, so
  126.                 # get rid of the leading double-quote
  127.                 $field = substr($field, 1);
  128.                 # and the trailing double-quote.
  129.                 chop($field);
  130.             }
  131.             # If the chunk starts and ends with a double-quote,
  132.             if (substr($chunk, 0, 1) eq '"' && substr($chunk, -1, 1) eq '"') {
  133.                 # get rid of the leading double-quote
  134.                 $field = substr($field, 1);
  135.                 # and the trailing double-quote.
  136.                 chop($field);
  137.             }
  138.             # In CSV files, double quotes are escaped by doubling them up,
  139.             # so un-double them.
  140.             $field =~ s/""/"/g;
  141.             # Finally, we have a field that is completely usable, so add it to the
  142.             # array of fields we will return at the end of this subroutine.
  143.             push(@fields, $field);
  144.             # Clear the field for re-use in the next iteration of this loop.
  145.             $field = "";
  146.         }
  147.         return @fields;
  148.     }
  149.  
  150.     sub nullify_field {
  151.         # For SQL fields that do not need to be wrapped in single quotes.
  152.         # If a field is empty, replace it with the string "null",
  153.         # which can safely be used in sql insert statements.
  154.         my $field = $_[0];
  155.         if ($field) {
  156.             return $field;
  157.         } else {
  158.             return "null";
  159.         }
  160.     }
  161.     print "<b>hem escrit algo a la BD</b>";
  162.  
  163.     print end_html;
  164. }
  165.  
Sep 26 '07 #1
0 1354

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Marc | last post by:
Hello, I have a page where i can upload binary file (using the HTML input type=file approach). This works fine for relatively small files (<6MB)) but when files get bigger (13MB) there is a...
1
by: Nikhil | last post by:
I have a VBscript that I use to upload files onto the server. The script works fine on IIS 5.0 but on IIS 6.0 on Windows 2003 I get an error when uploading certain files. I believe its because the...
7
by: Joe | last post by:
I have an upload file operation in the web application. UploadForm.asp is the form, and UploadAction.asp is the form processing. //UploadForm.asp <FORM NAME="InputForm"...
5
by: hb | last post by:
Hi, In my ASP.Net application 'MyWebApp' , the mode="StateServer" in <sessionState> of Web.config file, and the ASP.NET State Service is set to start automatically on the server. But every...
5
by: Nathan Sokalski | last post by:
I am trying to write code to allow my users to upload a file. The code I am using is as follows: Dim upfilename As String = "" If fileDetails.Value <> "" AndAlso...
3
by: J055 | last post by:
Hi How do I tell the user he has tried to upload a file which is too big... 1. when the httpRuntime.maxRequestLength has been exceeded and 2. when the uploaded file is under then...
1
by: wenqiang7 | last post by:
I am encountering a very strang problem with file uploading in my ASP.Net page. When we try to upload certain file, we'll get an error msg of "Cannot find server or DNS Error". We are running...
1
by: tom_burrow | last post by:
hi, i am hoping that someone may be able to help me... i have been writing vb.net pages using visual web developer. they all work fine locally however when i upload to the server i get the...
6
by: SayamiSuchi | last post by:
hi, I have taken two file upload control (fileUpload1 and fileUpload2) and two required field validator(requiredFieldValidator1 and requiredFieldValidator2) for the two upload controls mentioned...
4
rahulephp
by: rahulephp | last post by:
i think i am missing something in the below script: It shows error an do not upload files to destination: Let me know how to solve this: <?php if (isset($_POST)) { $uploadArray=...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.