Followers

Thursday, August 26, 2010

Sqllite

appdelegate

=============================================
- (void) copyDatabaseIfNeeded {

//Using NSFileManager we can perform many file system operations.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];

if(!success) {

NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"example.sql"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

if (!success)
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
else {
NSLog(@"open");
}

}

- (NSString *) getDBPath {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"example.sql"];
}
=====================================================

view control

========================================================

static sqlite3 *database = nil;
#import


appDelegate = (CompassDictionaryAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate copyDatabaseIfNeeded];



- (IBAction)MyFavoritesAction:(id)sender {
appDelegate.favorite = [NSMutableArray new];
[self SelectDatabase:[appDelegate getDBPath]];

MyFavorites *myfav = [[MyFavorites alloc] initWithNibName:@"MyFavorites" bundle:nil];
[self presentModalViewController:myfav animated:NO];
[myfav release];
}
------------------

-(void)SelectDatabase:(NSString*)DBPath{
appDelegate = (CompassDictionaryAppDelegate *)[[UIApplication sharedApplication] delegate];

if (sqlite3_open([DBPath UTF8String], &database) == SQLITE_OK) {

const char *sql = "select fav_title,fav_text from fav_list order by fav_title";
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {

while(sqlite3_step(selectstmt) == SQLITE_ROW) {
FavoriteData *data=[[FavoriteData alloc] init];
//sqlite3_bind_text(addStmt, 1, [data.name UTF8String], -1, SQLITE_TRANSIENT);
NSString *coffeeObj = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
// NSLog(@"name man ->%@",coffeeObj);
data.description = coffeeObj;
NSString *coffeeObj1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
//NSLog(@"name man ->%@",coffeeObj1);
data.name = coffeeObj1;
//NSLog(@"favourite_id---->%i",(NSInteger)sqlite3_column_int(selectstmt, 0));
//NSLog(@"search_list_id-->%i",(NSInteger)sqlite3_column_int(selectstmt, 1));
//NSLog(@"%i",coffeeObj.userid);
[appDelegate.favorite addObject:data];
}
}
sqlite3_finalize(selectstmt);
}
NSLog(@"close database");
sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.

//NSLog(@"favourite_id---->%i",(NSInteger)sqlite3_column_int(selectstmt2, 0));
//NSLog(@"search_list_id-->%i",(NSInteger)sqlite3_column_int(selectstmt2, 1));
}

Parse xml

- (ParseXml *) initparserxml:(AdMeAppDelegate *) appDelegate1 {
[super init];
appDelegate = (AdMeAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate = appDelegate1;
NSLog(@"inside parse XML in parse XML");
return self;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {

if([elementName isEqualToString:@"adme"]) {
//Initialize the array.
appDelegate.listData = [[NSMutableArray alloc] init];
} else if([elementName isEqualToString:@"campaign"]) {

//Initialize the campaign.
adetails = [[XmlData alloc] init];

//Extract the attribute here.
adetails.campaign_id = [[attributeDict objectForKey:@"campaign_id"] stringValue];

NSLog(@"Reading id value :%i", adetails.campaign_id);
}

NSLog(@"Processing Element: %@", elementName);
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

currentElementValue = [[NSMutableString alloc] initWithString:string];

/*if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
*/
NSLog(@"Processing Value: %@", currentElementValue);

}


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

if([elementName isEqualToString:@"adme"])
{

if([appDelegate.listData count] == 0) {
appDelegate.listData = [[NSMutableArray alloc] init];
adetails = [[XmlData alloc] init];
adetails.campaign_name = @"No campaign in list";
[appDelegate.listData addObject:adetails];
[adetails release];
adetails = nil;
}
return;
}


//There is nothing to do if we encounter the Books element here.
//If we encounter the Book element howevere, we want to add the campaign object to the array
// and release the object.
if([elementName isEqualToString:@"campaign"]) {
[appDelegate.listData addObject:adetails];
[adetails release];
adetails = nil;
}
else
[adetails setValue:currentElementValue forKey:elementName];

[currentElementValue release];
currentElementValue = nil;
}
------------------------------------------------------------------------------

view control
------------------------------------------------------------------------------

- (void)loadView {
}
*/

- (void)parseXML:(NSData *)data {
NSLog(@"inside parse XML in test list");
NSAttributedString *output = [NSString stringWithCString:[data bytes] length:[data length]];
NSString *mystr = [NSString localizedStringWithFormat:@"%@data:",output];
NSLog(@"data is %@",mystr);
printf("HTTP RESPONSE : - %s\n",&mystr);

NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data];
ParseXml *parser = [[ParseXml alloc] initparserxml:appDelegate];
[xmlParser setDelegate:parser];
[xmlParser setShouldProcessNamespaces:NO];
[xmlParser setShouldReportNamespacePrefixes:NO];
[xmlParser setShouldResolveExternalEntities:NO];


BOOL success = [xmlParser parse];
if(success)
NSLog(@"no errors");
else
NSLog(@"error !!!");
}


- (void)getListView {
NSData *data;
NSLog(@"Inside getList view");
NSString *listview_url = [NSString stringWithFormat:@"%@%@",appDelegate.HTTPS_BASE_URL,appDelegate.XML_VIEW_URL];
NSURL *listurl = [NSURL URLWithString:listview_url];
NSHTTPURLResponse *response;
NSError *error;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:listurl];
[request setHTTPMethod:@"GET"];
[request setHTTPShouldHandleCookies:YES];
request = [NSMutableURLRequest requestWithURL:listurl];


data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if([response statusCode] == 200) {
[self parseXML:data];
//UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning !" message:@"Got 200 status" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
//[alert show];

} else {
//UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning !" message:@"Error" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
//[alert show];
}
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
inc = 0.0;
appDelegate = (AdMeAppDelegate *) [[UIApplication sharedApplication] delegate];
[self getListView];
tableView.backgroundColor = [UIColor blackColor];
[super viewDidLoad];
}