I have a column in my database called: "CIElabOne" which is of the type numeric [] ("CIElabOne" numeric[]) and thus contains values like: {9.766934377517181,0.0011685082518947398,-0.0023119569625251746}
I cant access the values independently, when executing the following SQL query:
SELECT "fileName" FROM "clothItems" WHERE "CIElabOne[1]" = '9.766934377517181'
The result is : ERROR: column "CIElabOne[1]" does not exist
Selecting CIElabOne as a whole is not a problem but I need to evaluate each of the elements of the array. I don't know why this happens I am following the guide http://www.postgresql.org/docs/9.1/static/arrays.html but I don't seem to find the error
This is my real sql query in java:
sqlTwo = "SELECT \"fileName\" FROM \"clothItems\" WHERE \"CIElabOne[1]\" = '"
+ inputColorOneCIELAB[0]
+ "' AND \"CIElabOne[2]\" = '"
+ inputColorOneCIELAB[1]
+ "' AND \"CIElabOne[3]\" = '"
+ inputColorOneCIELAB[2]
+ "' and \"gender\" = '"
+ inputGender
+ "' AND \"shape\" <> '"
+ inputShape
+ "'";
inputColorOneCIELAB[] is an array of doubles
The array subscript operator must be outside the quoted identifier name, otherwise it's treated as part of the identifier.
"CIElabOne[1]"
means "the column named CIElabOne[1]." You want:
"CIElabOne"[1]
which means "the first array element of the column CIElabOne.
Related
I am getting this error when trying to run my Spring boot.
java.sql.SQLException: No value specified for parameter 2
My code is this:
public UserTemp findHistoryByID(Integer Patient_Number) {
String sql = "select Col1\n" +
"from (\n" +
" select Past_Diagnoses_1 as Col1\n" +
" from patienthistory\n" +
" where Patient_Number = ?\n" +
" union\n" +
" select Past_Diagnoses_2 as Col1\n" +
" from patienthistory" +
" where Patient_Number = ?" +
" ) as T;";
return jdbcTemplate.queryForObject(sql, new Object[]{Patient_Number}, (rs, rowNum) ->
new UserTemp(
rs.getString("Col1")
));
}
As in the comments, you are having 2 placeholders in the SQL query. So you have to pass patient_number 2 times.
Coming to your second question, it depends on your requirement.
If you need a single result, you need to fix it on the DB side as it's a data issue or the query used is not proper.
If more than one result is allowed, you can use jdbcTemplate.queryForList() instead of jdbcTemplate.queryForObject(). And change the return type of findHistoryByID() to List<Map<String,Object>> and all callers of this function.
Note: Here key for each Map in List is column names returned from DB.
More information on jdbcTemplate.queryForList() is in official documentation
I have a problem with a SQL statement. I have a java app with a button to add a column into a database. For every new date, I have a new column created, which is done using the following query
final String queryCreate = "alter table Currency add '"+ newColumn + "' decimal ";
When I try to populate the column with data using the following query:
final String queryAdd = "insert into Currency( '" + newColumn + "' ) values(1.95583)";
The data is added below the last row of the previous column.
like this:
https://postimg.org/image/579gjmyzj/
My question is why the insert statement does what it does in my situation, what am I doing wrong?
INSERT creates new records, if you want to modify existing records you need to use UPDATE.
For example, to modify the first record:
"UPDATE Currency SET " + newColumn + " = 1.95583 WHERE Currency_ID = 1"
use update query
assuming strCurrencyID="1";
strCurrencyName="EUR";
final String queryAddUpdate =
if exists(Select Top 1 1 from Currency where Currency_ID=" + strCurrencyID +")
Begin update Currency set " + newColumn + "=1.95583 where Currency_ID=" +strCurrencyID + "
End
Else
Begin
insert into Currency(Currency_id, Currency_name, '" + newColumn + "' ) values("+ strCurrencyID +",'" + strCurrencyName + ", 1.95583)
End"
This will update the value in in column if currency id exists if not this will insert new row.
But I think database design should be change can you explain your business requirement.
I'm accessing an SQL database and I want to retrieve all serial numbers in a column, my problem is not here, my problem is that the sql statement brings all serial numbers whether its duplicate or not.
Here is my SQL Statement;
String sqlS = "Select (SerialNumber) from dbo.Results where ErrorCode != 0 AND LineNumber = 1 AND TMDT BETWEEN '" +ourJavaDateObject + "' AND '" + tomorrow + "'";
Here is the output:
16110201009
16110201014
16110201017
16110201048
16110201048
16110201048
16110201048
16110201048
16110201048
Please notice that there are 6 of the same serial number, how can I sort that to delete the serial number.
This is the output that I would like:
16110201009
16110201014
16110201017
16110201048
To get unique values you need to use the keyword DISTINCT.
String sqlS = "SELECT DISTINCT (SerialNumber) FROM dbo.Results WHERE ErrorCode != 0 AND LineNumber = 1 AND TMDT BETWEEN '" +ourJavaDateObject + "' AND '" + tomorrow + "'";
However your code is not safe against SQL injection, because you parse arguments directly. Would you think of using PreparedStatement?
"Select distinct (SerialNumber) from dbo.Results where ErrorCode != 0 AND LineNumber = 1 AND TMDT BETWEEN '" + ourJavaDateObject + "' AND '" + tomorrow + "'"
I am trying to execute a select query from Java, the tricky part is that both the condition and the column have a String with spaces :
String query = "select rule_name "+
" from rules " +
" where rule_name = " m_rule; //rule_name = str tf //m_rule = str tf
So the query output :
select rule_name
from rules
where rule_name = str tf
The error code - ORA-00920 - I belive it is because of the spaces.
Is there any way to fix this?
The error that you are facing basically means :
ORA-00920: invalid relational operator
This is the blank that is causing the issue. Your Java code should enclose the conditional parameter in quotes:
String query = "select rule_name "+
" from rules " +
" where rule_name = '" + m_rule + "'";
Hope this will help!
So i just wrote down this SQL query and i am trying to capture the value of rest_id in query.list(). However, this is giving the value as [1] . I want just 1 without the braces. How do i do it? Please check the code below for reference:
String sql1 = "select rest_id from rest_details where rest_name = '" + nameclicked + "' and rest_location = '" +locclicked + "'" ;
SQLQuery query1 = session.createSQLQuery(sql1);
System.out.println("sql1 " + query1.list());
Use below code to get the element inside list:
System.out.println("sql1 " + query1.list().get(0));
This always returns only the first element from the list.
Replace
System.out.println("sql1 " + query1.list());
By :
for(String id : query1.list() ) System.out.println("sql1 " + id);