简体   繁体   English

在Perl中执行SQL文件

[英]Execute SQL file in Perl

We have a Perl script which runs a SQL and puts data in the table. 我们有一个Perl脚本,它运行SQL并将数据放入表中。 Now instead of supplying a single SQL statement, we want to pass bunch of them putting them together in a .sql file. 现在我们不想提供单个SQL语句,而是希望将它们串起来放在.sql文件中。 We know that our program will fail because it expects a single SQL statement, not s bunch of them (that too from a .sql file). 我们知道我们的程序会失败,因为它需要一个单独的SQL语句,而不是它们的一大堆(也来自.sql文件)。 How do we make it work with a .sql file (having multiple INSERT statements?). 我们如何使用.sql文件(具有多个INSERT语句?)。 We are using the DBI package. 我们正在使用DBI包。

A small snippet of code: 一小段代码:

$sth = $dbh->prepare("/home/user1/tools/mytest.sql");
$sth->execute || warn "Couldn't execute statement";
$sth->finish();

There is a sort of workaround for DDL. DDL有一种解决方法。 You need to slurp SQL file first and then enclose it's contents into BEGIN ... END; 您需要先粘贴SQL文件,然后将其内容包含在BEGIN ... END; keywords. 关键字。 Like: 喜欢:

sub exec_sql_file {
    my ($dbh, $file) = @_;

    my $sql = do {
        open my $fh, '<', $file or die "Can't open $file: $!";
        local $/;
        <$fh>
    };

    $dbh->do("BEGIN $sql END;");
}

This subroutine allows to run DDL (SQL) scripts with multiple statements inside (eg database dumps). 此子例程允许运行内部具有多个语句的DDL(SQL)脚本(例如,数据库转储)。

You don't need perl for this at all. 你根本不需要perl。 Just use the mysql command line client: 只需使用mysql命令行客户端:

mysql -h [hostname] -u[username] -p[password] [database name] < /home/user1/tools/mytest.sql

replace the [variables] with your information. 用您的信息替换[变量]。

Note no space after -u or -p. 注意 -u或-p后没有空格。 If your mysql server is running on the same machine you can omit -h[hostname] (it defaults to localhost) 如果您的mysql服务器在同一台机器上运行,您可以省略-h [hostname](默认为localhost)

Not exactly sure what you want... 不确定你想要什么......

Once you create a DBI object, you can use it over and over again. 创建DBI对象后,可以反复使用它。 Here I'm reading SQL statement after SQL statement from a file and processing each and every one in order: 在这里,我正在从文件中读取SQL语句之后的SQL语句,并按顺序处理每个语句:

use DBI;

my $sqlFile = "/home/user1/tools/mytest.sql"

my $dbh = DBI::Connect->new($connect, $user, $password)
    or die("Can't access db");

# Open the file that contains the various SQL statements
# Assuming one SQL statement per line

open (SQL, "$sqlFile")
    or die("Can't open file $sqlFile for reading");

# Loop though the SQL file and execute each and every one.
while (my $sqlStatement = <SQL>) {
   $sth = dbi->prepare($sqlStatement)
      or die("Can't prepare $sqlStatement");

   $sth->execute()
      or die("Can't execute $sqlStatement");
}

Notice that I'm putting the SQL statement in the prepare and not the file name that contains the SQL statement. 请注意,我将SQL语句放在prepare而不是包含SQL语句的文件名。 Could that be your problem? 这可能是你的问题吗?

Here is how I've done it. 我就是这样做的。 In my case I dont assume one SQL per line and I assume, my example is a bit better :) 在我的情况下,我不假设每行一个SQL,我认为,我的例子好一点:)

sub get_sql_from_file {
    open my $fh, '<', shift or die "Can't open SQL File for reading: $!";
    local $/;
    return <$fh>;
};

my $SQL = get_sql_from_file("SQL/file_which_holds_sql_statements.sql");
my $sth1 = $dbh1->prepare($SQL);
$sth1->execute();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM