JDBC connection for HANA with input parameters - jdbc

I am trying to extract some tables with using RJDBC connection. However, in the tables, there are some must input variables that I need to give. I need to find a way that extract these input parameters. As I understand, then I can extract the tables I want. However, I don't know how to find and extract them.
qry <- 'select * from "__tablename__"(
PLACEHOLDER."$$variable1$$" => ?,
PLACEHOLDER."$$variable2$$" => ?,
PLACEHOLDER."$$variable3$$" => ?,
PLACEHOLDER."$$variable4$$" => ?,
PLACEHOLDER."$$variable5$$" => ?)'
data <- dbGetQuery(conn, qry, variable1, variable2, variable3, variable4, variable5)

Related

Bulk Insert in Laravel 5.3

$list = [];
foreach($RoleDetails["Data"]["Permissions"] as $Permission) {
$MyModel = new UserRolePermissionModel();
$MyModel->UserID = $User->UserID;
$MyModel->RolePermissionID = $Permission->RolePermissionID;
$MyModel->IsActive = $Permission->IsActive;
array_push($list, $MyModel);
}
\DB::table('tbluserrolepermission')->insert($list);
Below are the error details
QueryException {#293 ▼
#sql: "insert into `tbluserrolepermission` (`0`, `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `10`, `11`, `12`, `13`, `14`, `15`, `16`, `17`) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
#bindings: array:18 [▶]
#message: "SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: insert into `tbluserrolepermission` (`0`, `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `10`, `11`, `12`, `13`, `14`, `15`, `16`, `17`) values ({"UserID":21,"RolePermissionID":19,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":20,"IsActive":0,"IsProtectionAvailable":0,"IsProtectionReadOnly":1}, {"UserID":21,"RolePermissionID":21,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":22,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":23,"IsActive":0,"IsProtectionAvailable":0,"IsProtectionReadOnly":1}, {"UserID":21,"RolePermissionID":24,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":25,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":26,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":27,"IsActive":0,"IsProtectionAvailable":0,"IsProtectionReadOnly":1}, {"UserID":21,"RolePermissionID":28,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":29,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":30,"IsActive":0,"IsProtectionAvailable":0,"IsProtectionReadOnly":1}, {"UserID":21,"RolePermissionID":31,"IsActive":0,"IsProtectionAvailable":0,"IsProtectionReadOnly":1}, {"UserID":21,"RolePermissionID":32,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":33,"IsActive":0,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":34,"IsActive":1,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}, {"UserID":21,"RolePermissionID":35,"IsActive":1,"IsProtectionAvailable":0,"IsProtectionReadOnly":1}, {"UserID":21,"RolePermissionID":36,"IsActive":1,"IsProtectionAvailable":1,"IsProtectionReadOnly":0}))"
#code: "42S22"
#file: "C:\xampp\htdocs\AS4\vendor\laravel\framework\src\Illuminate\Database\Connection.php"
#line: 761
-previous: PDOException {#356 ▶}
+errorInfo: array:3 [▶]
+"previous": PDOException {#356 ▶}
-trace: {▶}
}
It is not working, because you are not providing an array of arrays with only the values matching the column names to populate the database with.
Example
Lets say you want to populate an array of users and save them to the database. The following example would do that.
DB::table('users')->insert([
['email' => 'taylor#example.com', 'votes' => 0],
['email' => 'dayle#example.com', 'votes' => 0]
]);
Note that email and votes in the above example are 2 columns in the users table. Note also that only an array of other arrays are passed to the insert method. What you are trying to insert is actually an array of eloquent model objects.
Source: https://laravel.com/docs/5.3/queries#inserts
I fixed it like below.
foreach($RoleDetails["Data"]["RolePermissions"] as $RolePermission) {
$data = [
'UserID' => $User->UserID,
'RolePermissionID' => $RolePermission->RolePermissionID,
'IsActive' => $RolePermission->IsActive,
'IsProtectionAvailable' => $RolePermission->IsProtectionAvailable,
'IsProtectionReadOnly' => $RolePermission->IsProtectionReadOnly
];
array_push($list,$data);
}
\DB::table('tbluserrolepermission')->insert($list);
or it could be like this.
UserRolePermissionModel::insert($list);
Can you try this?
UserRolePermissionModel::insert($list->toArray());
OR
DB::table('table')->insert($list->toArray());

Insert timestamp with JdbcTemplate in Oracle database ( ORA-01858 )

I've read a lot of stuff about this error, and still not found the mistake.
I'm using JdbcTemplate to insert a row in some table with some timestamp column
I'm pretty sure the timestamp is the problem, as if delete from the insert it works fine)
My code:
private static final String INSERT_CITAS = "INSERT INTO CITAS ("
+ "idCita, idServicio, " + "fechaCita, "
+ "idEstado, idUsuarioInicial) " + "VALUES (?, ?, ?, ?, ?)";
Object[] params = {
idCita,
citaQuenda.getIdServicio(),
getDateToDBFormat(citaQuenda.getFechaCita()),
ESTADO_INICIAL,
USUARIO_INICIAL };
String queryCitas = INSERT_CITAS;
super.getJdbcTemplate().update(queryCitas, params);
protected String getDateToDBFormat(Date fechaCreacion){
return "TO_TIMESTAMP('" +
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(fechaCreacion)
+ "', 'yyyy-mm-dd hh24:mi:ss')" ;
}
And having the next error:
org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL [INSERT INTO citas_55 (idCita, idServicio, fechaCita, idEstado, idUsuarioInicial) VALUES (?, ?, ?, ?, ?)];
ORA-01858: a non-numeric character was found where a numeric was expected
I've tried to execute the sql in some SQL editor having success, so I can't be more confused.
Being my params: [461, 100, TO_TIMESTAMP('2015-01-28 00:00:01', 'yyyy-mm-dd hh24:mi:ss'), 1, 8888] This actually works.
INSERT INTO citas (idCita, idServicio, fechaCita, idEstado, idUsuarioInicial) VALUES (457, 100, TO_TIMESTAMP('2015-01-28 00:00:01', 'yyyy-mm-dd hh24:mi:ss') , 1, 8888);
Any kind of help would be appreciated. Thanks in advance!
Don't convert back and forth between dates/timestamps and Strings.
Just pass a java.sql.Timestamp instance as a parameter:
Object[] params = {
idCita,
citaQuenda.getIdServicio(),
new java.sql.Timestamp(citaQuenda.getFechaCita()),
ESTADO_INICIAL,
USUARIO_INICIAL };
String queryCitas = INSERT_CITAS;
super.getJdbcTemplate().update(queryCitas, params);
I will go out on a limb here, and think I may see the problem. getDateToDBFormat() method is returning a String type, which contains the text, "TO_TIMESTAMP(...)". That is not a date or timestamp parameter. It is a string parameter. You need to do this instead:
Remove the TO_TIMESTAMP stuff from getDateToDBFormat() and have it just return the formatted DATE/TIME value (the format you show is not an oracle timestamp, but a DATE type).
change your insert to:
"INSERT INTO CITAS ... VALUES (?, ?, TO_DATE(?,?) , ?, ?)"
Where the parameters to the TO_DATE call are the return from getDateToDBFormat() and the second parameter is the date format mask. However, can't you just get rid of that mess and bind a Java Date type (or jdbc sql equivalent) directly?
That should work.

Laravel Eager Loading, filtering and limit column fields does not work

I want to filter my selection of columns in my channels hasmany relation as well filtering it using where method but to no avail. Any idea?
$data['users'] = Users::with(array('Channels' => function($q){
$q->where('is_default','=', 1)->select(array('channel_name', 'channel_id'));
}))->get(array('id'));
But this works perfectly fine.
$data['users'] = Users::with(array('Channels' => function($q){
$q->where('is_default','=', 1)
}))->get(array('id'));
I tried putting the select method first before the where method still does not work
EDIT:
These are SQL queries executed which is really what I wanted however it does not fill up the relations property.
select created_at, id, name, referrer from users where status = ? limit 10 offset 0
select channel_name, channel_id from channels where channels.user_id in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) and is_default = ?
You may try this
$data['users'] = Users::with(array('Channels' => function($q){
$q->select('id, channel_name', 'user_id')->where('is_default','=', 1);
}))->get();
If you want to select columns in get() then make sure you select the associated key for channels table, for example, assuming that user_id is the foreign key in chennels table so you can do it using this:
$data['users'] = Users::with(array('Channels' => function($q){
$q->select('id, channel_name', 'user_id')->where('is_default','=', 1);
}))->get(array('id'));
If you don't select the primary key in the main query and foreign key in the sub query then with will not work. In both querys/select the related keys should be available.

Fastest way to insert elements from Ruby hash to SQLite 3

I have a Ruby hash with variables:
a two-element array of strings
an integer
I have tried the two following ways to insert the elements from the hash to a SQLite 3 DB:
myRubyHash.each do |k, v|
x=[k[0],k[1],v]
db.execute "INSERT INTO MyTable VALUES ( ?, ?, ? )", x
end
And,
myRubyHash.each do |x|
db.execute "INSERT INTO MyTable VALUES ( ?, ?, ? )", x
end
The first being considerably faster (but still quite slow). Is there a faster way to go about this?
If it helps, my SQLite 3 table was created by:
rows = db.execute <<-SQL
CREATE TABLE Assoc_words_p (
name1 varchar(30),
name2 varchar(30),
val int,
PRIMARY KEY (name1,name2)
);
SQL
Thanks
I found the prepare statement can be used as follows:
stmnt1 = db.prepare( "INSERT INTO Table(name1,name2,val) VALUES (?, ?, ?)" )
myRubyHash.each do |k, v|
stmnt1.execute(k[0],k[1],v/2)
end
Sadly, Hash#each is just not a fast method, and I expect that's what's causing the performance concerns. The first thing that comes to mind in terms of performance is wondering why there's a hash there at all - it seems like the faster way to go would be to put your data into the database directly and skip the hash altogether.

VB6, RDO AND QUERYSTRING

I'm creating a query string as follows ...
gSql = "{ CALL MYSP( ?, ?, ?, ?, ?, ?, {resultset 10, RECORD_OUTPUT} ) }"
the {resultset 1000, RECORD_OUTPUT} is copied it from Microsoft's MSDN
The error displayed after I compile is:
identifier \'RECORD_OUTPUT\' must be declared
What should the {resultset 1000, RECORD_OUTPUT} actually be ?
At this position, The Stored Procedure will return refcursor back to VB6.

Resources