I am having trouble linking my XCode Project with my SQLite3 database - database

#import "AddEandM.h"
#import "Questions.h"
#import "sqlite3.h"
#interface AddEandM ()
#end
#implementation AddEandM
#synthesize Topic, Question, Answer1, Answer2, Answer3, CorrectAnswer, status;
-(IBAction) backgroundTouched:(id)sender {
[Topic resignFirstResponder];
[Question resignFirstResponder];
[Answer1 resignFirstResponder];
[Answer2 resignFirstResponder];
[Answer3 resignFirstResponder];
[CorrectAnswer resignFirstResponder];
}
-(IBAction) textfieldReturn:(id)sender {
[sender resignFirstResponder];
}
- (void) saveData
{
sqlite3 * Questions;
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &Questions) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:
#"INSERT INTO DATABASE (Topic, Question, Answer1, Answer2, Answer3, CorrectAnswer) VALUES (\"%#\", \"%#\", \"%#\", \"%#\", \"%#\", \"%#\")",Topic.text, Question.text, Answer1.text, Answer2.text, Answer3.text, CorrectAnswer.text];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2 (Questions, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
status.text = #"Question/Answers Added";
Topic.text = #" ";
Question.text = #" ";
Answer1.text = #" ";
Answer2.text = #" ";
Answer3.text = #" ";
CorrectAnswer.text = #" ";
} else {
status.text = #"Failed to add Question/Answers";
}
sqlite3_finalize(statement);
sqlite3_close(Questions);
}
}
-(IBAction)Back:(id)sender {
Questions *second = [[Questions alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
NSString *docsDir;
NSArray *dirPaths;
sqlite3 * Questions;
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex: 0];
databasePath = [[NSString alloc]
initWithString: [docsDir stringByAppendingPathComponent:
#"Questions"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath:databasePath ] == NO)
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &Questions) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt = "Create Table If Not Exists Questions (QuestionNo Integer AutoIncrement, Question Text, Answer1 Text, Answer2 Text, Answer 3 Text, Correct Answer Text)";
if (sqlite3_exec(Questions, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
status.text = #"Failed to Create Table";
}
sqlite3_close(Questions);
} else {
status.text =#"Failed to open/create database";
}
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I am creating a revision application for my computer science class, I have very limited knowledge in regards to programming in Xcode/Ob C, can anyone assist me and tell me what is going wrong here? My database is named "Questions" and contains 2 tables: "EasyandMedium" as well as "Hard", however, I cannot get my program to save data to this database - Please Help!

What exactly is the problem you are having? Do you know if your fileExistsAtPath call succeeds? Does the sqlite3_open call succeed?
There are lots of examples for using SQLite3 in Xcode such as I am having trouble linking my XCode Project with my SQLite3 database. I would suggest checking one out as they make a great starting point.

Related

value is not called in other method though it is defined globally in ios6

I am posting the code which I tried.I am getting string value and then I am adding it to an array and getting the array value too.But problem comes when I try to use the array value in other method ,it's value is blank.
//ViewController.h
#interface ViewController : UIViewController{
NSString *stringValueVideoiD;
NSMutableArray *TableVideoIDArray;
}
//ViewController.m
#implementation ViewController
- (void)viewDidLoad
{
TableVideoIDArray=[[NSMutableArray alloc]init];
[self getAllRows];
}
-(void) getAllRows{
if(sqlite3_open([databasePath UTF8String], &db) == SQLITE_OK)
{
NSString *sqlStatement = [NSString stringWithFormat:#"SELECT * FROM table"];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(db,[sqlStatement UTF8String] , -1, &compiledStatement, NULL)== SQLITE_OK)
{
while(sqlite3_step(compiledStatement)==SQLITE_ROW)
{
char *videoId = (char *) sqlite3_column_text(compiledStatement, 1);
stringValueVideoiD = [[NSString alloc] initWithUTF8String:videoId];
TableVideoIDArray=[[NSMutableArray alloc]init];
[TableVideoIDArray addObject:stringValueVideoiD];
NSLog(#"vids array:%#",TableVideoIDArray);
//vids aray:value is printed
}
}
}
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell==nil) {
cell=[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSLog(#"here:%#",TableVideoIDArray);
//here comes the problem.My console result is
2013-09-18
11:18:24.431 TubeAlert[468:11303] here:(
)
//so guys I need your help why this is happenning or I am making mistakes in my code.
TableVideoIDArray allocated two times.
In viewDidLoad, use
TableVideoIDArray=[NSMutableArray array];
don't allocate with in a While loop.

Adding values stored in NSString in NSMutableArray for ios

I am posting the code which I tried.I am getting string value,but when adding all the values of nsstring to nsmutablearray and printing it ,I get null value.
//ViewController.h
#interface ViewController : UIViewController{
NSString *stringValueVideoiD;
NSMutableArray *TableVideoIDArray;
}
//ViewController.m
#implementation ViewController
- (void)viewDidLoad
{
TableVideoIDArray=[[NSMutableArray alloc]init];
}
-(void) getAllRows{
if(sqlite3_open([databasePath UTF8String], &db) == SQLITE_OK)
{
NSString *sqlStatement = [NSString stringWithFormat:#"SELECT * FROM table"];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(db,[sqlStatement UTF8String] , -1, &compiledStatement, NULL)== SQLITE_OK)
{
while(sqlite3_step(compiledStatement)==SQLITE_ROW)
{
char *videoId = (char *) sqlite3_column_text(compiledStatement, 1);
stringValueVideoiD = [[NSString alloc] initWithUTF8String:videoId];
[TableVideoIDArray addObject:stringValueVideoiD];
NSLog(#"vids id:%#",TableVideoIDArray);
//vids id:(null) is printed
}
}
}
}
Check if stringValueVideoiD has some value. Try to print it before addObject. Check you sql statement if it is returning any value.

UITapGestureRecognizer work by default on UISwipeGestureRecognizer

In my code by default work UITapGesture how to remove this
NSString *dirString;
host is class object
-(Void) handleSwipeEvent : (UISwipeGestureRecognizer *) recognizer {
//NSString *swipeDirection = NULL;
//CGPoint location = [recognizer locationInView:self];
dirString = NULL;
UITapGestureRecognizer *tap=nil;
tap.enabled=NO;
if ([recognizer direction] == UISwipeGestureRecognizerDirectionLeft) {
dirString = kCATransitionFromRight;
}
if ([recognizer direction] == UISwipeGestureRecognizerDirectionRight)
{
dirString = kCATransitionFromLeft;
}
if (dirString) [host swipeTo:dirString];
}
-(void)createSwipeGestureRecognizer:(UIView *)view direction: (UISwipeGestureRecognizerDirection)direction
{
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeEvent:)];
[view addGestureRecognizer:recognizer];
[recognizer setDirection:direction];
[self setSwipeRecognizer:recognizer];
[recognizer release];
}
- (void) setHost: (FlashCardViewController *) aHost {
host = aHost;
[self createSwipeGestureRecognizer:self direction:UISwipeGestureRecognizerDirectionLeft];
[self createSwipeGestureRecognizer:self direction:UISwipeGestureRecognizerDirectionRight];
}
On the view i have to touch then load next contant.
How can i disable tap event

by Adding Polyline(path) on MKMapview crashing the app in ios6

I have a requirement to adding a path in my MKMapview between two annotation pin. There is no issue with ios 5 or older but when i try to run app in ios 6 app is surprisingly quit. below is my code. If there is any correction in my code then please suggest me.
1.So my question is suggest the best way how to decrees memory in my following code so that i can solve crash in my Application.
- (void)viewDidLoad
{
NVPolylineAnnotation *annotation = [[NVPolylineAnnotation alloc] initWithPoints:pathArray mapView:_mapView];
[_mapView addAnnotation:annotation];
[annotation release];
[pathArray removeAllObjects];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
NSLog(#"%s",__FUNCTION__);
if ([annotation isKindOfClass:[NVPolylineAnnotation class]])
{
NSLog(#"=========ANOTATION=========NVPolylineAnnotationView START");
//ann=[ann initWithAnnotation:annotation mapView:_mapView];
NVPolylineAnnotationView *ann=[[NVPolylineAnnotationView alloc] init];
return [[ann initWithAnnotation:annotation mapView:_mapView] autorelease];//[[[NVPolylineAnnotationView alloc] initWithAnnotation:annotation mapView:_mapView] autorelease];
}
else if([annotation isKindOfClass:[MapViewAnnotation class]])
{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"pointers"] ;
// annView.rightCalloutAccessoryView = [UIButton buttonWithType: UIButtonTypeDetailDisclosure];
annView.animatesDrop=NO;
annView.canShowCallout = TRUE;
return [annView autorelease];
}
else if([annotation isKindOfClass:[PlacePin class]])
{
{
MKPinAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = #"com.invasivecode.pin";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinView.pinColor = MKPinAnnotationColorGreen;
pinView.canShowCallout = YES;
pinView.animatesDrop = NO;
}
else {
[mapView.userLocation setTitle:#"I am here"];
}
return pinView;
}
}
return nil;
}
- (void) mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
NSLog(#"%s",__FUNCTION__);
// fixes that some marker are behind the polyline
for (int i=0; i<[views count]; i++)
{
MKAnnotationView *view = [views objectAtIndex:i];
if ([view isKindOfClass:[NVPolylineAnnotationView class]])
{
[[view superview] sendSubviewToBack:view];
/* In iOS version above 4.0 we need to update the polyline view after it
has been added to the mapview and it ready to be displayed. */
NSString *reqSysVer = #"4.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
{
[self updatePolylineAnnotationView];
}
}
}
NSLog(#"----------didAddAnnotationViews");
}
- (void)updatePolylineAnnotationView
{
NSLog(#"%s",__FUNCTION__);
MKAnnotationView *annotationView = [views objectAtIndex:0];
id <MKAnnotation> mp = [annotationView annotation];
[mapView selectAnnotation:mp animated:NO];
*/
for (NSObject *a in [_mapView annotations])
{
if ([a isKindOfClass:[NVPolylineAnnotation class]])
{
NVPolylineAnnotation *polyline = (NVPolylineAnnotation *)a;
NSObject *pv = (NSObject *)[_mapView viewForAnnotation:polyline];
if ([pv isKindOfClass:[NVPolylineAnnotationView class]])
{
NVPolylineAnnotationView *polylineView =
(NVPolylineAnnotationView *)[_mapView viewForAnnotation:polyline];
[polylineView regionChanged];
}
}
}
}

Search bar and search display controller with segue

Never called when a search table cell is tapped
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"Show Detail"])
{
Player *player = [self.fetchedResultsController objectAtIndexPath:[self.tableView indexPathForSelectedRow]];
[segue.destinationViewController setPlayer:player];
}
}
This filters the list correctly, but the prepareForSegue is never called when a search table cell is tapped.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Player Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (tableView == self.tableView)
{
// normal table view population
Player *player = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [[NSString alloc] initWithFormat:#"#%# %# %#", player.number, player.firstName, player.lastName];
cell.detailTextLabel.text = [[NSString alloc] initWithFormat:#"%#", player.position];
[cell.imageView setImageWithURL:[NSURL URLWithString:player.photo] placeholderImage:[UIImage imageNamed:#"playerplaceholder.jpg"]];
}
else if(tableView == self.searchDisplayController.searchResultsTableView)
{
// search view population
Player *player = [self.filteredFetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [[NSString alloc] initWithFormat:#"#%# %# %#", player.number, player.firstName, player.lastName];
cell.detailTextLabel.text = [[NSString alloc] initWithFormat:#"%#", player.position];
[cell.imageView setImageWithURL:[NSURL URLWithString:player.photo] placeholderImage:[UIImage imageNamed:#"playerplaceholder.jpg"]];
}
return cell;
}
you need to put IF that check if the search have results.
like in this code:
if ([[segue identifier]isEqualToString:#"ShowDetails"])
{
ShowDetailsViewController *sdvc = (ShowDetailsViewController *)[segue destinationViewController];
NSIndexPath *indexPath = [[self tableView] indexPathForSelectedRow];
Books *selectedBooks = nil;
if(self.searchDisplayController.active)
selectedBooks = (Books *)[[self searchResults]objectAtIndex:[[[[self searchDisplayController]searchResultsTableView]indexPathForSelectedRow]row]];
else
selectedBooks = (Books *)[[self fetchResultsController] objectAtIndexPath:indexPath];
sdvc.currentBooks = selectedBooks;
}

Resources