Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

DBD::mysql::db do failed: You have an error in your SQL syntax;

by shajiindia (Acolyte)
on Jan 05, 2015 at 11:20 UTC ( [id://1112169]=perlquestion: print w/replies, xml ) Need Help??

shajiindia has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I am running a database utility and I am creating a database through a menu driven script. I am getting the following error

DBD::mysql::db do 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 '-secure-test' at line 1 at C:/Users/Sennovate/workspace/Perl/Automation/Sennovate/MySQL DB Creation/ssg-db-utility.pl line 79, <STDIN> line 5.

I am using Strawberry Perl 5.20.1

Here is my code snippet

sub create_database { print "Please enter the hostname:"; chomp($hostname = <STDIN>); print "Please enter the name of the new MySQL database:"; chomp($database = <STDIN>); $dsn = "dbi:$driver::$hostname:$port"; print "Please type the MySQL root username:"; chomp($username = <STDIN>); print "Please type the MySQL root password:"; chomp($password = <STDIN>); $dbh = DBI->connect($dsn, $username, $password); $result = $dbh->do("create database $database"); $dbh->do("use $database"); if($result) { say "Database '$database' created successfully"; } else { say "Database '$database' creation failed"; exit; } }

Here is the input

---------------------------------------- S E N N O V A T E I N C SSG Database Utility - Version 1.1 Copyright Sennovate Inc, 2014 ---------------------------------------- M E N U ------- 1. Create new database 2. Connect to existing database 3. Create tables 4. Create user 5. Drop existing database 6. Quit Please enter your choice:1 Please enter the hostname:db0002.stage-us.sennovate.com Please enter the name of the new MySQL database:ssg-secure-test Please type the MySQL root username:ssg-stage Please type the MySQL root password:p7ZUrf59zJHYMMuLKfWTxC DBD::mysql::db do failed: You have an error in your SQL syntax; check +the manual that corresponds to your MySQL server version for the righ +t syntax to use near '-secure-test' at line 1 at C:/Users/Sennovate/w +orkspace/Perl/Automation/Sennovate/MySQL DB Creation/ssg-db-utility.p +l line 79, <STDIN> line 5. Database 'ssg-secure-test' creation failed DBD::mysql::db do failed: You have an error in your SQL syntax; check +the manual that corresponds to your MySQL server version for the righ +t syntax to use near '-secure-test' at line 1 at C:/Users/Sennovate/w +orkspace/Perl/Automation/Sennovate/MySQL DB Creation/ssg-db-utility.p +l line 80, <STDIN> line 5.

Please help

Replies are listed 'Best First'.
Re: DBD::mysql::db do failed: You have an error in your SQL syntax;
by choroba (Cardinal) on Jan 05, 2015 at 11:23 UTC
    Can a database name contain a dash? If so, can you use such a name unquoted in a command?
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: DBD::mysql::db do failed: You have an error in your SQL syntax;
by KurtSchwind (Chaplain) on Jan 05, 2015 at 12:36 UTC
    Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.1.56 Source distribution Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights res +erved. This software comes with ABSOLUTELY NO WARRANTY. This is free software +, and you are welcome to modify and redistribute it under the GPL v2 lic +ense Type 'help;' or '\h' for help. Type '\c' to clear the current input st +atement. mysql> create database test-dash; ERROR 1064 (42000): You have an error in your SQL syntax; check the ma +nual that corresponds to your MySQL server version for the right synt +ax to use near '-dash' at line 1 mysql>

    You cannot put dashes in database names.

    mysql> create database 'test-dash'; ERROR 1064 (42000): You have an error in your SQL syntax; check the ma +nual that corresponds to your MySQL server version for the right synt +ax to use near ''test-dash'' at line 1 mysql> create database "test-dash"; ERROR 1064 (42000): You have an error in your SQL syntax; check the ma +nual that corresponds to your MySQL server version for the right synt +ax to use near '"test-dash"' at line 1 mysql> create database test\-dash; ERROR: Unknown command '\-'. ERROR 1064 (42000): You have an error in your SQL syntax; check the ma +nual that corresponds to your MySQL server version for the right synt +ax to use near '\-dash' at line 1
    --
    I used to drive a Heisenbergmobile, but every time I looked at the speedometer, I got lost.

      You can have a dash in a database name if it is done inside backticks

      mysql> create database `test-dash`; Query OK, 1 row affected (0.05 sec)
        Thanks rnewsham. It worked like a champ Modified code
        sub create_database { print "Please enter the hostname:"; chomp($hostname = <STDIN>); print "Please enter the name of the new MySQL database:"; chomp($database = <STDIN>); $dsn = "dbi:$driver::$hostname:$port"; print "Please type the MySQL root username:"; chomp($username = <STDIN>); print "Please type the MySQL root password:"; chomp($password = <STDIN>); $dbh = DBI->connect($dsn, $username, $password); $result = $dbh->do("create database `$database`"); $dbh->do("use `$database`"); if($result) { say "Database '$database' created successfully"; } else { say "Database '$database' creation failed"; exit; } }

        Forgot about back-tick quoting in MYSQL. Yep. That'll work.

        --
        I used to drive a Heisenbergmobile, but every time I looked at the speedometer, I got lost.
Re: DBD::mysql::db do failed: You have an error in your SQL syntax;
by sundialsvc4 (Abbot) on Jan 05, 2015 at 13:39 UTC

    And of course we may presume that this is an internal application that can be trusted not to be in the hands of any internal employees named Bobby Tables, right?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1112169]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-23 12:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found