Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: MySQL Table adding with Perl

by Anonymous Monk
on Mar 06, 2013 at 20:51 UTC ( #1022090=note: print w/ replies, xml ) Need Help??


in reply to Re^2: MySQL Table adding with Perl
in thread MySQL Table adding with Perl

Still not working

use DBI; #Welcoming Phrase print "\n\n Welcome to the world.\n\n"; #Load username database print " Are you a new user? (y/n)\n\n"; my $choice=<STDIN>; chomp $choice; if ($choice eq "n") { print " What is your username?\n\n"; my $username = <STDIN> ; chomp $username; my $connect = DBI->connect( "dbi:mysql:database=Champ;host=localhost", 'root','pass'); print "User $username Active\n"; } if ($choice eq "y") { print " What would you like your username to be?\n\n"; my $newuser = <STDIN>; chomp $newuser; my $connect = DBI->connect( "dbi:mysql:database=Champ;host=localhost", 'root','pass'); my $dbh->do("DROP TABLE IF EXISTS $newuser"); $maketable = ("CREATE TABLE $newuser (A,B,C,D,E)"); my $sql = "INSERT INTO $newuser (A,B,C,D,E) VALUES (?,?,?,?,?)"; $sth = $dbh->prepare($maketable); $sth = execute(); $statement = $connect->prepare($sql); $statement = execute(4,4,4,4,0); print "User $newuser Active\n";


Comment on Re^3: MySQL Table adding with Perl
Download Code
Re^4: MySQL Table adding with Perl
by Corion (Pope) on Mar 06, 2013 at 21:03 UTC

    If you use the following to connect to the database, DBI will tell you more about where things (maybe) go wrong:

    my $connect = DBI->connect( "dbi:mysql:database=Champ;host=localhost", 'root','pass', { RaiseError => 1 }, );

    This saves you adding the error checking in every situation.

    A bit more advice - I like to read passwords from a separate file, or from %ENV or anywhere else. That way, I can't inadverently post the database password when posting (or printing) my script.

      Unfortunately with the addition of that line, there was no output. The scripts runs without error. Thanks Corion for the advice about the passwords, I will definitely keep that in mind.

        Your code cannot run at all:

        $maketable = ("CREATE TABLE $newuser (A,B,C,D,E)");

        This is not valid SQL (except maybe in SQLite). But that doesn't matter because you never run that statement.

        $sth = execute();

        This should never work. You need to call the ->execute() method on your statement handle. Unless you also have defined (but not shown to us) a subroutine with the name execute.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1022090]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (11)
As of 2014-09-17 18:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (95 votes), past polls