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
Related
I am using Join operator to check two values in table but it does not work
select = [[NSString alloc] initWithFormat:#"select * FROM ContentMaster LEFT JOIN Category ON ContentMaster.CategoryID= Category.CategoryID where ContentMaster.ContentTitle='%#' || ContentMaster.ContentTagText='%#' ",appDelegate.tagInput,appDelegate.tagInput];
If i do not use operator and use one in where clause then it shows result.
like
select = [[NSString alloc] initWithFormat:#"select * FROM ContentMaster LEFT JOIN Category ON ContentMaster.CategoryID= Category.CategoryID where ContentMaster.ContentTitle='%#'",appDelegate.tagInput];
select = [[NSString alloc] initWithFormat:#"select * FROM ContentMaster LEFT JOIN Category ON ContentMaster.CategoryID= Category.CategoryID where ContentMaster.ContentTagText='%#'",appDelegate.tagInput];
Any idea how to fix this issue so that or operator works
In SQL (and SQLite in particular), operator || typically means string concatenation.
If you want logical or, use operator OR.
In sqlite just use or for logical or operation:
SELECT *
FROM ContentMaster
LEFT JOIN Category ON ContentMaster.CategoryID = Category.CategoryID
WHERE ContentMaster.ContentTitle = '%#' OR ContentMaster.ContentTagText= '%#'
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= \'%#\'"
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];
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.
I'm trying to use the SQLite C API in my iPhone app. I'm trying to query an SQLite database for the number of records that have been completed after a certain date. The database saves the completed date as text in YYYY-MM-dd format. For example the text 2009-04-10 might appear as a completed date.
When I query the database from the commandline my query works, but when run from the app, it doesn't. Here's what I'm doing:
From the commandline, I run this query:
sqlite> SELECT COUNT(*) FROM tasks WHERE completed > '2009-04-09'
...> go
1
As you can see, there is one record found as I expect.
In my app, I execute this code (written in Objective-C, obviously):
static sqlite3_stmt *count_tasks_statement = nil;
if(count_tasks_statement == nil) {
const char *sql = "SELECT COUNT(*) FROM tasks WHERE completed > '?'";
if (sqlite3_prepare_v2(database, sql, -1, &count_tasks_statement, NULL) != SQLITE_OK) {
NSAssert1(0, #"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
}
NSString *today = #"2009-04-09";
sqlite3_bind_text(count_tasks_statement, 1, [today UTF8String], -1, SQLITE_TRANSIENT);
// Get the row count from the query
NSInteger taskCount = 0;
if(sqlite3_step(count_tasks_statement) == SQLITE_ROW) {
// The second parameter indicates the column index into the result set.
taskCount = sqlite3_column_int(count_tasks_statement, 0);
}
// Reset the statement for future reuse.
sqlite3_reset(count_tasks_statement);
When I use the debugger on this code, and examine the taskCount variable, it is set to 0, indicating that no records were found. (If I change the code to return primary keys for found rows, it still returns nothing.)
Since it works from the commandline, but doesn't in my code, I assume that I'm doing something wrong with either the quoting of the question mark in my SQL, or with the binding of the literal text date to the query. But, I've tried it lots of different ways with no luck. Help!
Don't put parameter placeholders inside quotes, even if the value is a string or date literal.
const char *sql = "SELECT COUNT(*) FROM tasks WHERE completed > ?";
I think that you do not need the extra quotes around the question mark.
Try
const char *sql = "SELECT COUNT(*) FROM tasks WHERE completed > ?";
and it should work.