Perl Executing DBI execute in Loop - sql-server

I have a perl script which prepares a statement with params 1,2,3,4 using DBI interface .The prepare statement executes a stored procedure with 4 params.Then it runs a foreach loop and does execute
If the parameters are not right the stored procedure throws error and execute fails. I am interested in printing a warning and proceeding to next set of params.
After the first execute failure for an "invalid parameter", I get "Attempt to initiate a new Adaptive Server operation with results pending" as errmsg in SQL execute for subsequent loop iteration.
foreach my $file (#filelist)
{
#.. get param1 , param2 , param3, param4 from $file
unless ( dbh->execute($param1,$param2,$param3,$param4) )
{
#print some warning
next;
}
}
How do I continue processing with this error?
These are the version I am using
>perl -MDBI -e 'DBI-> installed_versions;'
Perl : 5.010001 (x86_64-linux-thread-multi)
OS : linux (2.6.18-348.12.1.el5)
DBI : 1.609
DBD::Sybase : 1.15
DBD::Sponge : 12.010002
DBD::SQLite : 1.27
DBD::Proxy : install_driver(Proxy) failed: Can't locate RPC/PlClient.pm in #INC
DBD::Informix : 2013.0521
DBD::Gofer : 0.011565
DBD::File : 0.37
DBD::ExampleP : 12.010007
DBD::DBM : 0.03
I added { RaiseError => 0, PrintWarn => 1, PrintError => 1 } in the connect as suggested by mekazu, (The code is exact same as yours mekazu ) but it is not helping still getting the same error.
In foreach I get success,
iteration 1 no error
iteration 2 gives a stored procedure error
" Database execute failed. ERROR MESSAGE: Server message number=52001
severity=16 state=1 line=124 server=dev procedure=sp1 text=Data
Error:Invalid parameter: P1
iteration 3
Database execute failed. ERROR MESSAGE: OpenClient message: LAYER =
(0) ORIGIN = (0) SEVERITY = (78) NUMBER = (51) Server dev, database
dev Message String: Attempt to initiate a new Adaptive Server
operation with results pending
iteration 4
Database execute failed. ERROR MESSAGE: OpenClient message: LAYER =
(0) ORIGIN = (0) SEVERITY = (78) NUMBER = (51) Server dev, database
dev Message String: Attempt to initiate a new Adaptive Server
operation with results pending

Ensure you disable RaiseError (default: false) when creating the handle. You can use PrintWarn (default: false) and PrintError (default: true) to save you doing it yourself:
$dbh = DBI->connect($dsn, $user, $password,
{ RaiseError => 0, PrintWarn => 1, PrintError => 1 });
my $sth = $dbh->prepare($query);
foreach my $file (#filelist)
{
#.. get param1 , param2 , param3, param4 from $file
unless ( $sth->execute($param1,$param2,$param3,$param4) )
{
#warning already printed
next;
}
}

Related

The error about “dropDatabase fail: chunk is not in COMPLETE state”

I run the following code in python:
exchange_path = f"dfs://market/{exchange.value}"
script = f'''if(existsDatabase(\"{exchange_path}\")) {{ dropDatabase(\"{exchange_path}\") }}'''
session.run(script)
There is an error:
RuntimeError: <Server Exception> in run: dropDatabase("dfs://market/BINANCE") => deleteSubChunks failed on '/market/BINANCE', chunk 17a19f25-cc16-15ae-714b-5ee30d5d6795 is not in COMPLETE state
Firstly drop the partitions by dropPartition and then use dropDatabase to delete the database.
dbName=exchange_path
fileCond=dbName + "%"
t=exec substr(file,strlen(dbName)) from rpc(getControllerAlias(),getClusterChunksStatus) where file like fileCond, state != "COMPLETE"
dropPartition(database("dfs:/"+dbName),t,,true)

Slick can't connect to mssql database

I would like to use Slick (3.2.3) to connect to a MSSQL database.
Currently, my project is the following.
In application.conf, I have
somedbname = {
driver = "slick.jdbc.SQLServerProfile$"
db {
host = "somehost"
port = "someport"
databaseName = "Recupel.Datawarehouse"
url = "jdbc:sqlserver://"${somedbname.db.host}":"${somedbname.db.port}";databaseName="${somedbname.db.databaseName}";"
user = "someuser"
password = "somepassword"
}
}
The "somehost" looks like XX.X.XX.XX where X's are numbers.
My build.sbt contains
name := "test-slick"
version := "0.1"
scalaVersion in ThisBuild := "2.12.7"
libraryDependencies ++= Seq(
"com.typesafe.slick" %% "slick" % "3.2.3",
"com.typesafe.slick" %% "slick-hikaricp" % "3.2.3",
"org.slf4j" % "slf4j-nop" % "1.6.4",
"com.microsoft.sqlserver" % "mssql-jdbc" % "7.0.0.jre10"
)
The file with the "main" object contains
import slick.basic.DatabaseConfig
import slick.jdbc.JdbcProfile
import slick.jdbc.SQLServerProfile.api._
import scala.concurrent.Await
import scala.concurrent.duration._
val dbConfig: DatabaseConfig[JdbcProfile] = DatabaseConfig.forConfig("somedbname")
val db: JdbcProfile#Backend#Database = dbConfig.db
def main(args: Array[String]): Unit = {
try {
val future = db.run(sql"SELECT * FROM somettable".as[(Int, String, String, String, String,
String, String, String, String, String, String, String)])
println(Await.result(future, 10.seconds))
} finally {
db.close()
}
}
}
This, according to all the documentation that I could find, should be enough to connect to the database. However, when I run this, I get
[error] (run-main-0) java.sql.SQLTransientConnectionException: somedbname.db - Connection is not available, request timed out after 1004ms.
[error] java.sql.SQLTransientConnectionException: somedbname.db - Connection is not available, request timed out after 1004ms.
[error] at com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:548)
[error] at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:186)
[error] at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:145)
[error] at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:83)
[error] at slick.jdbc.hikaricp.HikariCPJdbcDataSource.createConnection(HikariCPJdbcDataSource.scala:14)
[error] at slick.jdbc.JdbcBackend$BaseSession.<init>(JdbcBackend.scala:453)
[error] at slick.jdbc.JdbcBackend$DatabaseDef.createSession(JdbcBackend.scala:46)
[error] at slick.jdbc.JdbcBackend$DatabaseDef.createSession(JdbcBackend.scala:37)
[error] at slick.basic.BasicBackend$DatabaseDef.acquireSession(BasicBackend.scala:249)
[error] at slick.basic.BasicBackend$DatabaseDef.acquireSession$(BasicBackend.scala:248)
[error] at slick.jdbc.JdbcBackend$DatabaseDef.acquireSession(JdbcBackend.scala:37)
[error] at slick.basic.BasicBackend$DatabaseDef$$anon$2.run(BasicBackend.scala:274)
[error] at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135)
[error] at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
[error] at java.base/java.lang.Thread.run(Thread.java:844)
[error] Nonzero exit code: 1
Perhaps related and also annoying, when I run this code for the second (and subsequent) times, I get the following error instead:
Failed to get driver instance for jdbcUrl=jdbc:sqlserver://[...]
which forces me to kill and reload sbt each time.
What am I doing wrong? Worth noting: I can connect to the database with the same credential from a software like valentina.
As suggested by #MarkRotteveel, and following this link, I found a solution.
First, I explicitly set the driver, adding the line
driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
in the db dictionary, after password = "somepassword".
Secondly, the default timeout (after one second) appears to be too short for my purposes, and therefore I added the line
connectionTimeout = "30 seconds"
after the previous driver line, still in the db dictionary.
Now it works.

SQLSTATE[22021]: Character not in repertoire

I'm having this problem while seeding my PGSQL Database:
Illuminate\Database\QueryException : SQLSTATE[22021]: Character not in repertoire: 7 ERROR: secuencia de bytes no válida para codificación «UTF8»: 0xe3 0x83 0xe2 (SQL: insert into "races" ("name", "public_name", "members", "updated_at", "created_at") values (harp��a, Harpía, 23, 2018-04-21 20:30:20, 2018-04-21 20:30:20) returning "id")
at C:\Sandbox\rpgforum\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error
661| // message to include the bindings with SQL, which will make this exception a
662| // lot more helpful to the developer instead of just the database's errors.
663| catch (Exception $e) {
> 664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e
666| );
667| }
668|
Exception trace:
1 PDOException::("SQLSTATE[22021]: Character not in repertoire: 7 ERROR: secuencia de bytes no válida para codificación «UTF8»: 0xe3 0x83 0xe2")
C:\Sandbox\rpgforum\vendor\laravel\framework\src\Illuminate\Database\Connection.php:330
2 PDOStatement::execute()
C:\Sandbox\rpgforum\vendor\laravel\framework\src\Illuminate\Database\Connection.php:330
Please use the argument -v to see more details.
And this is my Seeder's code:
<?php
use Illuminate\Database\Seeder;
use App\Models\Race;
class RaceTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
$races = [
'Humano',
'Orco',
'Elfo',
'Harpía',
'Enano',
];
for ($i = 0; $i < count($races); $i++) {
$race_i = utf8_encode($races[$i]);
$race = Race::create([
'name' => strtolower($race_i),
'public_name' => $race_i,
'members' => mt_rand(10,30),
]);
} // for
}
}
My database configuration:
pgsql database
My config.database file config:
config.database file
Is there any solution for this problem? I can find the way to get rid of this problem...
EDIT:
I just noticed something weird. I got two records in my database:
database records
And both of them have symbols that gave me problem with the seeder. Maybe the problem is within the seeders logic? I'm doing the exact same thing I do the whole time with my Laravel/Postgresql projects so i can't find the reason why i can't seed words with accent mark but i can use a simple form to store that kind of information. Any ideas ?

DBLib error driving me crazy

I am working on a perl script that has multiple database calls and I continuously get the following error:
DB-Library error:
Attempt to initiate a new SQL Server operation with results pending.
In my code I have the following:
The first call to the database performs a number of insert statements built from a hash:
while (my ($key, $value) = each(%holidays)) {
system("log4k", "DEBUG", "$0", "Staging holiday info data for: $cal_name: $key");
$sql = "INSERT INTO stg_holiday_data (hol_mnemonic, hol_date, hol_comment, dml_type) VALUES (\"$cal_name\", $key, \"$value\", \"N\")";
system("log4k", "TRACE", "$0", "SQL being executed: $sql");
if ($test == 0) {
$dbh->dbcmd($sql);
($dbh->dbsqlexec() && $dbh->dbresults)
or &fatalError("Database error in $sql", "DB_ERROR");
while($dbh->dbresults != NO_MORE_RESULTS) {
while(my #dat = $dbh->dbnextrow){}
}
}
}
immediately after that finishes I close off the connection, and cancel it to make sure that there are no results left to be processed by issuing:
$dbh->dbcancel();
$dbh->dbclose();
From there I call a separate subroutine to execute a stored procedure which will produce three lines of output signifying row numbers:
subroutine call
&run_sproc() if ($test == 0);
subroutine:
sub run_sproc() {
system("log4k", "DEBUG", "$0", "Loading staged holiday data");
my $sql1 = "upd_holiday_data";
system("log4k", "TRACE", "$0", "SQL being executed: $sql1");
my($dbh2) = new Sybase::DBlib $ENV{DATABASE_USER}, $ENV{DATABASE_PASSWORD}, $ENV{DATABASE_SERVER}, "GME_calendar_sync";
&fatalError("Failed to login imagine database", "DB_LOGIN_ERROR") unless ($dbh2);
$dbh2->dbcmd($sql1);
($dbh2->dbsqlexec() && $dbh2->dbresults )
or &fatalError ("Database error in $sql", "DB_ERROR");
while ($dbh2->dbresults != NO_MORE_RESULTS) {
while (my #d = $dbh2->dbnextrow) {
system("log4k", "TRACE", "$0", "Next row being inserted #d");
}
}
$dbh2->dbclose();
}
I do have a third SQL block that comes after the stored procedure that works fine with or without this subroutine.
What is happening is I am receiving the error mentioned above right before the results from the stored procedure print. I have tried everything I can imagine to make sure that all the results are being processed. A sample of the log output is below:
[Tuesday, 23 October 2012 13:30:02 BST] [DEBUG] gme_process_calendars.pl: Staging holiday info data for: CA: 20251226
[Tuesday, 23 October 2012 13:30:03 BST] [TRACE] gme_process_calendars.pl: SQL being executed: INSERT INTO stg_holiday_data (hol_mnemonic, hol_date, hol_comment, dml_type) VALUES ("CA", 20251226, "upload", "N")
[Tuesday, 23 October 2012 13:30:03 BST] [DEBUG] gme_process_calendars.pl: Staging holiday info data for: CA: 20220103
[Tuesday, 23 October 2012 13:30:03 BST] [TRACE] gme_process_calendars.pl: SQL being executed: INSERT INTO stg_holiday_data (hol_mnemonic, hol_date, hol_comment, dml_type) VALUES ("CA", 20220103, "upload", "N")
[Tuesday, 23 October 2012 13:30:03 BST] [DEBUG] gme_process_calendars.pl: Loading staged holiday data
[Tuesday, 23 October 2012 13:30:03 BST] [TRACE] gme_process_calendars.pl: SQL being executed: upd_holiday_data
DB-Library error:
Attempt to initiate a new SQL Server operation with results pending.
[Tuesday, 23 October 2012 13:30:03 BST] [TRACE] gme_process_calendars.pl: Next row being inserted 310107230
Any help with this would be greatly appreciated as I have already tried everything I can find doing an internet search and reading the documentation.
Thanks
I think your problem is that your run_proc method tries to execute this SQL:
my $sql1 = "upd_holiday_data";
which I doubt is a valid SQL command
Solution Found:
In the run_sproc sub routine, the check:
($dbh2->dbsqlexec() && $dbh2->dbresults )
or &fatalError ("Database error in $sql", "DB_ERROR");
the second condition $dbh2->dbresults is what is causing this error condition, once this was removed the error did not come up anymore.

Not getting err code,type and state with Perl DBI (DBD::ODBC) on Sqlserver

I have a perl script which does the below to just run a insert stmnt and doing a deliberate RAISERROR. Now i can see that the error is generated but has the wrong error code - SQL-42000 , but the error diagnostic methods don't return anything. Using $DBI::err,$DBI::errstr,$DBI::state also doesnt print the correct thing. Only $DBI::errstr is coming up correctly.
$dbh->{RaiseError} = 1;
$dbh->{PrintError} = 1;
$dbh->begin_work;
eval {
$dbh->do(INSERT INTO ..);
$dbh->do (RAISERROR ('User defined error',16,1) --purposely raising error
$dbh->commit;
1;
};
if ($#) {
print $#;
print "err() ==>".$dbh->err();
print "errstr() ==>".$dbh->errstr();
print "state() ==>".$dbh->state();
$dbh->rollback or warn "rollback failed";
}
Output:
DBD::ODBC::db do failed: [unixODBC][FreeTDS][SQL Server]User defined error (SQL-42000) ..
err() =>
errstr() =>
state() =>
Your code sample is broken (the --purposely and missing ;, missing quotes in do call) and the error 42000 is because you have an error calling raiserror which is "Incorrect syntax near ...". I can't tell because you do show real working code. Anyway your code rewritten as the following works for me:
use DBI;
use strict;
use warnings;
my $dbh = DBI->connect('dbi:ODBC:xxx','xx','xx');
$dbh->{RaiseError} = 1;
$dbh->{PrintError} = 1;
$dbh->begin_work;
eval {
#$dbh->do(INSERT INTO ..);
$dbh->do (q/RAISERROR ('User defined error',16,1)/); #--purposely raising error
$dbh->commit;
1;
};
if ($#) {
print $#;
print "err() ==>".$dbh->err();
print "errstr() ==>".$dbh->errstr();
print "state() ==>".$dbh->state();
$dbh->rollback or warn "rollback failed";
}
and outputs:
DBD::ODBC::db do failed: [unixODBC][Easysoft][SQL Server Driver][SQL Server]User defined error (SQL-42000) at so1.pl line 16.
DBD::ODBC::db do failed: [unixODBC][Easysoft][SQL Server Driver][SQL Server]User defined error (SQL-42000) at so1.pl line 16.
err() ==>1errstr() ==>[unixODBC][Easysoft][SQL Server Driver][SQL Server]User defined error (SQL-42000)state() ==>42000

Resources