kcott already gave you some valid pointers for your problem.

However, if you have to do more work on this database or perhaps you need to write more scripts to extract data or manipulate data, then it is a good investment of your time to look into something like DBIx::Class].

Yes, it takes some learning and it takes some time to set-up the classes for your database, especially if you want to have the relational part right, but once you have done that, using this database becomes a breeze.

Believe me I speak from experience: before I used DBIx::Class I had to write every time I needed to write a new script for that database, again and again very similar code (connecting to the database, writing SQL, setting up the loops to select, insert, delete, ...). Now all these chores just become standard methods of your class.

Adding a next record, could become as simple as:

my $new_school = $schema->resultset('Schools')->create({ location => 1, name => 'mySchool', type => 'HighSchool', adress => 'Paris Champs Ellysees 1', description => 'Ecole Superieur', });

Searching for a record is equally simple:

my $rs = $schema->resultset('Schools')->search({name => 'mySchool'});
Once found, you could extract the fields like this:
my $school = $rs->first; print $school->adress;
Updating the record? Even easier!
$school->adress('New address of the school'); $school->update;

BTW: for your problem, try adding some or die $dbh->errstr; code to your do statements. You will then be able to see what went wrong.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James


In reply to Re: DBI insert: Need to store results in a MySQL-database (not in a file) by CountZero
in thread DBI insert: Need to store results in a MySQL-database (not in a file) 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.