in reply to Re^4: Relational table with perl DBI
in thread Relational table with perl DBI

Thank you very much for your answer. I though that at some point I'll have to use hashes, but I was always afraid of them. I understand your code and I tried to incorporate it in my own. I've already seen on the web the @{$hr_output}{@fields} syntax, though in this context it's the only time when I get it.

However I have a small program in the code. I get :

"DBD::mysql::st execute failed: Unknown column 'event' in 'field list' at db2.pl line 289. DBD::mysql::db selectcol_arrayref failed: Unknown column 'LAST_INSERT_ID' in 'field list' at db2.pl line 292. Can't use an undefined value as an ARRAY reference at db2.pl line 292. "

I don't really see where do the script bug.

And also the first "if" error managment loop gives me :

"Use of uninitialized value $inserted_records in numeric ne (!=) at db2.pl line 290. Use of uninitialized value $inserted_records in concatenation (.) or string at db2.pl line 290. Error inserting article http://rss.feedsportal.com/c/32788/f/524037/s/29456ceb/l/0Llci0Btf10Bfr0Cfrance0Cfaits0Edivers0Chautes0Epyrenees0Eemportes0Epar0Eune0Eavalanche0Eils0Es0Een0Esortent0Epresque0E78664640Bhtml/story01.htm, only [] got inserted: Unknown column 'event' in 'field list' at db2.pl line 290."

Though thank you very much again.

Replies are listed 'Best First'.
Re^6: Relational table with perl DBI
by M15U (Acolyte) on Mar 13, 2013 at 14:06 UTC

    I think I know where the problem is. When I print "@fields" if get : concord_file sys_time url event

    The thing is I have two tables:

    article : id_article url html_extr concord_file sys_time

    event : id_event event

    I don't really know how to reorder this two fields

      Now I really got it. Using the placeholders method for filling the tables requires the precise order of the column. Using a hash means that all data is scrambled

      Plus, in this aproach the 'event' column is in the same table as the article.

      Is there a away to reorder a hash index following a specific pattern ? how can I get the 'event' column to be treate separetly of the article table ?

        That was what @{$hr_output}{@fields} was supposed to catch....it gives you the values of the hash in the order of @fields.
        You don't have to follow the exact same order as the fields are defined in the table, but you will have to follow the same order you specified in the INSERT-query, which is why I used @fields a lot at that point.
        Also, it seems I made a slight mistake for the last_insert_id-select. It should be<br? my $sth_select_last_insert_id = $dbh->prepare( qq( SELECT LAST_INSERT_ID() )); (Note the added ()).
        The other errors are all a result of this. First the query fails (syntax error), which results in an undef result.
        But when executing it (at my $article_id = ($dbh->selectcol_arrayref($sth_select_last_insert_id))->[0]; I immediately dereference it. But undef is not an arrayref, so that won't do (and perl complains about that).
        Then when trying to insert the undef (which is NULL when stored in a database) in the article_event_index table, an error occurs, which leaves $inserted_records containing undef. When comparing that to a value, perl warns (but proceeds anyway) about that.