AWS SimpleDB Select Expression iPhone - iphone

I need to construct a selectExpression for SimpleDB where I only want items with attributes = to X, sorted in descending order by attribute Y. All I have so far is this:
NSString *selectExpression = [NSString stringWithFormat:#"select itemName() from `%#`",domainString];
#try {
SimpleDBSelectRequest *selectRequest2 = [[[SimpleDBSelectRequest alloc] initWithSelectExpression:selectExpression] autorelease];
SimpleDBSelectResponse *selectResponse = [[Constants sdb] select:selectRequest2];
if (items == nil) {
items = [[NSMutableArray alloc] initWithCapacity:[selectResponse.items count]];
}
else {
[items removeAllObjects];
}
for (SimpleDBItem *item in selectResponse.items) {
[items addObject:item.name];
}
[items sortUsingSelector:#selector(compare:)];
}
#catch (AmazonServiceException *exception) {
NSLog(#"Exception = %#", exception);
}
I would prefer to only select items (above) with attributes X by Y rather than get all items and then sort. How do I add this? Does it go in the selectExpression string or somewhere else. Many thanks!

Try this out --
Select x,y from <domain_name> where itemName() is not null order by itemName() asc

NSString *selectExpression = [NSString stringWithFormat:#"select itemName() from `%#` where X = '%#' order by 'X' descending",domainName,attributeValue];

Related

How to Show a String instead of address in textView

i am trying to get a random string from database i have tried to convert array in a String but when i click button i only get address can anyone help me how to show that string in my button action?
- (IBAction)randBtn:(id)sender {
SendQuoteVC *svc= [[SendQuoteVC alloc]initWithNibName:#"SendQuoteVC" bundle:nil];
DBHandler *db =[[DBHandler alloc]init];
arr =[db randomQuote];
NSMutableString * show = [[NSMutableString alloc] init];
for (NSObject * obj in arr)
{
[show appendString:[obj description]];
}
NSLog(#"The concatenated string is %#", show);
NSString *rs =[NSString stringWithFormat:#" %#",show];
svc.quote=rs;
[self.navigationController pushViewController:svc animated:YES];
}
and my log message is
2012-12-26 17:02:46.062 SendQuote[2281:c07] QUERY: SELECT * FROM quotes ORDER BY RANDOM() LIMIT 1
2012-12-26 17:02:46.097 SendQuote[2281:c07] txtQuote is (
"<quoteDC: 0xa9a94c0>"
)
2012-12-26 17:02:46.100 SendQuote[2281:c07] rand is (
"<quoteDC: 0xa9a94c0>"
)
i have changed my user entity class to quoteDC class and my Db function is
-(NSMutableArray *)randomQuote{
NSMutableArray *dataArray =[[NSMutableArray alloc]init];
NSString * sqlStr=[NSString stringWithFormat:#"SELECT * FROM quotes ORDER BY RANDOM() LIMIT 1"];
sqlite3_stmt *ReturnStatement = (sqlite3_stmt *) [self getStatement: sqlStr];
while (sqlite3_step(ReturnStatement)==SQLITE_ROW)
{
#try
{
quoteDC *worddc = [[quoteDC alloc] init];
NSString *userid = [NSString stringWithUTF8String:(char *)sqlite3_column_text(ReturnStatement, 0)];
NSString *name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(ReturnStatement, 1)];
worddc.quote_id=[userid integerValue];
worddc.quotes=name;
[dataArray addObject:worddc];
}
#catch (NSException *ept) {
NSLog(#"Exception in %s, Reason: %#", __PRETTY_FUNCTION__, [ept reason]);
}
}
return dataArray;
}
the [db randomQuote] method returns an array of users not strings, you can typecast it to user object than get your desired property out of it!
It seems like your array is an array of objects of some user class rather than array of strings.
Hence the call to
[obj description]
is returning an object instead of a string.
You would need to override the description method in your user class, and return string.
This code makes the issue:
for (NSObject * obj in arr)
{
[show appendString:[obj description]];
}
Because your entity class don't have a property like description.
Instead of this write:
for (quoteDC * obj in arr)
{
[show appendString:[obj quotes]];
}
Try this i think it work for u...
- (IBAction)randBtn:(id)sender {
SendQuoteVC *svc= [[SendQuoteVC alloc]initWithNibName:#"SendQuoteVC" bundle:nil];
DBHandler *db =[[DBHandler alloc]init];
arr =[db randomQuote];
NSString *rs =(NSString *)arr;
svc.quote=rs;
[self.navigationController pushViewController:svc animated:YES];
}
Instead of for (NSObject * obj in arr) use the object that is stored in arr.
I guess it is some DBHandler object. So use
for (DBHandler * dbh in arr){
[show appendString:[dbh thePropertyYouWantToAppend]];
}

UITableview with sections for Names in sorted order

I know how to work with UITableview delegates and datasource protocol methods. So, I can work with sections. Now I would like to use a single query to fetch all the records from the database, ie, select firstname, lastname from contacts order by firstname asc. (Now I am using 26 individual query, I know this is not the right solution)
Now I would like to group the contacts based on its alphabets for sections. If no contacts starts from letter S. Then the section for S should not appear. Can you please provide me the right code or any tutorial? Thanks
Can you plese remodify this script?
-(void)read_data_fromDB
{
objectsForCharacters = [[NSMutableDictionary alloc]init];
sqlite3 *db = [eikardAppDelegate getNewDBConnection];
arr_sectionTitles = [[NSMutableArray alloc] init];
for(char c='A';c<='Z';c++)
{
NSMutableString *query = nil;
query = [NSMutableString stringWithFormat:#"select first_name, middle_name, last_name from phonebook where first_name like '%c%%';",c];
const char *sql = [query UTF8String];
sqlite3_stmt *selectAllStmt = nil;
if(sqlite3_prepare_v2(db,sql, -1, &selectAllStmt, NULL)!= SQLITE_OK)
NSAssert1(0,#"error preparing statement",sqlite3_errmsg(db));
else
{
NSMutableArray *arr_persons = [[NSMutableArray alloc] init];
while(sqlite3_step(selectAllStmt)==SQLITE_ROW)
{
//NSLog(#"Firstname : %#",query);
PersonInfo *person =[[PersonInfo alloc] init];
char *chrstr =(char *)sqlite3_column_text(selectAllStmt, 0);
if(chrstr !=NULL)
{
person.str_firstName = [NSString stringWithUTF8String:chrstr];
NSLog(#"Firstname : %#",person.str_firstName);
}
chrstr =(char *)sqlite3_column_text(selectAllStmt, 1);
if(chrstr !=NULL)
{
person.str_middleName = [NSString stringWithUTF8String:chrstr];
NSLog(#"Middlename : %#",[NSString stringWithUTF8String:chrstr]);
}
chrstr =(char *)sqlite3_column_text(selectAllStmt, 2);
if(chrstr !=NULL)
{
person.str_lastName = [NSString stringWithUTF8String:chrstr];
NSLog(#"Lastname : %#",person.str_lastName);
}
[arr_persons addObject:person];
[person release];
}
if([arr_persons count]>0)
{
NSString *keyValue = [NSString stringWithFormat:#"%c",c];
[objectsForCharacters setObject:arr_persons forKey:keyValue];
[arr_sectionTitles addObject:keyValue];
}
//[arr_persons release];
}
sqlite3_finalize(selectAllStmt);
}
sqlite3_close(db); }
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [arr_sectionTitles count];
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *secTitle = [arr_sectionTitles objectAtIndex:section];
return [[objectsForCharacters objectForKey:secTitle] count];
}
It depends of the origin of the data. If you are using a NSFetchedResultsController then you can use initWithFetcRequest:managedObjectContext:sectionNameKeyPath:cacheName: with the key name of the property for the sectionNameKeyPath:. You will multi-section ordered results.
If you have the result data from the query (for the whole selection, not for just one of the 26 chars) in an array, then you'd better rearranging data in an ordered array of arrays. That is, each element of the master array is the array of results for each letter in index.
if you use [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles] as titles for sections, it is easy to implement an index for the table. You don't have to create a section for each index, you will reference the correct section for each index in the method tableView:sectionForSectionIndexTitle:atIndex:

I can't trap FMDB empty resultset

I am having trouble trapping an empty result set from FMDB.
The code is below. I am getting NSLog's from the database opening and closing and NSLog "1" but none of the ones in the If statement!
If I have data in the database its fine, but I want to trap and edit result if the database is empty.
[self openDatabase];
NSNumberFormatter *nfcurrency = [[NSNumberFormatter alloc]init];
[nfcurrency setNumberStyle:NSNumberFormatterCurrencyStyle];
[nfcurrency setLocale:[NSLocale currentLocale]];
FMResultSet *result = [[self getDatabase]executeQuery:#"SELECT BFNeeded FROM tblBets ORDER BY pk DESC LIMIT 1,1;"];
//FMResultSet *result = [[self getDatabase]executeQuery:#"SELECT BFNeeded FROM tblBets ORDER BY pk DESC LIMIT 1,1;"];
NSLog(#"1");
if (result == NULL) {
NSLog(#"Last BFNeeded Result = nil");
} else {
while ([result next]) {
NSLog(#"HERE");
NSString *lastBFNeeded = [nfcurrency stringFromNumber:[NSNumber numberWithDouble:[result doubleForColumn:#"BFNeeded"]]];
NSLog(#"lastBFNeeded=%#",lastBFNeeded);
}
}
NSLog(#"ClosingDB");
[self closeDatabase];
Continuing after getting first reply:
I can't get hasAnotherRow to work as expected.
I have this code:
FMResultSet *result = [[self getDatabase]executeQuery:#"SELECT BFNeeded FROM tblBets ORDER BY pk DESC LIMIT 0,1;"];
if (result == nil) {
NSLog(#"Last BFNeeded Result = nil");
}
else {
NSLog(#"has results1: %#", [result hasAnotherRow] ? #"YES" : #"NO");
while ([result next]) {
NSLog(#"has results2: %#", [result hasAnotherRow] ? #"YES" : #"NO");
}
}
With a database that returns a result, I get result1 NO, result2 YES so I assume the hasAnotherRow must go inside the while ([result next]) loop.
However with an empty database, I get result1 NO and it doesn't even get to result2!
the "result" will never be nil for a query that produces 0 rows.
also, you shouldn't compare object pointers to NULL -- compare to nil. See this question: NULL vs nil in Objective-C
try this:
FMResultSet *result = [[self getDatabase]executeQuery:#"SELECT BFNeeded FROM tblBets ORDER BY pk DESC LIMIT 1,1;"];
NSLog ( #"has results: %#", [result hasAnotherRow] ? #"YES" : #"NO" );
NSInteger count;
query=[NSString stringWithFormat:#"select Count(*) from %# where name = 'brandon' ",dbName];
results = [database executeQuery:query ];
while([results next]) {
count = [results intForColumnIndex:0];
NSLog(#"count:%d",count);
}
will set count equal to zero if there is no entry

How to sort NSMutableArray elements?

I have model class which contains NSString's- studentName, studentRank and studentImage. I wanna sort the NSMutableArray according to studentRanks. what I have done is
- (void)uploadFinished:(ASIHTTPRequest *)theRequest
{
NSString *response = nil;
response = [formDataRequest responseString];
NSError *jsonError = nil;
SBJsonParser *json = [[SBJsonParser new] autorelease];
NSArray *arrResponse = (NSArray *)[json objectWithString:response error:&jsonError];
if ([jsonError code]==0) {
// get the array of "results" from the feed and cast to NSArray
NSMutableArray *localObjects = [[[NSMutableArray alloc] init] autorelease];
// loop over all the results objects and print their names
int ndx;
for (ndx = 0; ndx < arrResponse.count; ndx++)
{
[localObjects addObject:(NSDictionary *)[arrResponse objectAtIndex:ndx]];
}
for (int x=0; x<[localObjects count]; x++)
{
TopStudents *object = [[[TopStudents alloc] initWithjsonResultDictionary:[localObjects objectAtIndex:x]] autorelease];
[localObjects replaceObjectAtIndex:x withObject:object];
}
topStudentsArray = [[NSMutableArray alloc] initWithArray:localObjects];
}
}
How can I sort this topStudentsArray according to the ranks scored by the Students and If the two or more student have the same rank, How can I group them.
I did like this
TopStudents *object;
NSSortDescriptor * sortByRank = [[[NSSortDescriptor alloc] initWithKey:#"studentRank" ascending:NO] autorelease];
NSArray * descriptors = [NSArray arrayWithObject:sortByRank];
NSArray * sorted = [topStudentsArray sortedArrayUsingDescriptors:descriptors];
but this is not displaying results properly. please help me to overcome this problem. thanks in advance.
doing something like this might do the trick
Initially sort the arrGroupedStudents in the (ascending/descending) order of studentRank
//Create an array to hold groups
NSMutableArray* arrGroupedStudents = [[NSMutableArray alloc] initWithCapacity:[topStudentsArray count]];
for (int i = 0; i < [topStudentsArray count]; i++)
{
//Grab first student
TopStudents* firstStudent = [topStudentsArray objectAtIndex:i];
//Create an array and add first student in this array
NSMutableArray* currentGroupArray = [[[NSMutableArray alloc] initWithCapacity:0] autorelease];
[currentGroupArray addObject:firstStudent];
//create a Flag and set to NO
BOOL flag = NO;
for (int j = i+1; j < [topStudentsArray count]; j++)
{
//Grab next student
TopStudents* nextStudent = [topStudentsArray objectAtIndex:j];
//Compare the ranks
if ([firstStudent.studentRank intValue] == [nextStudent.studentRank intValue])
{
//if they match add this to same group
[currentGroupArray addObject:nextStudent];
}
else {
//we have got our group so stop next iterations
[arrGroupedStudents addObject:currentGroupArray];
// We will assign j-1 to i
i=j-1;
flag = YES;
break;
}
}
//if entire array has students with same rank we need to add it to grouped array in the end
if (!flag) {
[arrGroupedStudents addObject:currentGroupArray];
}
}
Finally your arrGroupedStudents will contain grouped array with equal rank. I have not test run the code so you might need to fix a few things falling out of place. Hope it helps
If you want to display in the order of ranks, you should set the ascending as YES.
NSSortDescriptor * sortByRank = [[NSSortDescriptor alloc] initWithKey:#"studentRank" ascending:YES];
static int mySortFunc(NSDictionary *dico1, NSDictionary *dico2, void *context)
{
NSString *studentName1 = [dico1 objectForKey:#"studentName"];
NSString *studentName2 = [dico2 objectForKey:#"studentName"];
return [studentName1 compare:studentName2];
}
- (IBAction)sortBtnTouched:(id)sender
{
[topStudentsArray sortUsingFunction:mySortFunc context:NULL];
}

SQL select statement equivalent for query on plist

I am trying to figure out the best approach for queries on a plist. Thus, attempting at making an sql equivalent to "SELECT * FROM sometable WHERE somecol = someval AND someothercol = someotherval AND ".... so on and soforth ... using a plist as the sql table equivalent. Coming from Ruby and mysql, this just seems like a lot of code for one simply query. The results come back as expected (at least at first run they did, I have not tested this rigorously) with no errors.
So here is the question: Is there some simple method hiding in the docs somewhere that would make this less clunky?
and if not what is a better approach?
EPFramework.m
// LOAD PLIST AND FILTER MULTIPLE TIMES
-(NSMutableArray *)loadPlistAndFilterMultipleTimes:(NSString *)plist ArrayOfKeys:(NSArray *)arrayOfKeys ArrayOfKeyValues:(NSArray *)arrayOfKeyValues
{
// set the array counts
int arrayOfKeysCount = [arrayOfKeys count];
int arrayOfKeyValuesCount = [arrayOfKeyValues count];
// initialize the array to return
NSMutableArray *arrayFilteredResults = [[[NSMutableArray alloc] init] retain];
// qualify the search
if(arrayOfKeysCount == arrayOfKeyValuesCount && arrayOfKeysCount > 0 && arrayOfKeyValuesCount > 0)
{
// get the plist
NSString *fullFileName = [NSString stringWithFormat:#"%#.plist", plist];
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:fullFileName];
// put the plist records into an array
NSArray *arrayOfDictionaryItemsInPlist = [[NSMutableArray alloc] initWithContentsOfFile:path];
// load our dynamic array for mutability throughout the loops
NSMutableArray *arrayFiltered = [[[NSMutableArray alloc] initWithArray:arrayOfDictionaryItemsInPlist] retain];
// build an array of the final results to return
for(int i=0; i < arrayOfKeysCount; i ++)
{
// initialize this loops search criteria
NSString *key = [arrayOfKeys objectAtIndex:i];
id value = [arrayOfKeyValues objectAtIndex:i];
// set the filter
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"%K == %#", key, value];
// filter the result
arrayFilteredResults = [[[NSArray alloc] initWithArray:[arrayFiltered filteredArrayUsingPredicate:predicate]] retain];
}
} else {
NSLog(#"arrOfKeys count does not match arrayOfKeyValues count"); // the search did not qualify
}
// return the results
return arrayFilteredResults;
// release the allocated memory
[arrayFilteredResults release];
}
IndexController.m
NSArray *arrayOfKeys = [NSArray arrayWithObjects:
[NSString stringWithString:#"recordset"],
[NSString stringWithString:#"ep_object_attribute_id"],
nil];
NSArray *arrayOfKeyValues = [NSArray arrayWithObjects:
[NSString stringWithString:#"1778587279"],
[NSNumber numberWithInt:133],
nil];
NSMutableArray *arrayOfResult = [epFrameWork loadPlistAndFilterMultipleTimes:#"FormEntries" ArrayOfKeys:arrayOfKeys ArrayOfKeyValues:arrayOfKeyValues];
NSLog(#"arrayOfResult: %#", arrayOfResult);
Here's the link for fmdb. Should be quick to pick it up, be faster and you get a real database instead of simulating one with plists :)
https://github.com/ccgus/fmdb
Hope that helps