I am using FMDB, which is a wrapper for SQLite. http://github.com/ccgus/fmdb
Here is my query line:
FMResultSet *athlete = [db executeQuery:#"SELECT * FROM athletes WHERE athlete_name LIKE ?", search_text];
Using this I can get a result back if I type in the exact name of the athlete. But, I'm trying to use LIKE so I can search on the name. But, when I add %?% instead of just ? ... nothing returns. And there are no errors.
Has anyone ran into this before and know what I'm doing wrong?
Thanks everyone!
The wildcard characters (%) have to be part of the substituted variable, not the query string:
FMResultSet *rs = [db executeQuery:#"SELECT * FROM athletes WHERE athlete_name LIKE ?",
[NSString stringWithFormat:#"%%%#%%", search_text]];
Related
I have IPhone application in which I am inserting data. It works fine when I fetch all the data without any condition then it works fine if given any condition like
following query then it does not show anything.
NSString *select =
[NSString stringWithFormat:
#"SELECT * from ContentMaster As ml
LEFT JOIN ContentTagging As cat
ON cat.ContentID = ml.ContentID
where cat.ContenTagText= \'%#\'" ,appDelegate.tagInput];
const char *sql = [select UTF8String];
then it does not show any thing using this if i use the select * form ContentMaster then it shows all records added.
Try this.
SELECT * from ContentMaster
LEFT JOIN ContentTagging
ON ContentMaster.ContentID = ContentTagging.ContentID
WHERE ContentTagging.ContenTagText= \'%#\'"
In an iPhone App I've written, I connect to a web service for executing SQL Server stored procedures. I sent some simple queries, it works fine.
I tried some simple stored procedures, yes, again it works well. The issue is with this particular stored procedure's conversion to NSString. I realize am missing adding an escape character for some specific character. I've added escape characters for square brackets '[]', and for '%'. Am saying that escape characters is the issue because the error am getting after execution is saying 'syntax error in the stored procedure'.
But when I execute this directly in the DB (i.e. locally through SQL command-line client), am not getting any error ... it works fine. So, seems to be an issue when converting it to NSString. Please help.
DECLARE #ts_now bigint = (SELECT cpu_ticks/(cpu_ticks/ms_ticks) FROM sys.dm_os_sys_info);
SELECT TOP(10) SQLProcessUtilization AS \\[SQL Server Process CPU Utilization\\],
SystemIdle AS \\[System Idle Process\\],
100 - SystemIdle - SQLProcessUtilization AS \\[Other Process CPU Utilization\\],
DATEADD(ms, -1 * (#ts_now - \\[timestamp\\]), GETDATE()) AS \\[Event Time\\]
FROM (
SELECT record.value('(./Record/#id)\\[1\\]', 'int') AS record_id,
record.value('(./Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)\\[1\\]', 'int')
AS \\[SystemIdle\\],
record.value('(./Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)\\[1\\]',
'int')
AS \\[SQLProcessUtilization\\], \\[timestamp\\]
FROM (
SELECT \\[timestamp\\], convert(xml, record) AS \\[record\\]
FROM sys.dm_os_ring_buffers
WHERE ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR'
AND record LIKE '%%<SystemHealth>%%') AS x
) AS y
ORDER BY record_id DESC;
Am getting an error saying the syntax is wrong. I read this stored procedure into a NSString object and then send the NSString to the web service like this :
NSError *error;
NSString *txtFilePath = [[NSBundle mainBundle] pathForResource:#"Proc_Usage" ofType:#"txt"];
NSString *procUsage = [NSString stringWithContentsOfFile:txtFilePath encoding:NSUTF8StringEncoding error:&error];
if (procUsage == nil) {
return -1;
}
[[DbConnect DbConnect_Singleton] executeInDb:procUsage];
Have you tried it without any escaping at all? I don't see why it should need any escaping at all, given that you are reading the string from a file - it's not like it's a string literal.
It's possible that the double percents are being read as a single percent. Can you try changing:
LIKE '%%<SystemHealth>%%'
to
LIKE '%%%%<SystemHealth>%%%%'
I am doing an iPad project, there are 3 tables in UI. if i select row in each table. Sqlite query will take the value from table and run the query. please find my Query below.
NSString * str3=tableFou.string4;
NSString * str4=tableFiv.string5;
NSString * str2=tablethr.string3;
sqlite3 *database;
favorite=[[NSMutableArray alloc]init];
if(sqlite3_open([databasePath UTF8String], &database)== SQLITE_OK){
NSString *sql=[NSString stringWithFormat:#"Create view prizeview as SELECT country.name,item_country.id,text.text,substr(text,1,2) FROM country,item_country,prize,item_prize,gender,item_gender,text WHERE country.name = '%#' AND text.item = item_country.item AND country.id = item_country.id AND prize.name = '%#' and text.item=item_prize.item and prize.id = item_prize.id and gender.name = '%#' and text.item=item_gender.item and gender.id = item_gender.id ",str3,str4,str2];
const char *sqlStatement = [sql UTF8String];;
problem is that, the query is running if i select all the tables only.. but i want results if i select even one table. how can i do that?
Try with if condition use with and condition and pass all your str in if condition only.
use your if condition like this.
if(([propertyTypeLabel.text length]==0) || ([cityLabel.text length]==0 ) ||( [locationLabel.text length]==0 )|| ([priceLabel.text length] ==0)||([postedLabel.text length]==0))
In this like any of text is zero condition true you also use if ant of the table cell is tap your query run.
You can't search for empty fields unless the field is actually empty. F.ex. if you search
select * from mytable where username = "";
You won't find it, but if you are ok with the field being empty or whatever result, you must use like instead of =
select * from mytable where username like "%";
or better yet, you can create your query like this, lets say you have three fields to search for, username, firstname and lastname. The code below needs a few more if's and buts, but you should be able to make this work and only add the parameters to where if they fulfill the requirements.
NSMutableString *query = [[NSMutableString alloc] init];
[query appendString:#"select * from table where "];
if (username.text.lenghth >1) [query appendString:[NSString stringWithFormat:#"username = '%#'"]];
if (firstname.text.length >1) [query appendString:[NSString stringWithFormat:#"&& firstname = '%#'"]];
if (lastname.text.length >1) [query appendString:[NSString stringWithFormat:#"&& lastname = '%#'"]];
Hope this helps
I have written the query given below for fetching data with two parameters. Can you suggest the correct SQL syntax for this purpose.
SQL query:
FMResultSet *resultOfDesiredCuisine = [db executeQueryWithFormat:#"select * from dishes where cuisineId IN (select cuisineId from cuisines where Lower(cuisineName) like %#) and
categoryID IN \"(select categoryID from categories where Lower(categoryName) like %#)",cuisineName,categoryName];
SQLite does not work white the %# syntax but uses ?
FMResultSet *resultOfDesiredCuisine = [db executeQueryWithFormat:#"SELECT * FROM dishes WHERE cuisineId IN (SELECT cuisineId FROM cuisines WHERE LOWER(cuisineName) like ?) AND
categoryID IN (SElECT categoryID FROM categories WHERE LOWER(categoryName) LIKE ?)", cuisineName, categoryName];
I have a method which is supposed to return a PatientDetails object when I insert an unique nric as the parameter. This doesn't work. However when I changed this method to accept int age instead of NSString * nric, it worked.
Is there something wrong with my syntax "WHERE ic = %s" cause it seems wierd. I have googled 2 days on this and cannot find a solution.
Please help as I am a newbie.
- (PatientDetails *)patientDetails:(NSString *)nric {
PatientDetails *retval = nil;
NSString *query = [NSString stringWithFormat:#"SELECT ic, district, age, race, referralSource, DMRelatedAdmin, postalCode FROM patientInfo WHERE ic = %s" , nric];
sqlite3_stmt *statement;
%s denotes the standard C string. In order to use NSString objects as the argument, you need to use the object notation %#.
WHERE ic = '%#'
Yes, the format specifier for Objective-C strings is %#. So it should be,
NSString *query = [NSString stringWithFormat:#"SELECT ic, district, age, race, referralSource, DMRelatedAdmin, postalCode FROM patientInfo WHERE ic = %#" , nric];