how to perform full text search using sqlite fts3 - sql

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.

Related

How to create a dynamic query using collection-valued named parameters?

As the title suggests, i'm currently trying to add parts to the JPQL-query using collection-valued named parameters (:queryLst).
Function call:
List<PanelSet> psetLst = setRepository.getMaxZchnrGroupByLeftEight(p.getCustomerNumber(), p.getDrawingNumber(), queryLst);
queryLst:
// Is used to store values from scanned and convert them into parts of a query
ArrayList<String> queryLst = new ArrayList<>();
for (int i = 0; i < size1; i++) {
scanEdvRev = scanned.get(i).toString();
queryLst.set(i, "and left(a.drawingnumber, 8) != left('" + scanEdvRev + "', 8)");
}
SetRepository:
public interface SetRepository extends CrudRepository<PanelSet, Integer> {
#Query("select distinct max(a.drawingNumber) from PanelSet a "
+ "where a.customerNumber = :customerNumber "
+ "and a.drawingNumber != :drawingNumber (:queryLst) "
+ "group by left(a.drawingNumber, 8)")
List<PanelSet> getMaxZchnrGroupByLeftEight(#Param("customerNumber") String customerNumber,
#Param("drawingNumber") String drawingNumber,
#Param("queryLst") ArrayList<String> queryLst);
}
When i run the project i get the following exception:
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ( near line 1, column 159 [select distinct max(a.drawingNumber) from com.asetronics.qis2.model.PanelSet a where a.customerNumber = :customerNumber and a.drawingNumber != :drawingNumber (:queryLst) group by left(a.drawingNumber, 8)]
I'm unsure whether my approach to this problem is the correct way of doing this and whether this exception is caused by a simple syntax error or by my usage of collection-valued named parameters.
I've followed this guide trying to solve the problem.
EDIT: I'm basically trying to add each String from ArrayList<String> queryLst to the parametrized query inside setRepository.
#Query("select distinct max(a.drawingNumber) from PanelSet a "
+ "where a.customerNumber = :customerNumber "
+ "and a.drawingNumber != :drawingNumber (:queryLst) "
+ "group by left(a.drawingNumber, 8)")
If successful, the query behind the function
List<PanelSet> getMaxZchnrGroupByLeftEight(#Param("customerNumber") String customerNumber,
#Param("drawingNumber") String drawingNumber,
#Param("queryLst") ArrayList<String> queryLst);
should look like this:
queryStr = "select distinct max(a.drawingNumber) from PanelSet a "
+ "where a.customerNumber = " + customerNumber + ""
+ "and a.drawingNumber != " + drawingNumber + "";
for (String s : queryLst) {
queryStr = queryStr + s;
}
queryStr = queryStr + " group by left(a.drawingNumber, 8)";
I hope this clarifies what i'm trying to do with queryLst.
It can't be done using your approach of passing in a list of query chunks.
The closest you'll get is by adding every possible condition to the query and provide values for all those conditions in a way that allows conditions to be ignored, typically by providing a null.
You code might look like this:
#Query("select distinct max(a.drawingNumber) from PanelSet a "
+ "where a.customerNumber = :customerNumber "
+ "and a.drawingNumber != :drawingNumber "
+ "and a.myTextColumn = coalesce(:myTextColumn, a.myTextColumn) "
+ "and a.myIntegerColumn = coalesce(:myIntegerColumn, a.myIntegerColumn) "
// etc for all possible runtime conditions
+ "group by left(a.drawingNumber, 8)")
List<PanelSet> getMaxZchnrGroupByLeftEight(
#Param("customerNumber") String customerNumber,
#Param("drawingNumber") String drawingNumber,
#Param("myTextColumn") String myTextColumn,
#Param("myIntegerColumn") Integer myIntegerColumn);
Passing null for myTextColumn or myIntegerColumn will allow that column to be any value (except null).
You'll have to find SQL that works for the type of conditions you have and the data type of the columns involved and whether nulls are allowed.
If passing nulls doesn't work, use a special value, perhaps blank for text columns and some "impossible" date like 2999-01-01 fir date columns etc and code the condition like:
and (a.myCol = :myCol or :myCol = '2999-01-01')

Do you know how to fix this UPDATE issue?

When this code runs, I get an UPDATE writing error. Does anybody know what the problem is, and how to fix it?
This is the code:
string sql2 = "UPDATE ezuser";
sql2 += " SET fname = '" + Request.Form["fname"]+ "'";
sql2 += " , lname = '" + Request.Form["lname"] + "'";
sql2 += " , fav = '" + Request.Form["fav"] + "'";
sql2 += " , pw = '" + Request.Form["pw"] + "'";
sql2 += " , order = '" + Request.Form["order"] + "'";
sql2 += " WHERE email = '" + Request.Form["email"] + "'";
MyAdoHelper.DoQuery(fileName, sql2);
Eventhough the question doesnt tell me much about the datatypes of columns, the only thing I could suspect here is the order column, which might be of integer datatype and you might be passing string to that.
Additional note: your code looks very much vulnerable to sql injections. Please take a look into that as well.
At least in SQL Server, order is a reserved keyword and needs to properly quoted if used literally as a column name. Like so:
sql2 += " , [order] = '" + Request.Form["order"] + "'";
As sabhari already mentioned, you need to learn about SQL Injection and how to properly guard against that. Research parametrized statements for the programming language you are using.

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();

Oracle SQL Select Query Not Working in VB.NET Works Fine in SQL +

When my query executes within my VB application I am receiving the following error:
ORA-00942: table or view does not exist
All table names in my query are spelt correctly, and do in fact exist.
If I dump the query from my VB.net app and then run the query manually in Oracle SQL Plus it executes just fine.
In both cases I am logged in with the exact same credentials and have selected the same database. For my connection within visual basic I am using OleDb.
From within my visual basic application I also ran a query to dump all tables which the user has access to using
select table_name from all_tables
And the table names which I am querying show up.
Any idea what would be causing this?
SELECT
RSS.STEP_STATUS_DATE,
RSS.VALUE_RECORDED
FROM
ITR,
REPORT R,
INSTRUCTION I,
INSTRUCTION_STEP INS,
REPORT_STEP RS,
REPORT_STEP_STATUS RSS
WHERE
ITR.ITR_NO = '1' AND
I.INSTRUCTION_ID = '12345' AND
INS.STEP_NO = '2' AND
R.INSTRUCTION_ID = I.INSTRUCTION_ID AND
RS.REPORT_ID = R.REPORT_ID AND
RS.INSTRUCTION_STEP_ID = INS.INSTRUCTION_STEP_ID AND
RSS.REPORT_STEP_ID = RS.REPORT_STEP_ID AND
RSS.MEASUREMENT_NAME = 'ESN'
My visual basic code is as follows:
strQuery = "SELECT RSS.STEP_STATUS_DATE, " +
" RSS.VALUE_RECORDED " +
"FROM ITR, " +
" REPORT R, " +
" INSTRUCTION I, " +
" INSTRUCTION_STEP INS, " +
" REPORT_STEP RS, " +
" REPORT_STEP_STATUS RSS " +
"WHERE ITR.ITR_NO = '%01' AND " +
" I.INSTRUCTION_ID = '%02' AND " +
" INS.STEP_NO = '%03' AND " +
" R.INSTRUCTION_ID = I.INSTRUCTION_ID AND " +
" RS.REPORT_ID = R.REPORT_ID AND " +
" RS.INSTRUCTION_STEP_ID = INS.INSTRUCTION_STEP_ID AND " +
" RSS.REPORT_STEP_ID = RS.REPORT_STEP_ID AND " +
" RSS.MEASUREMENT_NAME = '%04'"
I'm looking at this portion (and others like it):
RSS.MEASUREMENT_NAME = '%04'
I expect you meant to do this:
RSS.MEASUREMENT_NAME LIKE '%04'
While I'm here, almost no one uses the "A,B" join syntax any more. It's extremely out-dated, and can lead to errors where join conditions are applied to the wrong tables, such that the query doesn't return any results... in other words, it might even be causing the problem you have right now.

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 + " + ' , ' ";