Hi Roberto,

I made your suggested changes and I got the same error mesage:

#!/usr/bin/perl 
use DBI;
use Mysql;
$infile = "/cadfs8/sys/dnazary/mysql_test_data1";
$dbh = DBI ->connect('DBI:mysql:dnazary_test;host=localhost', 'root', 'mysql');
open (IFH, $infile) or die ("Could not open input file.");
$query = $dbh->prepare ( 'INSERT into infiles (name,model) VALUES(?,?)') || die 
$query->errstr;
foreach (<IFH>) {
(@fields) = split / /, $_;
$query->execute($fields[0], $fields[1]);
}   
close IFH;
$dbh->disconnect();

dnazary ~ cadlab408 8:08:34am > ./mysql_write2.pl
DBD::mysql::st execute failed: Unknown column 'name' in 'field list' at 
./mysql_write2.pl line 16, <IFH> line 1.

and here is my DB:

mysql> use dnazary_test;
Database changed
mysql> describe infiles;
+--------+-------------+------+-----+---------+----------------+
| Field  | Type        | Null | Key | Default | Extra          |
+--------+-------------+------+-----+---------+----------------+
| keynum | int(10)     |      | PRI | NULL    | auto_increment |
| name   | varchar(10) |      |     |         |                |
| model  | varchar(10) |      |     |         |                |
+--------+-------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)


Regards
David


-----Original Message-----
From: Roberto Ruiz [mailto:[EMAIL PROTECTED]
Sent: Monday, June 09, 2003 7:34 PM
To: [EMAIL PROTECTED]
Subject: Re: Inserting data into MySql using perl script


Hi,

On Mon, Jun 09, 2003 at 04:49:41PM -0700, Nazary, David wrote:
> 
> I am unable to insert data into MySql with following script:
> 
> #!/usr/bin/perl 
> use DBI;
> use Mysql;

Not needed, you may comment it out.

> $infile = "/cadfs8/sys/dnazary/mysql_test_data1";
> $dbh = DBI ->connect('DBI:mysql:dnazary_test:localhost', 'root', 'mysql');

You need to correct the dsn. If the name of the database is
dnazary_test and you are trying to connect to localhost, the dsn for
mysql it should look like:

$dbh = DBI->connect('DBI:mysql:database=dnazary_test;host=localhost',
       'mysql_user_name', 'mysql_password');


> open (IFH, $infile) or die ("Could not open input file.");
> foreach (<IFH>) {
> (@fields) = split / /, $_;
> $query = $dbh->prepare ("INSERT into infiles (keynum,name,model) 
> VALUES('null','$fields[0]','$fields[1]')"); 
> $query->execute();
> }   

Prepare should be moved outside the foreach loop, and if you are going
to modify only two values, just name those two, like:

$query = $dbh->prepare('insert into infiles (name,model) values(?,?)');
foreach (<IFH>) {
        @fields = split / /, $_;
        $query->execute(fields[0], fields[1]);
}

> close IFH;
> $dbh->disconnect();
> 
> The error I get is:

> DBD::mysql::st execute failed: Unknown column 'name' in 'field list'
> at ./mysql_write1.pl line 21, <IFH> line 1.

Check the name of the fields in table infiles in the mysql command
line, like:

mysql> use dnazary
mysql> describe infiles;

Because the error tells you that there is no column whit the name
"name" in the table. Correct it as you found what the name of your
columns are. Also the error may be related to the way you specified
your database name in the dsn.

See you
Roberto Ruiz

--
A computer lets you make more mistakes faster than any other invention
in human history, with the possible exception of handguns and tequila.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to