perl bash script don't insert sql query - bash

i want to insert a query but not work :
my $query1 = $db1->prepare("SELECT host, name, severity FROM XX");
my $query2 = $db2->prepare('UPDATE worldmap_table' . ' SET severity = ?, name = ? WHERE HOST = ?');
$query1->execute;
while (my #row = $query1->fetchrow_array) {
$query2->execute($row[2]);
print "$row[2]\n";
}
preparation query 3
my $query3 = $db1->prepare("SELECT host, name, severity
FROM XX);
preparation query 4
my $query4 = $db2->prepare('UPDATE worldmap_table' . ' SET severity = 6, name = ? WHERE HOST = ?');
$query3->execute;
this part not work
while (my #row2 = $query3->fetchrow_array) {
$query4->execute($row2[2], $row2[1], $row2[0]);
print "$row2[2], $row2[1], $row2[0] \n";
}
i have this error:
DBD::mysql::st execute failed: called with 3 bind variables when 2 are needed at worldmap2.pl line 103.
DBD::mysql::st execute failed: called with 3 bind variables when 2 are needed at worldmap2.pl line 103.

There are two placeholders in query 4:
my $query4 = $db2->prepare(
'UPDATE worldmap_table' . ' SET severity = 6, name = ? WHERE HOST = ?'
# ~ ~
);
But you're calling it with only one argument:
$query4->execute($row2[2]);
# ~~~~~~~~
The error message mentions different numbers, so I guess you simplified the code.

Related

perl update query errors [duplicate]

i want to insert a query but not work :
my $query1 = $db1->prepare("SELECT host, name, severity FROM XX");
my $query2 = $db2->prepare('UPDATE worldmap_table' . ' SET severity = ?, name = ? WHERE HOST = ?');
$query1->execute;
while (my #row = $query1->fetchrow_array) {
$query2->execute($row[2]);
print "$row[2]\n";
}
preparation query 3
my $query3 = $db1->prepare("SELECT host, name, severity
FROM XX);
preparation query 4
my $query4 = $db2->prepare('UPDATE worldmap_table' . ' SET severity = 6, name = ? WHERE HOST = ?');
$query3->execute;
this part not work
while (my #row2 = $query3->fetchrow_array) {
$query4->execute($row2[2], $row2[1], $row2[0]);
print "$row2[2], $row2[1], $row2[0] \n";
}
i have this error:
DBD::mysql::st execute failed: called with 3 bind variables when 2 are needed at worldmap2.pl line 103.
DBD::mysql::st execute failed: called with 3 bind variables when 2 are needed at worldmap2.pl line 103.
There are two placeholders in query 4:
my $query4 = $db2->prepare(
'UPDATE worldmap_table' . ' SET severity = 6, name = ? WHERE HOST = ?'
# ~ ~
);
But you're calling it with only one argument:
$query4->execute($row2[2]);
# ~~~~~~~~
The error message mentions different numbers, so I guess you simplified the code.

Execute oracle sql query in PERL script while loop

I am trying to execute an oracle sql query in while loop of a PERL script as follows --
my $dbh = DBI->connect( 'dbi:Oracle:'.$dbname,
$dbusername,
$pass,
{ PrintError => 0,
RaiseError => 1
}
) || die "Erreur lors de la connexion: $DBI::errstr";
print ("Connexion à la base de données $dbname avec $dbusername OK \n");
$requete = "select distinct to_char(date_appli,'yyyymmdd') from DATE_APPLI ".
"where frequence = 'Q' and actif = 1";
$sth_sql = $dbh->prepare($requete);
$sth_sql->execute();
#row=$sth_sql->fetchrow_array;
$datesitu=#row[0];
$sth_sql->finish;
print "La date de situation est $datesitu \n";
# SQL to get validation script and table names
$requete = "select SCRIPT_NAME, table_name from fdf_scripts";
$sth_sql = $dbh->prepare($requete);
$sth_sql->execute();
$sth_sql->bind_columns(undef, \$script_name, \$table_name);
while ($sth_sql->fetch()) {
$script_sql="$sql_path\\"."FDF_Test_scripts\\".$script_name.".sql"; #validation script path
$script_log="$log_path\\".$script_name.".log"; #log files path
$rep_file_name="$sql_path\\"."FDF_Test_scripts\\".$table_name.".sql"; #reports SQL path
$csv_file="$sql_path\\"."FDF_Test_scripts\\".$table_name.".csv"; #report CSV path
#Load data into validation tables using validation scripts
$CmdText="sqlplus -s $connect \#$script_sql $script_log";
print "Inserting data into table : $table_name \n";
#system ("$CmdText");
$col_sql = "select COLUMN_NAME from all_tab_cols where TABLE_NAME = upper(\'$table_name\')\n";
print "$col_sql\n";
$sth_sql = $dbh->prepare($col_sql);
$sth_sql->execute();
$sth_sql->bind_columns(undef, \$COLUMN_NAME);
while ($sth_sql->fetch()) {
print "$COLUMN_NAME\n";
}
if (open (my $fh, '<:encoding(UTF-8)', $script_log)){
while (my $line = <$fh>){
if ($line=~m/\bERROR\b/){
print "Error While Loading $table_name table Please Check log file for errors at: $script_log \n";
}
}
}
else {
warn "Could open file: $script_log \n"
}
}
in above code i am fetching table names from a table and then looping them to get the column names of each table which is present in that table.
The code got executed for one iteration of inner while loop but throw error for next iteration as below --
DBD::Oracle::st fetch failed: ERROR no statement executing (perhaps you need to call execute first) [for Statement "select COLUMN_N\
AME from all_tab_cols where TABLE_NAME = upper('FDF_Bond_validation_results')
I am not proficient in PERL and just started to using this and not sure how can i overcome this problem.
Please let me know if more information is required.
In the loop, the line $sth_sql = $dbh->prepare($col_sql); overwrite the content of the $sth_sql variable. The second time the loop run, the content of $sth_sql isn't the same and the statement that overwrote it has already been exhausted (trying to fetch from it again is what causes the error).
To correct this issue you should either use a different name for the second $sth_sql variable or simply declare the second $sth_sql variable with my to keep it inside the loop (see perldoc -f my).
Note: This solution has been identified by JGNI in the comment of the question. My role has only be to summarize the issue to provide a proper answer to this question.

Why is my dataveiw filter crashing

i have a dataview from a data table i am trying to filter to see if there are duplicate values for a manufacture, Type and Serial number but i am getting an error
string strFilter = "Manufacture = " + strMake + " and Type = " + strModel + " and Serial Number = " + strSn;
strfilter = "Manufacture = ford 150 and Type = Raptor and Serial Number = 9999"
dv.RowFilter = strFilter;
this it the error i am getting
An unhandled exception of type 'System.Data.SyntaxErrorException' occurred in System.Data.dll
Additional information: Syntax error: Missing operand after 'Number' operator
You need single quotes for strings.Write serial number in brackets like [Serial number] if that is the name of your column.
For one you are missing a semi colon here.
strfilter = "Manufacture = ford 150 and Type = Raptor and Serial Number = 9999" <---------
:)

I Have 2 1064 SQL Errors And Can Not Figure Them Out

This code worked good on ph 5.3. My server got upgraded to php5.6 and now the code no longer works.
Here is the two statements.
$sql = "UPDATE " . FORUM_TOUR_TABLE . "
SET page_subject = '$subject', page_text = '$message', page_access = $page_access
WHERE page_id = $id";
It says :
`SQL Error : 1064 Syntax error near 'WHERE page_id = 1' at line 3
UPDATE SET phpbb_forum_tour page_subject = ' Welcome To The Tower' , page_text = ' Update me please ' = WHERE page_access page_id = 1
Line : 172
File: admin_forum_tour.php`
That happens when I try to update a post.
When I try to add a new post using this code:
$sql = "INSERT INTO " . FORUM_TOUR_TABLE . " (page_id, page_subject, page_text, page_sort, bbcode_uid, page_access)
VALUES ($id, '$subject', '$message', $page, '$bbcode_uid', $page_access)";
This gives me this:
`SQL Error : 1064 Syntax error near ')' at line 2
INSERT INTO phpbb_forum_tour ( page_id , page_subject , page_text , page_sort , bbcode_uid , page_access ) VALUES (3, '2 Word Rule ', ' Hello World I am here ', 30 , ' f4e1be18dc ' )
Line : 198
File: admin_forum_tour.php`
I 100% apologize for asking this as I am more than sure it has been beat to death. I have been searching for a couple hours now and have all but given up. I had it working once but was told I opened my self up to injection. I am not a pro coder. I get lucky sometimes and make things work. I am sure these errors and code is basic to most but I am still learning.
Thanks for helping and understanding my "newbieness".

Conditionally modifying multiple strings with For

So I have 13 binary values, which I call b_1... b_13, and based off these values I'd like to either set something I call indic_j to a previously defined string called inf_j, or nothing at all. Is it possible to do this without using 13 "If..." statements? What I have tried is below:
inf_1 = "aaaaa"
inf_2 = "bbbbb"
... and so on defining 13 infs, where aaaaa, bbbbb etc are names of columns in a table that I want to select.
FOR j = 1 to 13
IF b_j = 1 THEN "indic_"+j = inf_j + ",";
ELSE "indic_"+j = ""
ENDIF
ENDFOR
Also, before this I haven't introduced anything called indic_1, indic_2, etc. Is this needed?
My end goal is to transfer selected columns over to Excel. I've no problems doing this with predetermined columns, but I'm not sure how to allow for selected columns only.
I've tried using 13 IF statements, but I'm getting operator/operand type mismatch errors. My code currently is
IIF(b_1 = 1, indic_1 = inf_1 + ",",indic_1 = "")
IIF(b_2 = 1, indic_1 = inf_2 + ",",indic_1 = "")
IIF(b_3 = 1, indic_1 = inf_3 + ",",indic_1 = "")
and so on for 13 times, and then
SELECTIONRANG = indic_1 + indic_2 + indic_3 + indic_4 + indic_5 + indic_6 +indic_7 + indic_8 + indic_9 + indic_10 + indic_11 + indic_12 + indic_13
SELECTIONRANGE = LEFT(SELECTIONRANG,LEN(Selectionrang)-1)
You could create te variable name as a string and use it with &
As:
ind = 13
Var = "inf_" + ind
&Var ** inf_13

Resources