Escape characters in NSMutableDictionary - iphone

When i pass an NSString as object for a key in an NSMutableDictionary it seems to contain escape characters as regular characters. How can I remove them? I have tried stringByReplacingOccurrencesOfString:#"\" withString:#"" byt it does not work.
Here is my code and output:
Code:
NSString* fql1 = [NSString stringWithFormat:#"SELECT page_id FROM place WHERE distance(latitude,longitude,\"%#\",\"%#\") < 1500 AND checkin_count > 5", latitude, longitude];
NSString* fql2 = [NSString stringWithFormat:#"SELECT author_uid, post_id, timestamp, tagged_uids, message FROM checkin WHERE page_id IN (select page_id from #PlaceQuery)"];
NSString* fql = [[NSString stringWithFormat:#"{\"PlaceQuery\":\"%#\",\"CheckInQuery\":\"%#\"}",fql1,fql2] stringByReplacingOccurrencesOfString:#"\\" withString:#""];
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObject:fql forKey:#"queries"];
NSLog(#"%#", fql);
NSLog(#"%#", params);
Output from NSLog(#"%#", fql):
{"PlaceQuery":"SELECT page_id FROM place WHERE distance(latitude,longitude,"37.331693","-122.030457") < 1500 AND checkin_count > 5","CheckInQuery":"SELECT author_uid, post_id, timestamp, tagged_uids, message FROM checkin WHERE page_id IN (select page_id from #PlaceQuery)"}
Output from NSLog(#"%#", params):
{
queries = "{\"PlaceQuery\":\"SELECT page_id FROM place WHERE distance(latitude,longitude,\"37.331693\",\"-122.030457\") < 1500 AND checkin_count > 5\",\"CheckInQuery\":\"SELECT author_uid, post_id, timestamp, tagged_uids, message FROM checkin WHERE page_id IN (select page_id from #PlaceQuery)\"}";
}
I have been searching for hours for a solution so thanks A LOT in advance ;)

NSLog escapes strings when they're printed out--you probably don't actually have escape characters in your string.
Try this:
NSLog(#"%s\n", [ string UTF8String ] )
and see what happens...

This turned out to be an issue about permissions... yes, embarrasing...
What confused me was that the escape characters was in the response output...
Thanks #williham and #nielsbot

Related

How to run SQLite Query even if Input is Blank

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

WHERE AND in Sqlite Statement

I am having a problem combining two WHERE statements in the following SQL statement :
query = [[NSString alloc] initWithFormat: #"SELECT Name, Description,Postcode,AddressLine1, ImageURL, Free, Area, OpeningTimes, NearestTube, Cost,UniqueID, URL, Number, FirstLetter FROM MainDetails WHERE Free ='Y' AND FirstLetter = '%#%'",tmpLike];
I want to say where Free is equal to Y and FirstLetter is equal to tmplike, however the above is not working.
Can anyone help ?
Your using a wildcard so you want LIKE not equality (=);
... WHERE Free ='Y' AND FirstLetter LIKE '%#%'"
TRY THIS REMOVE % FROM LAST
query = [[NSString alloc] initWithFormat: #"SELECT Name, Description,Postcode,AddressLine1, ImageURL, Free, Area, OpeningTimes, NearestTube, Cost,UniqueID, URL, Number, FirstLetter FROM MainDetails WHERE Free ='Y' AND FirstLetter = '%#'",tmpLike];
try with escape char '/'
query = [[NSString alloc] initWithFormat: #"SELECT Name, Description,Postcode,AddressLine1, ImageURL, Free, Area, OpeningTimes, NearestTube, Cost,UniqueID, URL, Number, FirstLetter FROM MainDetails WHERE Free ='Y' AND FirstLetter = '%#\%'",tmpLike];
use this one it work fine
query = [[NSString alloc] initWithFormat: #"SELECT Name, Description,Postcode,AddressLine1, ImageURL, Free, Area, OpeningTimes, NearestTube, Cost,UniqueID, URL, Number, FirstLetter FROM MainDetails WHERE Free ='Y' AND FirstLetter LIKE '%# %%'",tmpLike];
Maybe simply brackets ?
... WHERE (Free ='Y') AND (FirstLetter LIKE '%#')",tmpLike];

how to select a string attribute from a table in objective c

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];

Xcode: How do I wrap a long string around to the next line in the xcode editor

I've got a long string an sql statement, which I want to wrap onto the next line in the xcode editor, how do I wrap it round e.g.
[db executeUpdate:#"insert into test (rid, one, two, amount, startdate, recurrance) values (nil, ?, ?, ?, ?, ?)",
Try:
[db executeUpdate:#"insert into test (rid, one, two, amount, values"
" startdate, recurrance)(nil, ?, ?, ?, ?, ?)",...
I had similar issue... call me paranoid.. but i like my sqlstring to look pretty and easy to trace in case something went wrong i used the stringWithFormat
example:
NSString sqlString = [NSString stringWithFormat: #"%# %# %# %# %#",
#"SELECT name, address, phone",
#"FROM whateverDatabase",
#"LEFT JOIN ...",
#"WHERE ...",
#"AND ...."];
hopefully if this is what you are looking for
(i figured i posted on the wrong page... maybe here is better)

iphone:creating a query

in my table of sqlite database, there are fields named id,firstName,middle, lastName
now i want to run the query as following
MyQuery = [NSString stringWithFormat:#"select id,(firstName || middlename || lastname) as Name from Students where firstname like '%#' or middlename like '%#' or lastname like '%#'",searchBarString,searchBarString,searchBarString];
above query work well. but i want to add a space between first and middle name(also middle and last name). so how can i perform this task? please suggest
MyQuery = [NSString stringWithFormat:#"select id,(firstName || ' ' || middlename || ' ' || lastname) as Name from Students where firstname like '%#' or middlename like '%#' or lastname like '%#'",searchBarString,searchBarString,searchBarString];
Try this!
You could simply return the three columns in separate variables and then join them when displaying them:
NSString *lastName = ...;
NSString *middleName = ...;
NSString *firstName = ...;
NSString *joined = [NSString stringWithFormat:#"%# %# %#",lastName,middleName,firstName];
This has the advantage that you can sort by any of the three columns in the UI without making new sqlite queries and allow the user to edit these.