Worklight SQL Adapter - can't pass a param to IN clause - ibm-mobilefirst

i want to pass a list of values as a string to an IN clause in my Worklight 6.1 SQL Adapter:
SQL_Query= select * from USERS where name in (?);
i pass the following string as a param:
'john','mike','joe'
The call to the SQL procedure failed with the following error:
java.sql.SQLException: Invalid column index
So my question is: how to deal with IN params in a SQL Adapter?

The point was to use a length-variable parameter, as i don't know how many people to retrieve (the number depends on context).
Actually, it's not possible to do such with a PreparedStatement (JDBC). It can only be used with a Statement. Unfortunately, Worklight API does not propose statement but only preparedstatement.
The following is working, but i have to limit my search to 3 people:
SQL_Query= select * from USERS where name in (?,?,?);
or (dirty):
SQL_Query= select * from USERS where name = ? or name = ? or name = ?;

Related

Custom JPA repository query with spring boot

I am trying to execute a custom query through jpa repository interface like this:
#Query(
value = "SELECT (TRIM(TYPE_NAME) ||'.'|| TRIM(NAME)) AS NAMES FROM ?1",
nativeQuery = true)
public List<String> getNamesFromView( String viewName);
I want to pass the name of the table to fetch from, dynamically by the user.
I am getting runtime exception
nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement
If I hardcode the table name, this is working fine
Table names can not be parametarized, that's the reason why your getting error.
The bind variables exist primarily for performance reasons, parametarized query will be compiled only once by DB and for later subsequent executions same compiled version is used.
The value for the placeholder would be a string.
So SELECT (TRIM(TYPE_NAME) ||'.'|| TRIM(NAME)) AS NAMES FROM ?1 with xyz as the table name would actually translates to
SELECT (TRIM(TYPE_NAME) ||'.'|| TRIM(NAME)) AS NAMES FROM 'xyz', which is invalid SQL
As you mentioned you have entityManager's instance, you can execute the query like below:
entityManager.query(String.format("SELECT (TRIM(TYPE_NAME) ||'.'|| TRIM(NAME)) AS NAMES FROM %s", viewName));

Create SQLite view as a union with source table name field [duplicate]

I want to write a query that examines all the tables in an SQLite database for a piece of information in order to simplify my post-incident diagnostics (performance doesn't matter).
I was hoping to write a query that uses the sqlite_master table to get a list of tables and then query them, all in one query:
SELECT Name
FROM sqlite_master
WHERE Type = 'table' AND (
SELECT count(*)
FROM Name
WHERE conditions
) > 0;
However when attempting to execute this style of query, I receive an error no such table: Name. Is there an alternate syntax that allows this, or is it simply not supported?
SQLite is designed as an embedded database, i.e., to be used together with a 'real' programming language.
To be able to use such dynamic constructs, you must go outside of SQLite itself:
cursor.execute("SELECT name FROM sqlite_master")
rows = cursor.fetchall()
for row in rows:
sql = "SELECT ... FROM {} WHERE ...".format(row[0])
cursor.execute(sql)

Is it possible to use a returned column value as a table name in an SQLite query?

I want to write a query that examines all the tables in an SQLite database for a piece of information in order to simplify my post-incident diagnostics (performance doesn't matter).
I was hoping to write a query that uses the sqlite_master table to get a list of tables and then query them, all in one query:
SELECT Name
FROM sqlite_master
WHERE Type = 'table' AND (
SELECT count(*)
FROM Name
WHERE conditions
) > 0;
However when attempting to execute this style of query, I receive an error no such table: Name. Is there an alternate syntax that allows this, or is it simply not supported?
SQLite is designed as an embedded database, i.e., to be used together with a 'real' programming language.
To be able to use such dynamic constructs, you must go outside of SQLite itself:
cursor.execute("SELECT name FROM sqlite_master")
rows = cursor.fetchall()
for row in rows:
sql = "SELECT ... FROM {} WHERE ...".format(row[0])
cursor.execute(sql)

Execute raw SQL using ServiceStack.OrmLite

I am working ServiceStack.OrmLite using MS SQL Server. I would like to execute raw SQL against database but original documentation contains description of how to do it with SELECT statement only. That is not enough for me.
I cant find the way to run anything as simple as that:
UPDATE table1
SET column1 = 'value1'
WHERE column2 = value2
Using, for example:
var two = db.Update(#"UPDATE table1
SET column1 = 'value1'
WHERE column2 = value2");
Running this expressions with db.Update() or db.Update<> produces uncomprehensive errors like
Incorrect syntax near the keyword 'UPDATE'.
I would like to use raw sql because my real UPDATE expression uses JOIN.
db.Update is for updating a model or partial model as shown in OrmLite's Documentation on Update. You can choose to use the loose-typed API to build your update statement, e.g:
db.Update(table: "table1",
set: "column1 = {0}".Params("value1"),
where: "column2 = {0}".Params("value2"));
The Params extension method escapes your values for you.
Otherwise the way to execute any arbitrary raw sql is to use db.ExecuteSql().
If it is a SELECT statement and you want to execute using raw sql you can use:
List<Person> results = db.SqlList<Person>("SELECT * FROM Person WHERE Age < #age", new { age=50});
Reference: https://github.com/ServiceStack/ServiceStack.OrmLite#typed-sqlexpressions-with-custom-sql-apis

Querying a custom postgresql parameter with SELECT statement

In another question, it was asked how to query a postgresql runtime parameter (e.g. SHOW search_path;) using a SELECT query. In the answer, it was suggested to use
SELECT * FROM pg_settings WHERE name = 'search_path';
This works great, but how can this be done for custom parameters defined in an extension? (See docs about Customized Options).
Example:
SET abc.my_var = 1;
SHOW abc.my_var;
outputs
1
but
SELECT * FROM pg_settings WHERE name = 'abc.my_var';
returns no rows. Is there some other table/view I can query for my custom parameter using a SELECT statement?
Use the function current_setting()
SELECT current_setting('abc.my_var');
http://www.postgresql.org/docs/current/static/functions-admin.html#FUNCTIONS-ADMIN-SET