Using the IN clause with SQLite - java

In a development environment with SQLite 3.7.11 and Java, despite having read the following answers:
Answer 1
Answer 2
Answer 3,
am finding the usage of the SQLite IN clause not very straight-forward.
Assume a simple table TEST with the following structure:
----------------------------
id PRODUCT TAG
(int) (text) (text)
----------------------------
1 Cinthol Soap, Bath, Cleaning
2 Vim Dishwash, Kitchen, Cleaning
3 Burger Food, Breakfast, Lunch, Dinner
The following queries are behaving this way:
----------------------------------------------------------------------------
Query Result Expected
----------------------------------------------------------------------------
SELECT PRODUCT FROM TEST WHERE TAG IN('Soap') Cinthol Cinthol
SELECT PRODUCT FROM TEST WHERE TAG IN('Dinner') <EMPTY> Burger
SELECT PRODUCT FROM TEST WHERE TAG IN('Soap', 'Bath') <EMPTY> Cinthol
SELECT PRODUCT FROM TEST WHERE TAG IN('Cleaning') <EMPTY> Cinthol, Vim
So the questions are:
Except the first query, why are the others not producing the expected results? Is there something fundamentally wrong in the understanding?
If wrong, what is the right query to get the expected results (without using the instr function)?
Furthermore, the TAG column eventually has to be bound with an array of tokens in Java, building the query dynamically. The answers listed above have pointers to that, though.
Thanks in advance!

In clause doesn't work like this.assume if you had one TAG each column you could get the results.you need to add another table to keep your tags.in this table you need pk , foreign key(id deom tests) ,and tag so that you wil have multitags for each product.this is a solution you can make different.You had better search database notmalization first.gl

Related

How to escape ?(Question Mark) in prepared statement of hive query?

I have the following hive table-
hive> desc table_group_special_character_3;
OK
column_? int
column_2 int
Time taken: 2.084 seconds, Fetched: 4 row(s)
Through code, we run hive queries to fetch the result. For the above table while executing "select column_?,column_2 from table_group_special_character_3;" I get the following exception-
"parameter #1 is unset".
I know for sure that this is happening as my column name is column_?(post removing it is working).
I would like to know how to escape ?.
When special characters are present as a part of the column name in Hive table, you can use the symbol ` around the column name to select the column.
For your question, you can run a query like the below in Hive,
select `column_?`,column_2 from table_group_special_character_3;
Hope this helps!

ORA-00904 Error

I'm using JDBC to access a database and I'm basically querying by calling a String.
Here is my table book_table:
| title | book no. | author_first | author_last |
The cat, 0, Tim, Joe
The dog, 1, John, Soe
The mouse, 2, Josh, Moe
And here is the SQL statement I am querying with:
"select title from book_table where author_first like '%tim%' and author_last like '%joe%'";
I keep getting the error exception in Java: ORA-00904: "author_last": invalid identifier"
Any clue what I'm doing wrong?
It would be very likely that you may have used double quotes while creating the DDL for the book_table.
From the error it means that there is no such column as author_first
The names used in database should be working fine and that uppercase and lowercase could be very well be ignored if you would have not used double quotes.
create table book_table(title varchar2(100));
create table BOOK_TABLE(TITLE varchar2(100));
In both the cases
select * from book_table where title like '%mytitle%';
should be working fine.
However if you would have used double quotes, then you would have to use the exact cases while performing sql operations.
create table "BOOK_TABLE"("Title" varchar2(100));
In this case
select * from book_table where title like '%mytitle%'
would not work
You have to make use of
select * from book_table where "Title" like '%mytitle%';
I'd suggest dropping the table and recreating them without double quotes and see if that solves it.

Interacting with Wikipedia's API: grabbing the content of the table present on a page

E.g.
Here is a link http://en.wikipedia.org/wiki/Brad_Pitt_filmography
which lists all the films of brad Pitt in table format.
I want grab the table content.
Query-> http://en.wikipedia.org/w/api.php?action=query&titles=Brad_Pitt_filmography&
what other parameters will come in the query ?
How can I store it in MySQL Database using Java
use "action=parse":
http://en.wikipedia.org/w/api.php?action=parse&format=xml&prop=text&page=Brad_Pitt_filmography&section=1&contentformat=text/plain
you can change "prop" to text(html) or wikitext. for more info please check this:
http://en.wikipedia.org/w/api.php?action=help&modules=parse

FullTextSearch in MYSql

In mysql fulltext search, the match gives some values,I don't know which one is mentioned by that values?
I need the comparsion output in percentage?How can i achieve that?
SELECT id,title,body,MATCH (title,body)
AGAINST ('database') FROM articles WHERE MATCH (title,body)
AGAINST ('database');
Output:
id', 'title', 'body', 'MATCH'
1, 'MYSql Tutorial', 'My Sql is one of the database language', 0.93769526481628
10, 'MySQL vs. YourSQL', 'In the following database comparison ...', 0.93769526481628
6, 'MySQL Tutorial', 'DBMS stands for DataBase ...', 0.92749810218811
I think you mean you want the relevance as well as the result. That would look something like:
SELECT id, title, body, MATCH(title, body) AGAINST ('database') as Relevance FROM `articles` WHERE MATCH(title, body) AGAINST ('database' IN BOOLEAN MODE) ORDER BY `Relevance` DESC
edit I got the question wrong. Here's a list of all relevance's in percentage form:
SELECT a.id, a.title, a.body,
MATCH(a.title) AGAINST ('database') as titleRelevance,
MATCH(a.body) AGAINST ('database') as bodyRelevance,
MATCH(a.title) AGAINST ('database')/c.maxTitleRelevance *100 AS percentageTitleRelevance,
MATCH(a.body) AGAINST ('database')/d.maxBodyRelevance *100 AS percentageBodyRelevance,
c.maxTitleRelevance + d.maxBodyRelevance AS maxTotalRelevance,
(MATCH(a.title) AGAINST ('database')+MATCH(a.body) AGAINST ('database'))/(c.maxTitleRelevance + d.maxBodyRelevance)*100 AS percentageTotalRelevance
FROM `articles` a,
(SELECT MAX(MATCH(b.title) AGAINST('database')) as maxTitleRelevance FROM articles b LIMIT 1) c,
(SELECT MAX(MATCH(b.body) AGAINST('database')) as maxBodyRelevance FROM articles b LIMIT 1) d
WHERE MATCH(a.title, a.body) AGAINST ('database' IN BOOLEAN MODE)
This might be easier to read in the following fiddle. http://sqlfiddle.com/#!2/c7885/14
Full text searches in mysql are flaky at best. If the word database is in at least 50% of rows of the db, it will be ignored as a common stopword if you dont specify boolean mode like in the query above. In terms of how relevance is calculated I'm afraid I cant help you there.

Complicated LIKE Expression in derby (Java DB)

i've a table with ID, Name both are String type in ID i'v value like
1.3.6.1,
1.3.6.2,
1.3.6.1.2,
1.3.6.1.3,
1.3.6.1.4,
1.3.6.2.1.
1.3.7.2,
1.3.7.5,
1.3.8.1,
etc
I need to retrieve records like 1.3.6. .. but not like 1.3.6.ANY_NUMBER. ..,
Can u help me to write a Derby query for it
Thanks in advance
Hanks
You wrote the answer yourself
WHERE id LIKE '1.3.6%' AND id NOT LIKE '1.3.6.1%'
Maybe you better don't use LIKE but a simple equals:
SELECT * FROM db WHERE id='1.3.6';
Use this to select data from [column_name] in format up to 3 dots (like 1, 1.1, 1.1.1 but never 1.1.1.1)
SELECT SUBSTRING_INDEX([column_name],".",3) FROM [table_name];
or
Use this to select rows where [column_name] is not longer than 5 chars (1, 1.1, 1.1.1 but never 1.1.1.1)
SELECT [column_name] FROM [table_name] WHERE CHAR_LENGTH([column_name]) < 6

Categories