To: erix,

Nice!!! I had no clue that MySQL syntax can be applied like this.

To be honest out of curriocity I created a small working example:

#!/usr/bin/perl use strict; use warnings; use DBI; my $username = "username"; my $password = "password"; my $tut_id = "10"; my $tut_title = "title"; my $tut_author = "author"; my $dsn = "dbi:mysql:thanos:127.0.0.1:3306"; my $dbh = DBI->connect($dsn,$username,$password) or die "cannot connect to database : $DBI::errstr"; my $sth = $dbh->prepare(qq{INSERT INTO `test` VALUES(?,?,?)}); $sth->execute("$tut_id","$tut_title","$tut_author") || die $DBI::errst +r; $dbh->disconnect;

And I am getting this error:

DBD::mysql::st execute failed: Column count doesn't match value count +at row 1 at db.pl line 18.

The moment that I am placing the columns e.g. (`column-1`,`column-2`,`column-3`) the code executes withought any erorrs.

I tried the same syntax on phpMyadmin:

INSERT INTO `test` VALUES ('10','title','author')

error output:

#1136 - Column count doesn't match value count at row 1
ERROR 1136 (21S01): Column count doesn't match value count at row 1

And also at the MySQL (terminal):

ERROR 1136 (21S01): Column count doesn't match value count at row 1

At this point maybe my version is different.

Well anyway, back to the question. The error should be coming of the avoidance of the quotes on the strings:

Before:

$sth->execute($tut_id,$tut_title,$tut_author) || die $DBI::errstr;

After:

$sth->execute("$tut_id","$tut_title","$tut_author") || die $DBI::errst +r;

Wow so many things that we learn in this forum, I had also no clue about (sqlfiddle). Thanks for sharing. :D

Update:

I think I know the reason that my code withought specifying the columns can not work

From 13.2.5 INSERT Syntax text taken:

If you are not running in strict SQL mode, any column not explicitly given a value is set to its default (explicit or implicit) value. For example, if you specify a column list that does not name all the columns in the table, unnamed columns are set to their default values.

So I assume this syntax can work on SQL but not on MySQL that I am using.

But it was nice that you pointed out, someone who is using SQL will be interested on this.


In reply to Re^3: Not able to insert data in mysql by thanos1983
in thread Not able to insert data in mysql by ashishg0310

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.