Monday, December 23, 2013

iphone sqlite display the data to the page?

I built this two pages, page (1 page) there is a button to be able to click to another page can be connected (2 pages), write data in the 2nd page, save in sqlite, I would like to Home The displayed data stored in sqlite, how not show it, what data is displayed like to pay attention to it? I put the code page of stick over. I do not know where Q can upload compressed documents? Here is the code, please predecessors pointing.
 

# Import "SQL_DemoViewController.h"
# Import "User.h"
# Import "ResultViewcontroller.h"
@ Implementation SQL_DemoViewController

@ Synthesize resultViewController;
@ Synthesize addNavigationController;
@ Synthesize dataView;



- (NSString *) databasePath
{
NSArray * path = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString * pathname = [path objectAtIndex: 0];
return [pathname stringByAppendingPathComponent: DataFile];
}


- (Id) initWithDelegate: (id) delegate
{
if (self = [super init]) {
}
return self;
}


- (Void) loadView
{
[Super loadView];

self.view.backgroundColor = [UIColor whiteColor];
dataView = [[UITableView alloc] initWithFrame: CGRectMake (0.0, 0.0, 320.0, 400.0) style: UITableViewStylePlain];
self.dataView.delegate = self;
self.dataView.dataSource = self;

dataView.separatorColor = [UIColor lightGrayColor];

[Self.view addSubview: dataView];


UIBarButtonItem * addButton = [[[UIBarButtonItem alloc] initWithTitle: @ "new" style: UIBarButtonItemStyleBordered target: self action: @ selector (addView)] autorelease];
self.navigationItem.rightBarButtonItem = addButton;

}



- (NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) section {
return [self getToTalCount];

}


- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
{
static NSString * CellIdentifier = @ "CellIdentifier";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier] autorelease];
}
User * user = [self getUser: [indexPath row] +1];
cell.textLabel.text = [NSString stringWithFormat: @ "% @ |% @", user.id, user.detail];
return cell;
}




- (Void) addView
{
ResultViewcontroller * controller = self.resultViewController;
if (addNavigationController == nil) {
UINavigationController * navController = [[UINavigationController alloc] initWithRootViewController: controller];
self.addNavigationController = navController;
[NavController release];
}

ResultViewcontroller * resultViewcontroller = [[ResultViewcontroller alloc] initWithDelegate: self];
[Self.navigationController pushViewController: resultViewcontroller animated: YES];
}


- (User *) getUser: (int) id
{
if (sqlite3_open ([[self databasePath] UTF8String], & database)! = SQLITE_OK)
{
sqlite3_close (database);
NSAssert (0, @ "open database faild!");
return nil;
}
User * user = [[User alloc] init];
NSString * countSQl = [NSString stringWithFormat: @ "SELECT ROW, DETAIL FROM UsrTable WHERE ROW =% i", id];
sqlite3_stmt * statement;
if (sqlite3_prepare_v2 (database, [countSQl UTF8String], -1, & statement, nil) == SQLITE_OK)
{
while (sqlite3_step (statement) == SQLITE_ROW)
{
user.id = [NSString stringWithFormat: @ "% d", sqlite3_column_int (statement, 0)];
user.detail = [NSString stringWithFormat: @ "% s", sqlite3_column_text (statement, 0)];

}
/ / Sqlite3_finalize (statement);
return user;

}

return nil;
}

- (NSInteger) getToTalCount
{
if (sqlite3_open ([[self databasePath] UTF8String], & database)! = SQLITE_OK)
{
sqlite3_close (database);
NSAssert (0, @ "open database faild!");
return 0;
}
NSString * countSQL = @ "SELECT COUNT (*) FROM UsrTable";
sqlite3_stmt * statement;
if (sqlite3_prepare_v2 (database, [countSQL UTF8String], -1, & statement, nil) == SQLITE_OK)
{
while (sqlite3_step (statement) == SQLITE_OK)
{
int i = sqlite3_column_int (statement, 0);
/ / Sqlite3_finalize (statement);
return i;

}

}
return 0;

}

- (Void) dealloc {
[Super dealloc];

}

@ End
Reply:
My problem now is I showed in the investigation, or the problem select the database.
Reply:

Call this class table you want to call reloadData method, the method can be viewWillAppear[Your tableview object reloadData];
Reply:
I added reloadData but the problem persists, then I - (NSInteger) getToTalCount here return 0 removed after the data is stored can be displayed, but another problem has emerged. In the absence of data TableView unit Yigai displayed as NULL. How to solve.

Reply:
Debugging a look at the data does not come out
Reply:
Print what log, and see in the end there is wood there to get data

No comments:

Post a Comment