gpt4 book ai didi

Perl DBI - 使用多条语句运行 SQL 脚本

转载 作者:行者123 更新时间:2023-12-01 15:21:13 24 4
gpt4 key购买 nike

我有一个 sql 文件 test.sql 用于运行一些看起来像这样的 SQL(创建对象/更新/删除/插入)

    CREATE TABLE test_dbi1 (
test_dbi_intr_no NUMBER(15)
, test_dbi_name VARCHAR2(100);

UPDATE mytable
SET col1=1;

CREATE TABLE test_dbi2 (
test_dbi_intr_no NUMBER(15)
, test_dbi_name VARCHAR2(100);

通常,我只会使用 SQLPLUS(从 Perl 中)使用以下命令执行此 test.sql:
@test.sql

有没有办法做同样的事情,在 Perl 中使用 DBI?
到目前为止,我发现 DBI 一次只能执行一条语句,而且没有“;”在末尾。

最佳答案

数据库控制一次可以执行多少条语句。我不记得 Oracle 是否允许每个 prepare 有多个语句与否(MySQL 确实如此)。尝试这个:

my $dbh = DBI->connect(
"dbi:Oracle:dbname",
"username",
"password",
{
ChopBlanks => 1,
AutoCommit => 1,
RaiseError => 1,
PrintError => 1,
FetchHashKeyName => 'NAME_lc',
}
);
$dbh->do("
CREATE TABLE test_dbi1 (
test_dbi_intr_no NUMBER(15),
test_dbi_name VARCHAR2(100)
);

UPDATE mytable
SET col1=1;

CREATE TABLE test_dbi2 (
test_dbi_intr_no NUMBER(15),
test_dbi_name VARCHAR2(100)
);
");

$dbh->disconnect;

当然,如果将语句分解,您将获得更好的错误处理。您可以使用简单的解析器将字符串分解为单独的语句:
#!/usr/bin/perl

use strict;
use warnings;

my $sql = "
CREATE TABLE test_dbi1 (
test_dbi_intr_no NUMBER(15),
test_dbi_name VARCHAR2(100)
);

UPDATE mytable
SET col1=';yes;'
WHERE col2=1;

UPDATE mytable
SET col1='Don\\'t use ;s and \\'s together, it is a pain'
WHERE col2=1;


CREATE TABLE test_dbi2 (
test_dbi_intr_no NUMBER(15),
test_dbi_name VARCHAR2(100)
);
";

my @statements = ("");
#split the string into interesting pieces (i.e. tokens):
# ' delimits strings
# \ pass on the next character if inside a string
# ; delimits statements unless it is in a string
# and anything else
# NOTE: the grep { ord } is to get rid of the nul
# characters the split seems to be adding
my @tokens = grep { ord } split /([\\';])/, $sql;
# NOTE: this ' fixes the stupid SO syntax highlighter
#this is true if we are in a string and should ignore ;
my $in_string = 0;
my $escape = 0;
#while there are still tokens to process
while (@tokens) {
#grab the next token
my $token = shift @tokens;
#if we are in a string
if ($in_string) {
#add the token to the last statement
$statements[-1] .= $token;
#setup the escape if the token is \
if ($token eq "\\") {
$escape = 1;
next;
}
#turn off $in_string if the token is ' and it isn't escaped
$in_string = 0 if not $escape and $token eq "'";
$escape = 0; #turn off escape if it was on
#loop again to get the next token

next;
}
#if the token is ; and we aren't in a string
if ($token eq ';') {
#create a new statement
push @statements, "";
#loop again to get the next token
next;
}
#add the token to the last statement
$statements[-1] .= $token;
#if the token is ' then turn on $in_string
$in_string = 1 if $token eq "'";
}
#only keep statements that are not blank
@statements = grep { /\S/ } @statements;

for my $i (0 .. $#statements) {
print "statement $i:\n$statements[$i]\n\n";
}

关于Perl DBI - 使用多条语句运行 SQL 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1232950/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com