· 8 years ago · Apr 22, 2018, 08:36 PM
1//
2// PlaceViewController.m
3// TouristNov09
4//
5// Created by Marcio Valenzuela on 11/11/09.
6// Copyright 2009 Personal. All rights reserved.
7//
8
9#import "PlaceViewController.h"
10#import <sqlite3.h> // Import the SQLite database framework
11
12@implementation PlaceViewController
13@synthesize places;
14
15- (id)initWithStyle:(UITableViewStyle)style {
16 // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
17 if (self = [super initWithStyle:style]) {
18 }
19 return self;
20}
21
22- (void)viewDidLoad {
23 [super viewDidLoad];
24 self.title = @"Places";
25
26 // Setup some globals
27 databaseName = @"AnimalDatabase.sql";
28
29 // Get the path to the documents directory and append the databaseName
30 NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
31 NSString *documentsDir = [documentPaths objectAtIndex:0];
32 databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
33
34 // Execute the "checkAndCreateDatabase" function
35 [self checkAndCreateDatabase];
36
37 // Query the database for all places records and construct the "places" array
38 [self readPlacesFromDatabase];
39
40
41 // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
42 // self.navigationItem.rightBarButtonItem = self.editButtonItem;
43}
44
45-(void)checkAndCreateDatabase{
46 // Check if the SQL database has already been saved to the users phone, if not then copy it over
47 BOOL success;
48
49 // Create a FileManager object, we will use this to check the status
50 // of the database and to copy it over if required
51 NSFileManager *fileManager = [NSFileManager defaultManager];
52
53 // Check if the database has already been created in the users filesystem
54 success = [fileManager fileExistsAtPath:databasePath];
55
56 // If the database already exists then return without doing anything
57 if(success) return;
58
59 // If not then proceed to copy the database from the application to the users filesystem
60
61 // Get the path to the database in the application package
62 NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
63
64 // Copy the database from the package to the users filesystem
65 [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
66
67 [fileManager release];
68}
69
70-(void)readPlacesFromDatabase{
71 // Setup the database object
72 sqlite3 *database;
73
74 // Init the places Array
75 places = [[NSMutableArray alloc] init];
76
77 // Open the database from the users filessytem
78 if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
79 NSLog(@"db opens fine");
80
81 // Setup the SQL Statement and compile it for faster access
82 const char *sqlStatement = "select * from places";
83 sqlite3_stmt *compiledStatement;
84 NSLog(@"sets, compiles, databasePath=%s, sqlStatement=%s",[databasePath UTF8String],sqlStatement);
85
86 //prepare the SQL statement for execution
87 if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
88 NSLog(@"compiledStmnt should not be null %@",compiledStatement);
89
90 // Step thru SQL & Loop through the results and add them to the feeds array
91 while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
92 // Read the data from the result row
93 NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
94 NSString *aDescription = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
95 NSString *aCountry = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
96 NSString *aplaceImageURL = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
97 NSLog(@"aCountry is %@", aCountry);
98 // Create a new animal object with the data from the database
99 Place *placeObject = [[Place alloc] initWithplaceName:aName
100 placeCountry:aCountry
101 placeDescription:aDescription
102 placeImageURL:aplaceImageURL];
103
104 // Add the placeObject to the places Array
105 [places addObject:placeObject];
106
107 [placeObject release];
108 }
109 }
110 // Release the compiled statement from memory
111 sqlite3_finalize(compiledStatement);
112
113 }
114 sqlite3_close(database);
115}
116
117
118
119
120/*
121- (void)viewWillAppear:(BOOL)animated {
122 [super viewWillAppear:animated];
123}
124*/
125/*
126- (void)viewDidAppear:(BOOL)animated {
127 [super viewDidAppear:animated];
128}
129*/
130/*
131- (void)viewWillDisappear:(BOOL)animated {
132 [super viewWillDisappear:animated];
133}
134*/
135/*
136- (void)viewDidDisappear:(BOOL)animated {
137 [super viewDidDisappear:animated];
138}
139*/
140
141/*
142// Override to allow orientations other than the default portrait orientation.
143- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
144 // Return YES for supported orientations
145 return (interfaceOrientation == UIInterfaceOrientationPortrait);
146}
147*/
148
149- (void)didReceiveMemoryWarning {
150 // Releases the view if it doesn't have a superview.
151 [super didReceiveMemoryWarning];
152
153 // Release any cached data, images, etc that aren't in use.
154}
155
156- (void)viewDidUnload {
157 // Release any retained subviews of the main view.
158 // e.g. self.myOutlet = nil;
159}
160
161
162#pragma mark Table view methods
163
164- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
165 return 1;
166}
167
168
169// Customize the number of rows in the table view.
170- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
171 return [places count];
172}
173
174
175// Customize the appearance of table view cells.
176- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
177
178 static NSString *CellIdentifier = @"Cell";
179
180 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
181 if (cell == nil) {
182 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
183 }
184
185 // Set up the cell...
186 cell.textLabel.text = [places objectAtIndex:indexPath.row];
187 return cell;
188}
189
190
191- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
192 // Navigation logic may go here. Create and push another view controller.
193 //PlaceDetailViewController *anotherViewController = [[PlaceDetailViewController alloc] initWithNibName:@"PlaceDetailViewController" bundle:nil];
194 //PlaceDetailViewController.receivedPlaceName = [place objectAtIndex:indexPath.row];
195 //[self.navigationController pushViewController:anotherViewController];
196 //[anotherViewController release];
197}
198
199
200/*
201// Override to support conditional editing of the table view.
202- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
203 // Return NO if you do not want the specified item to be editable.
204 return YES;
205}
206*/
207
208
209/*
210// Override to support editing the table view.
211- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
212
213 if (editingStyle == UITableViewCellEditingStyleDelete) {
214 // Delete the row from the data source
215 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
216 }
217 else if (editingStyle == UITableViewCellEditingStyleInsert) {
218 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
219 }
220}
221*/
222
223
224/*
225// Override to support rearranging the table view.
226- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
227}
228*/
229
230
231/*
232// Override to support conditional rearranging of the table view.
233- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
234 // Return NO if you do not want the item to be re-orderable.
235 return YES;
236}
237*/
238
239
240- (void)dealloc {
241 [super dealloc];
242 [places release];
243}
244
245
246@end