how to put the data into the database.....?
#!/usr/bin/perl use strict ; use DBI; use XML::Twig; # prepare database my $dbh=dbh(); # connect init(); $dbh->do('USE db123'); #$dbh->do('DELETE FROM pois'); # sql my $sql = 'REPLACE INTO pois VALUES (?,?,?,?,?,?)'; my $sth = $dbh->prepare($sql); # set up handler my $t = XML::Twig->new( twig_handlers => { 'node' => \&node } ); # parse xml my $xml = do { local $/; <DATA> }; $t->parse($xml); #$t->parsefile('.osm'); sub node { my ($t,$elt) = @_; my %data=( 'id' => $elt->att('id'), 'lat' => $elt->att('lat'), 'lon' => $elt->att('lon'), ); for my $tag ( $elt->children() ){ $data{$tag->att('k')} = $tag->att('v'); #print $tag->att('k').' = '.$tag->att('v')."\n"; } # update database my @f = map{ $data{$_} }('id','lat','lon','name','amenity','operator +'); if ($f[3] ne '' && $f[4] ne '' && $f[5] ne ''){ print "-- INSERT --\n". (join "\n",@f). "\n\n"; $sth->execute(@f); } }
sub init { $dbh-> do('CREATE DATABASE IF NOT EXISTS db123 DEFAULT CHARACTER SET latin1 COLLATE latin1_german2_ci'); $dbh->do('USE db123'); $dbh->do('CREATE TABLE IF NOT EXISTS pois ( id BIGINT(20) UNSIGNED NOT NULL, lat FLOAT(10,7) NOT NULL, lon FLOAT(10,7) NOT NULL, name VARCHAR(255) COLLATE utf8_bin NOT NULL, amenity VARCHAR(255) COLLATE utf8_bin NOT NULL, operator VARCHAR(255) COLLATE utf8_bin NOT NULL, PRIMARY KEY (id) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin'); } sub dbh { my $dsn = "DBI:mysql:database=;host=localhost"; my $dbh = DBI->connect($dsn, 'user', 'pwd', {RaiseError => 1, PrintError => 1}) or die (Error connecting " $DBI::errstr"); } __DATA__ <?xml version='1.0' encoding='UTF-8'?> <osm> <node id="2064639440" lat="49.4873181" lon="8.4710548"> <tag k="amenity" v="restaurant"/> <tag k="cuisine" v="turkish"/> <tag k="email" v="info@lynso.de"/> <tag k="name" v="Kilim - Cafe und Bar Restaurant"/> <tag k="opening_hours" v="Su-Th 17:00-1:00; Fr, Sa 17:00-3:00"/> <tag k="operator" v="Cengiz Kaya"/> <tag k="phone" v="06 21 - 43 755 371"/> <tag k="website" v="http://www.kilim-mannheim.de/"/> </node> <node id="2126473801" lat="49.4851170" lon="8.4756295"> <tag k="amenity" v="restaurant"/> <tag k="cuisine" v="italian"/> <tag k="email" v="mannheim1@vapiano.de"/> <tag k="fax" v="+49 621 1259 779"/> <tag k="name" v="Vapiano"/> <tag k="opening_hours" v="Su-Th 10:00-24:00; Fr-Sa 10:00-01:00"/> <tag k="operator" v="Vapiano"/> <tag k="phone" v="+49 621 1259 777"/> <tag k="website" v="http://www.vapiano.de/newsroom/?store=29"/> <tag k="wheelchair" v="yes"/> </node> </osm>
I hope this answers your question.
poj

In reply to Re^3: from osm to mysql by poj
in thread from osm to mysql by Perlbeginner1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.