SQLite query in Objective C iPhone App - iphone

I am building this application which fetches data from SQLite database tables, when a string is being entered in to UISearchBar, all possible entries should be printed. Right now i am able to fetch all the entries from the table but i am not able to perform the specific search, can anyone help me with the query or give some example to refer?

SELECT * FROM mytable WHERE myfield like '%your searchwordhere%' by executing this query you can get the words which are having the word of your search keyword.

Try this:
SELECT fieldone, fieldtwo FROM mytable WHERE myfield like '%%SEARCH_TERM_GOES_HERE%%'

Related

PostgreSQL, allow to filter by not existing fields

I'm using a PostgreSQL with a Go driver. Sometimes I need to query not existing fields, just to check - maybe something exists in a DB. Before querying I can't tell whether that field exists. Example:
where size=10 or length=10
By default I get an error column "length" does not exist, however, the size column could exist and I could get some results.
Is it possible to handle such cases to return what is possible?
EDIT:
Yes, I could get all the existing columns first. But the initial queries can be rather complex and not created by me directly, I can only modify them.
That means the query can be simple like the previous example and can be much more complex like this:
WHERE size=10 OR (length=10 AND n='example') OR (c BETWEEN 1 and 5 AND p='Mars')
If missing columns are length and c - does that mean I have to parse the SQL, split it by OR (or other operators), check every part of the query, then remove any part with missing columns - and in the end to generate a new SQL query?
Any easier way?
I would try to check within information schema first
"select column_name from INFORMATION_SCHEMA.COLUMNS where table_name ='table_name';"
And then based on result do query
Why don't you get a list of columns that are in the table first? Like this
select column_name
from information_schema.columns
where table_name = 'table_name' and (column_name = 'size' or column_name = 'length');
The result will be the columns that exist.
There is no way to do what you want, except for constructing an SQL string from the list of available columns, which can be got by querying information_schema.columns.
SQL statements are parsed before they are executed, and there is no conditional compilation or no short-circuiting, so you get an error if a non-existing column is referenced.

postgresql queries with expression

I create this table 'friend' with PgAdmin III. I would like to select all the data where the word 'grand' is contained in the columns keywords.
I tried the following:
SELECT * FROM friend where keywords LIKE 'grand';
but it does not work, i don't receive any results.
Would someone have any ideas ?
Thank you
Use wildcards:
select *
from friend
where keywords like '%grand%';
Read more in the documentation.

Get all except built-in schema names from a specific database using T-SQL query

As the title entails, is this possible?
Using the following code returns a list of the built-in schemas as well as all the other schemas:
select name from MyDbName.sys.schemas
What I want is only the schemas I created for each table. I also tried doing the following code but its not very solid.
select name from MyDbName.sys.schemas where name not like '%db_%'
select distinct SCHEMA_NAME(schema_id) from sys.objects
I think this will work for you.

PostgreSql XML Text search

I have a text column in a table. We store XML in this column. Now I want to search for tags and values
Example data:
<bank>
<name>Citi Bank</name>
.....
.....
/<bank>
I would like to run the following query:
select * from xxxx where to_tsvector('english',xml_column) ## to_tsquery('<name>Citi Bank</name>')
This works fine but it also works for tags like name1 or no tag.
How do I have to setup my search in order for this to work so I get an exact match for the tag and value ?
You could use the xpath function like this
select *
from xxx
where xpath(xml_column, 'bank/name/text()') = 'CitiBank';
BUT it won't use the full-text search index. You could use a subquery to find probable matches and avoid full scans, and the xpath expression for getting correct answers, or create a function index if the queries are going to be always the same.
You might want to reconsider storing XML in a database, instead you could look at inserting the data into related tables, since using XML is a poor replacement for a relational store. Even if you go with XML in database, use the XML type, not the TEXT type, and create an index like this (yes, basically you'd need an index per xpath expression):
CREATE INDEX my_funcidx ON my_table USING GIN ( CAST(xpath('/bank/name/text()', xmlfield) AS TEXT[]) );
then, query it like this:
SELECT * FROM my_table WHERE CAST(xpath('/bank/name/text()', xmlfield) AS TEXT[]) #> '{Citi Bank}'::TEXT[];
and this will use the index, as EXPLAIN will indicate.
The important part is the CASTing to TEXT[], as XML[], which the xpath function returns, isn't indexable by default.

Write Insert Query in SQLite3 in iPhone

Hi need insert data in table if the record is not already exits
Ex:
IF NOT EXISTS (SELECT Id FROM table WHERE id=_Id) THEN
INSERT INTO tbale(.....)
This can be easily done using stored procedure in MySql. But I want to d same thing in SQLite by writing a single query statement.
Pleas help
INSERT INTO CategoryMaster (CategoryID, CategoryText) SELECT %d,'%#' WHERE NOT EXISTS (SELECT 1 FROM CategoryMaster WHERE CategoryID= %d)
Its working for me :)
SQlite doesn't have stored procedudes that you need to do logic like this. But you can always extend sqlite with simple C-functions. Or you could simply code this logic in whatever language you are writing your program in. I don't think the performance hit is that great. Did your profiling show that this is a critical path that needs to be optimized?
Put a unique index on the id column (or make it the primary key) then use:
REPLACE INTO table(.....)
see:
http://www.sqlite.org/lang_insert.html or http://www.sqlite.org/lang_replace.html
I would do this by running a first query to see if the record exists then decide whether to run an insert or update. I don't think SQLite has a fully featured query language to support the desired shorter approach.