how to use ">" or "<" as variable in javafx sqlite query - sql

I have created a combobox in javafx and I want to query the sqlite db for data which are greater or less than what is selected from the combobox.
Combobox have ObservableList "10,20,30,40,50"
My query is " Select * From table Where age ( xxx ) ?"
xxx can be (" >=" or "<=")
this is my query
String qry_age = "Select * From table Where age (>=) ?";
PreparedStatement ps_age = connect.prepareStatement(qry_age);
ps_age.setInt(1,15);
ResultSet rs_age = ps_age.executeQuery();
while (rs_age.next()) {
System.out.println(rs_age.getString("age"));
}

You could simply use string concatenation to construct the query:
String ageCompareOperator = ">="; // or something else e.g. value from ComboBox
String qry_age = "Select * From table Where age " + ageCompareOperator + " ?";
PreparedStatement ps_age = connect.prepareStatement(qry_age);
...

Related

Eclipselink NamedNativeQuery pass column name as parameter and not a value

Trying to pass column name as parameter but JPA sets it as a value surrounding it with single quotes.
#NamedNativeQueries({
#NamedNativeQuery(
name = "Genre.findAllLocalized",
query = "SELECT "
+ " CASE "
+ " WHEN ? IS NULL THEN genre_default"
+ " ELSE ? "
+ " END localized_genre "
+ "FROM genre ORDER BY localized_genre")
})
Then:
List<String> res = em.createNamedQuery("Genre.findAllLocalized")
.setParameter(1, colName)
.setParameter(2, colName)
.getResultList();
The problem is that the column names being passed are taken as values so the result will return result list with repeated values of "col_name" instead of selecting the value of the column passed as parameter.
Is this achievable?
Basically it makes no sense to create a prepared query like this, how would you name that query anyway: "*"? So the short answer is: no.
But you could create named queries dynamically if this matches your requirement:
String colName = "colName";
String query = "SELECT WHEN " + colName + " IS NULL THEN genre_default";
Query query = entitymanager.createQuery(query);
Probably using a criteria builder is more the way you want to use JPA (code from https://en.wikibooks.org/wiki/Java_Persistence/Criteria):
// Select the employees and the mailing addresses that have the same address.
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery criteriaQuery = criteriaBuilder.createQuery();
Root employee = criteriaQuery.from(Employee.class);
Root address = criteriaQuery.from(MailingAddress.class);
criteriaQuery.multiselect(employee, address);
criteriaQuery.where( criteriaBuilder.equal(employee.get("address"), address.get("address"));
Query query = entityManager.createQuery(criteriaQuery);
List<Object[]> result = query.getResultList();

pass object to IN Operator without single quote

Select * from table_A WHERE name in (#nameObj)
My issue is I am passing this object from code behind as
#nameObj = "'" + "john" + "'" + "," + "'" + "joseph" + "'"
So I want my final query like this:
Select * from table_A WHERE name in ('john','joseph')
But it is giving query like this:
Select * from table_A WHERE name in ('''john'',''joseph''')
This work for me
sql = $#" AND Office IN ('{string.Join("','", new string[] { "aa", "bb"})}')";
But you should pass this as sql parameter;
in sql query you have
AND Office IN (#datain)
and in call query
_dbContext.Offices.FromSql(sql,
new NpgsqlParameter(
"#datain",
$#"'{string.Join("','", new string[] {"aa", "bb"})}')")
)
.ToList();

Passing hexadecimal hash value to JDBC query

I have a table in my MYSQL database with hexadecimal(md5 hash values), I pick the values in one Query1 iterate over the resultset RS1 and Now I need to Fetch data from another table which has this hash value in the key column..I get an sql syntax error for executing the same:
String targetQuery = "select hashValue from targettbl ";
String sourceQuery = "select st.* from sourcetbl st where seqNo in" +
"(select seqNo from sourcetblkey where hashValue in (?)" ;
try {
stmt1 = conn.createStatement();
stmt2 = conn.prepareStatement(sourceMD5Query);
rs1 = stmt1.executeQuery(targetMd5Query);
while(rs1.next())
{
stmt2.setString(1, rs1.getString(1));
rs2 = stmt2.executeQuery(sourceQuery);
ResultSetMetaData rsmd = rs2.getMetaData();
int columnsNumber = rsmd.getColumnCount();
while(rs2.next())
{
System.out.println("Source Row");
for(int i=1;i<columnsNumber;i++)
{
System.out.println(""+rs2.getString(i));
}
}
}
Error:
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
Pls help
There is a )missing at end of your statement. Change it to this:
String sourceQuery = "select st.* from sourcetbl st where seqNo in" +
"(select seqNo from sourcetblkey where hashValue in (?))" ;

how to perform full text search using sqlite fts3

I have compiled the fts3 module for sqlite3.6.2. How to build the virtual table and perform search on some field of existent tables (like content:string in tweets)? Does ft3 support fuzzy search? (I checked the documentation but still remained horribly confused...).
You can do something as simple as this to search on 2 fields. You have to use unions.
string createSql = "CREATE VIRTUAL TABLE TweetFts USING FTS3(TweetId, Title, Description)";
string insertSql = "INSERT INTO TweetFts (TweetId, Title, Description)
SELECT TweetId, Title, Description FROM Tweet";
string sql = #"select TweetId from TweetFts where Title match '" + allWords + "'";
sql += " union ";
sql += #"select TweetId from TweetFts where Description match '" + allWords + "'";
sql += " union ";
sql += #"select TweetId from TweetFts where Title match '""" + exactMatch + #"""'";
sql += " union ";
sql += #"select TweetId from TweetFts where Description match '""" + exactMatch + #"""'";
Run this query and you have a list of Tweet that match.
I don't see anything fuzzy other than the prefix search using *.
There is a soundex function.

SQL Server Dynamic SQL

I am executing Dynamic SQL,
sqlQuery = " SELECT ";
sqlQuery += _Allowed + " , ";
sqlQuery += " + cast( ";
sqlQuery += " _ID as nvarchar ) ";
sqlQuery += " FROM ";
sqlQuery += " TBL_SUCCESS ";
when i execute it suppose to return common separated values like 2,4,5 in single column
instead it return values in separate column
my MyDataTable suppose to populate
Column1
2,4,5
but it populates
column1 column2 column3
2 4 5
How to get the output?
Need to see the value of _Allowed to know what else is happening, but you need to at least put quotes around the comma and concatenate it inside the SQL statement, like this:
sqlQuery += _Allowed + " + ' , ' ";