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

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

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

Escape characters in NSMutableDictionary

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

iphone sqlite3_column_text issue

I have a column in sqlite3 table. Column has values like
1 ½”
1 ¾”
2 ½” etc.
Column has VARCHAR datatype.
I am using this code.
pref_HoseDiameter = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
Now, when I am fetching these values from database, I am getting pref_HoseDiameter string values like this:
1 1/2"
1 3/4"
2 1/2"
How to fetch those values as they are in database or how to convert them that look like database values.
Help would be appreciated.
Thanks in advance.
Try to use sqlite3_column_text16 to get the string as UTF-16, then create the string with +stringWithCharacters:.
const UInt16* text = sqlite3_column_text16(statement, column);
int text_len = // find the first 0x0000 in text;
NSString* str = [NSString stringWithCharacters:text length:text_len];

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.