Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Import dump file into mysql DB using DBI

by siva kumar (Pilgrim)
on Oct 11, 2007 at 10:58 UTC ( [id://644185]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,
I have a mysql dump "xyz.sql". I want to use DBI package for DB connection. Using database handle I want to import the dump file into DB. What I have done is :
open(FH,"xyz.sql"); $/ = EOF; $sql = <FH>; $sth = $dbh->prepare("$sql"); $sth->execute();
The above code doesn't seems to be working. It result error as
DBD::mysql::st execute failed: You have an error in your SQL syntax; c +heck the manual that corresponds to your MySQL server version for the + right syntax to use near '; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 S' at line 7 at resultsDBsource.pl line 34, <FH> chunk 1.
Please advice .. Thnx

Replies are listed 'Best First'.
Re: Import dump file into mysql DB using DBI
by Corion (Patriarch) on Oct 11, 2007 at 11:01 UTC

    You are sending multiple statements to MySQL and it seems that it does not like this. I often use $/ = ");\n" to read in a MySQL dump and feed it to a database statement by statement with little overhead.

Re: Import dump file into mysql DB using DBI
by graff (Chancellor) on Oct 11, 2007 at 12:29 UTC
    This last part of the error message:
    ... near '; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 S' at line 7 at resultsDBsource.pl line 34, <FH> chunk 1.
    looks sort of like the dump file has been edited in some way to place C-style commenting marks around some lines. I shouldn't be surprised of mysql rejects this (the normal way to comment out a line of SQL is to put two hyphens at the beginning.)

    (UPDATE: I should have checked the mysql manual first; C-style comments are allowed, and comments starting with /*! are "special": not ignored, but used to flag statements that use mysql-specific extensions to SQL, and the digits following /*! indicate what (earliest) version of mysql is needed for the extended statement to work. What do you know about the version of the server you are using?)

    Rather than reading the whole file as a into a single scalar, you should use the idea mentioned above, setting $/ to the string that marks the end of an sql statement, and read the file one statement at a time, and -- this is important -- on each statement, check for "/*commented strings*/" and remove them before preparing the statement.

    If you are trying to load a database on a server where you do not have login shell access, you might want to have mysql running on your own machine, and try loading the dump file locally via a shell command line:

    mysql test < dump.file
    will load the file contents into the "test" database. Revise the dump file until that operation produces the desired result, then load the dump file on the remote server.

    (If you are able to connect to the target database from anywhere via a login-shell "mysql" command, why not just load the dump file that way?)

      Hi,
      I don't have access to the target DB machine.
      I have to automate the perl script, the script will search for ".sql" files and those files have to updated to DB.
        I still prefer to use mysql command line for this task. If you think you have to use a perl sript, then I assume you have remote access to the DB server, in which case you can also use the mysql client from your host.

        SQL files (.sql) are meant to be executed in batch mode, each statement is terminated with a semicolon (";"). With DBI, you execute query one statement at a time and semicolons are disliked :-)

        Finally, if you still need a perl script to automate DB updating (still assuming you the remote access), this might help (sorry, I don't bother to test it).

        #!/usr/bin/perl use strict; use warnings; my $sql_file = '/path/to/sql/file.sql'; exit 0 # silently unless -r $sql_file; my $user = 'user'; my $pass = 'pass'; my $db = 'somedb'; system "mysql -u$user -p$pass $somedb < $sql_file" and die "can't update $db\n";
        Also, you may want to consider about DB backup and/or hotcopy, as both facilities are provided by mysql, AFAIK.

        Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

        You can always do a system "mysql $connection_data < $sql_file" (assuming that the mysql client is installed on the machine where the script is running)
Re: Import dump file into mysql DB using DBI
by roboticus (Chancellor) on Oct 11, 2007 at 11:38 UTC
    siva kumar:

    Use the command-line interface and check out the command in your $sql variable. Once you get it working there, then try it with your code.

    I can't be any more specific, because the error message indicates a problem in your SQL which you haven't bothered to provide.

    ...roboticus

Re: Import dump file into mysql DB using DBI
by rayzit (Initiate) on Oct 11, 2007 at 12:17 UTC
    you have to escape "'" and similar text elements in the sql like this \', maybe qq or qw $sql will help

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-25 07:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found