Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

DBD ERROR: error possibly near <*> indicator at char

by suhailck (Friar)
on Mar 25, 2009 at 06:40 UTC ( [id://753029]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, Im new to perl and i wrote a script for oracle DB using DBI
The program is as follows,
#!/usr/bin/perl use DBI; use DBD::Oracle; my $dbh=DBI->connect("dbi:Oracle:XE","suhail","suhail",<br>{AutoCommit +=>0,RaiseError=>1})||die "Can't connect $DBI::errstr"; $sql='insert into u_configname values (?,?,?,?,?,?,?,?,?,?,?)'; open(MY,"Desktop/b1.txt"); while(<MY>) { my($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k)=split /,/; eval { my $sth=$dbh->prepare($sql); $sth->execute($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k)||die "$!"; $dbh->commit; }; if ($@) { $dbh->rollback; } } close(MY); END { $dbh->disconnect if defined $dbh; }

After running the above program it shows error like,
DBD::Oracle::st execute failed: ORA-01722: invalid number (DBD ERROR: error possibly near <*> indicator at char 75 in 'insert into u_configname values (:p1,:p2,:p3,:p4,:p5,:p6,:p7,:p8,:p9,:p10,:<*>p11)') for Statement "insert into u_configname values (?,?,?,?,?,?,?,?,?,?,?)" with ParamValues: :p1='0', :p10='09-JAN-2009', :p11='1000003 ', :p2='0', :p3='09-JAN-2009', :p4='1000003', :p5='SO_PRICE_LIMIT', :p6='Y', :p7='priceLimit', :p8='PriceLimit', :p9='1000000' at ora4.pl line 14, <MY> line 1.
.............................
And the u_configname table description is as follows
SQL> desc u_configname;
Name Null? Type
----------------------------------------- --------
----------------------------
AD_CLIENT_ID NOT NULL NUMBER(10)
AD_ORG_ID NOT NULL NUMBER(10)
CREATED NOT NULL DATE
CREATEDBY NOT NULL NUMBER(10)
IDENTIFIER NOT NULL VARCHAR2(60)
ISACTIVE NOT NULL CHAR(1)
NAME NOT NULL VARCHAR2(60)
PRINTNAME NOT NULL VARCHAR2(60)
U_CONFIGNAME_ID NOT NULL NUMBER(10)
UPDATED NOT NULL DATE
UPDATEDBY NOT NULL NUMBER

Can anyone help me on this problem plz...............

Replies are listed 'Best First'.
Re: DBD ERROR: error possibly near <*> indicator at char
by afoken (Chancellor) on Mar 25, 2009 at 08:25 UTC

    Look at the error message: The last parameter has whitespace (the newline character) appended. It is there because perl does not auto-chomp while reading from a file, you have to do it. Just add a chomp; as first statement in your while loop.

    Note that your code is not very efficient: prepare the statement outside the loop, execute it for each loop run, and finish it after the loop. The eval-commit-construction actually just compensates your explicit AutoCommit => 0 in connect(), so remove one. If you want to commit either all or nothing, wrap the entire while loop in an eval, move the commit behind the loop, still inside the eval, and issue a rollback if eval throws an exception.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      Also depending on your requirements you may want commit points inside the loop so you issue a commit every n inserts.
      Thank u very much, Alexander :)
      @afoken I am not able to find how to chomp the <*> of before value e the 5th value of array var1 .. please help guys
      my $sql2 = 'select name,value from A.Database2 minus select name,value + from A.database1'; my $sth2 = $dbh1->prepare($sql2); $sth2->execute(); while (my @row = $sth2->fetchrow_array) { print join(',', @row), "\n\n"; chomp ; push(@var1,$row[0]); } foreach (@var1) { print "$_\n"; } my $sth3 = $dbh->prepare("select name,value from A.database1 where nam +e in (".join(",",@var1).")") or die "Can't prepare statement: $DBI::e +rrstr"; $sth3->execute(@var1); while (my @row = $sth3->fetchrow_array) { print join(", ", @row), "\n"; DBD::Oracle::db prepare failed: ORA-00904: "APR_IMDG_MODE_ENABLED": in +valid identifier (DBD ERROR: error possibly near <*> indicator at cha +r 144 in 'select name,value from A where name in (a,b,c,d,<*>e)') [fo +r Statement "select name,value from A where name in (a,b,c,d,e)"] at +hello.pl line 102. Uncaught exception from user code: Can't prepare statement: ORA-00904: "APR_IMDG_MODE_ENABLED": i +nvalid identifier (DBD ERROR: error possibly near <*> indicator at ch +ar 144 in 'select name,value from where name in (a,b,c,d,<*>e)') at +hello.pl line 102. at hello.pl line 102

        You need to properly quote your values. Since they're strings, they should have quotes around them so the SQL should look like:

        select name,value from A where name in ('a','b','c','d','e')

        While you could do it something like this:

        # Make a quoted list of strings my @quoted_vals = map { "'$_'" } @var1; my $sth3 = $dbh->prepare("select name,value from A.database1 where nam +e in (" . join(",",@quoted_vals) . ")" ) or die "Can't prepare statement: $DBI::errstr";

        you probably don't want to. This is dangerous if you're not in total control of the values in @var1. Why is it dangerous? If one of the values contained "); drop table A.database1; --" then you could have someone destroying your database.

        The DBI library has a feature (placeholders) that protect you from SQL injection attacks when you're not in total control of the values. Basically it works by using question marks instead of the values in your statement, like this:

        select name,value from A where name in (?, ?, ?, ?, ?)

        Then, when you execute the statement, you provide the values for the question mark slots. The DBI library will handle all the proper quoting.

        For your case, you'd do it like this:

        # Build SQL statement with question marks instead of values my $sth3 = $dbh->prepare("select name,value from A.database1 where nam +e in (" . join(",", "?" x @var1) . ")" ) or die "Can't prepare statement: $DBI::errstr"; # Tell DBI to replace the question marks with the list of values $sth3->execute(@var1);

        Note: I've not tested this. I've done it many times in the past, but I don't currently have a database setup on this machine, so I'm doing it from memory. If you try this and there's a problem, let me know and I'll fix it up.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: DBD ERROR: error possibly near <*> indicator at char
by frieduck (Hermit) on Mar 25, 2009 at 16:20 UTC
    In addition to the answer you've already received, I'd also recommend specifying the column names in the INSERT statement. That can save you from nasty surprises down the road.
    Change:
    insert into u_configname values (?,?,?,?,?,?,?,?,?,?,?)
    to:
    insert into u_configname (ad_client_id,ad_org_id,created,...) values ( +?,?,?,?,?,?,?,?,?,?,?)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-25 19:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found