develooper Front page | perl.beginners | Postings from July 2003

DBI execute problem

Thread Next
From:
Ramprasad
Date:
July 17, 2003 04:34
Subject:
DBI execute problem
Message ID:
3F1689BA.1080508@netcore.co.in
Hello all,

I have a mysql database and I want to create tables
w1 w2 w3 w4 ... w30

Can I use the prepare and execute methods of DBI
I am getting an error because DBI is quoting the table name and Mysql is 
not accepting it

This is my code

#!/usr/bin/perl
use DBI;
use strict;

my $dbh = DBI->connect( 'DBI:mysql:database=words;host=;port=3306', "", "" )
     or die "Can't connect to Mysql database: $DBI::errstr\n";

my $sql="CREATE TABLE ? (word VARCHAR( ? ) DEFAULT 'a' NOT NULL ,'wkey' 
VARCHAR( ? ) DEFAULT 'a' NOT NULL ,PRIMARY KEY ( word ))";
my $sth = $dbh->prepare( $sql ) || die $dbh->errstr;

my($i);
foreach $i ( 1..30){
   $sth->execute("w$i",($i+1),($i+1)) || die $dbh->errstr;
   print "Created w$i\n";
}


Error

DBD::mysql::st execute failed: You have an error in your SQL syntax. 
Check the manual that corresponds to your MySQL server version for the 
right syntax to use near ''w1' (word VARCHAR( 2 ) DEFAULT 'a' NOT NULL 
,'wkey' VARCHAR( 2 at ./createsql.pl line 13.



The problem is with the execute statement
I know I can use the do statement and put the entire sql in the loop but 
Is there a way I can so it using prepare and execute


Thanks
Ram


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About