473,386 Members | 1,733 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,386 software developers and data experts.

adding columns to db at runtime and inserting to db

file test.txt where first entry is the name in db columns and second is name that is in html files used for parsing.
i make a search using the second elements on every line
name and stream location has a _ before and after becuause i dnt parse them from file . i simply assign them value /

name _Name_
age age
stream_loc _Stream Location_
html file to be parsed is a simple file
<html><body> age: 2<br></body></html>
i can print the line where age;2 occured but somehow when i extract it, it doesnt go into db as 2 but as "absent".

this means the html file entries are not being parsed.or if parsed but not assinged to the hash variable ..

there is no problem with first and last element i can get their values correctly in db. but not for age.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #!/perl/bin/perl
  3.  
  4. use CGI qw(:standard);
  5. use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
  6. use File::Find;
  7. use DBI;
  8. use HTML::Scrubber;
  9. use Tie::IxHash;
  10.  
  11. print header;
  12.  
  13. my $count=0;
  14. my $lines;
  15.  
  16.  
  17. #===============================================================================
  18. #first entry is the database column name and second is the file parameter name which is to be parsed from file
  19. open FILE,"test.txt";
  20. while(<FILE>)
  21. {
  22.     push @query_ref,[split];
  23. }
  24. #print $query_ref[1][1];
  25.  
  26. close (FILE);
  27. #===============================================================================
  28.  
  29. #Defining directory of the location of streams
  30. my $dir="C:/type";
  31.  
  32. #Connection String
  33. my $dbh=DBI->connect('dbi:Oracle:test1','scott','tiger',{RaiseError => 1, AutoCommit => 1})or die "Can't connect to database $DBI::errstr\n";
  34.  
  35. my %params=();
  36.  
  37. find(\&edits,$dir);
  38.  
  39.     sub edits
  40.     {
  41.         if (/\.html$/)  #looks for html files in folder
  42.         {
  43.         $count++;
  44.         print "<BR>$count)..<BR>";
  45.  
  46.  
  47.  
  48.         my $scrubber = HTML::Scrubber->new;     #initialize html scrubber
  49.  
  50.  
  51.  
  52.         tie %params,"Tie::IxHash";
  53.  
  54.         open(FILE,$File::Find::name) or die $!;
  55.  
  56.  
  57.  
  58.         while(defined(my $line = <FILE>))
  59.             {
  60.         my $line= $scrubber->scrub($line);    #scrub all html Tags
  61.  
  62.         $params{$query_ref[0][1]}{$_} = 1;
  63.         $params{$query_ref[1][1]}={};
  64.         $params{$query_ref[2][1]}{$File::Find::name} = 1;
  65.  
  66.  
  67.         foreach my $key (keys %params)
  68.         {   
  69.             if($line=~/$key/)
  70.             {
  71.             print "<br>$line";  #when age: 23 appears it prints #correctly
  72.             if($line =~ m/: (.*)/)
  73.             {                    
  74.                 $params{$key}{$1} = 1;        #<<<==i think here is #prob
  75.             }
  76.             }
  77.         }    
  78.         #------------------SPECIAL CASES------------------------------------------------
  79.  
  80.         }
  81.  
  82.     }
  83.  
  84.     # Transform hash data into 'xxx,yyy,zzz' forms as needed
  85.     foreach my $key (keys %params)
  86.     {
  87.         $params{$key} = join(',', keys(%{$params{$key}}));
  88.  
  89.         if (!$params{$key})
  90.         {
  91.         $params{$key} = 'Absent';
  92.         }    
  93.     }
  94.  
  95.     for my $key ( keys %params ) 
  96.     {
  97.        my $value = $params{$key};
  98.        print "<Br>$key => $value<BR>";
  99.     }
  100.  
  101.  
  102.         my $query = "insert into pooja values ('" .join("','", map { $params{$$_[1]} } @query_ref) . "')";
  103.         my $sql=qq{$query};
  104.         my $sth=$dbh->prepare($sql);
  105.         $sth->execute();
  106.           close(FILE);
  107.     }
  108.  
  109.  
  110. print "<br>$count files updated"; 
  111. $dbh->disconnect if defined($dbh);
  112.  
  113.  
  114.  
  115.  
Aug 17 '09 #1
3 1614
nithinpes
410 Expert 256MB
The problem is in this line:
Expand|Select|Wrap|Line Numbers
  1. $params{$query_ref[1][1]}={};
  2.  
With each iteration of the while loop, value for the key 'age' will be re-assigned to an anonymous hash, overwriting the value (key-value pair) assigned in:
Expand|Select|Wrap|Line Numbers
  1. $params{$key}{$1} = 1; 
  2.  
The structure for "age" key is different from others in your script. If that is how you want it to be, define that line before while loop:

Expand|Select|Wrap|Line Numbers
  1. $params{$query_ref[1][1]}={};
  2.  
  3. while(defined(my $line = <FILE>)) 
  4.  
Aug 20 '09 #2
Thanks for the reply. i did that . but then "Age" doesnt show up at all in the final hash entries. i can just see two values Name and stream location

also i noticed one more thing. with the initial same code if i input a text file it works very well and fetches age 's value too.

then y not in html. as u can see i have used scrubber package too that scrubs all html tags.
Aug 20 '09 #3
hey.. in addition to your suggestion for shifting that line up it was also
tie %params,"Tie::IxHash"; function which i had placed inside the sub/ it should be in the declaration part exactly after declaration of hash.

my %params;
tie %params,"Tie::IxHash";
find(\&edits,$dir);

thankyou so much for your help. it works very well now for all files :-) :-)
Aug 20 '09 #4

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

Similar topics

2
by: Gail Zacharias | last post by:
I am investigating the possibility of using pgsql as the database in an application. I have some unusual requirements that I'd like to ask you all about. I apologize in advance if my terminology is...
2
by: Viorel | last post by:
Adding new row with default values. In order to insert programmatically a new row into a database table, without direct "INSERT INTO" SQL statement, I use the well-known DataTable.NewRow,...
1
by: Chip | last post by:
I've got a datagrid defined on the design page. At runtime, I'd like to add a bound column. I'm using Dim CallNum As New BoundColumn() CallNum.DataField = "call_num" CallNum.HeaderText = "Call #"...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
3
by: Fao, Sean | last post by:
I have a DataGrid that I'm adding CheckBox controls to at runtime (in the code behind) and I'm not sure if I'm doing it correctly. First of all, I noticed that the MyDataGrid.Columns.Add() method...
4
by: Eamonn | last post by:
Hi I am running Microsoft Visual Studio 2005 Team Edition Version 8.0.50727.42. I am adding columns to a GridView at runtime. When I add a BoundField that will contain DateTime data I want to...
13
by: Jo | last post by:
Hi. I'm getting the following error when creating a table with 250 columns .. I have tried creating it in a 32K tablespace , still the same issue. Is this a limitation in DB2? I am using DB2...
5
by: explode | last post by:
I made a procedure Public Sub Novo(ByVal nova1 As String, ByVal nova2 As String) that creates a new oledbDataAdapter with insert update select and delete commads. I also added that commands can...
2
by: gkinu | last post by:
Hello, I am creating a report that can have a varying number of columns that are only determined at run time. I saw a solution that comes near what i want. I downloaded a collection of sample...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.