2015-08-25 100 views
-1

注意:我对C或IOS开发知之甚少。将大型sqlite数据库导入tableview并更新

我有一个小型的sqlite数据库(110Kb),大约有700条记录。我想知道将它们导入到Tableview中的最佳方法。 另外,当我单击一个单元格时,应用程序将移动到另一个ViewController,并使用Tableview中的数据根据​​是否使用登录或退出按钮来更新数据库文件。 到目前为止,我发现这个网站(我刚刚下载的项目),并用它来尝试和学习的东西: http://klanguedoc.hubpages.com/hub/IOS-5-How-To-Display-SQLite-Data-in-a-UITableView
任何帮助,将不胜感激:)代码.m文件中插入记录来实现代码如下:

// AuthorVC.m 


#import "AuthorVC.h" 
#import "Author.h" 
#import <sqlite3.h> 


@implementation AuthorVC 
@synthesize theauthors; 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [self authorList]; 
    [super viewDidLoad]; 

} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
    [super viewDidDisappear:animated]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 

    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    // Return the number of rows in the section. 
    return [self.theauthors count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"AuthorsCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    int rowCount = indexPath.row; 

    Author *author = [self.theauthors objectAtIndex:rowCount]; 
    cell.textLabel.text = author.name; 
    cell.detailTextLabel.text = author.title; 


    return cell; 
} 


-(NSMutableArray *) authorList{ 
    theauthors = [[NSMutableArray alloc] initWithCapacity:10]; 
    @try { 
     NSFileManager *fileMgr = [NSFileManager defaultManager]; 
     NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"AuthorsDb.sqlite"]; 
     BOOL success = [fileMgr fileExistsAtPath:dbPath]; 
     if(!success) 
     { 
      NSLog(@"Cannot locate database file '%@'.", dbPath); 
     } 
     if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK)) 
     { 
      NSLog(@"An error has occured: %s", sqlite3_errmsg(db)); 

     } 


     const char *sql = "SELECT * FROM books"; 
     sqlite3_stmt *sqlStatement; 
     if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK) 
     { 
      NSLog(@"Problem with prepare statement: %s", sqlite3_errmsg(db)); 
     }else{ 

      while (sqlite3_step(sqlStatement)==SQLITE_ROW) { 
       Author * author = [[Author alloc] init]; 
       author.name = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)]; 
       author.title = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,2)]; 
       author.genre = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 3)]; 
       [theauthors addObject:author]; 
      } 
     } 
     sqlite3_finalize(sqlStatement); 

    } 
    @catch (NSException *exception) { 
     NSLog(@"Problem with prepare statement: %s", sqlite3_errmsg(db)); 
    } 
    @finally { 
     sqlite3_close(db); 

     return theauthors; 
    } 
} 
@end 

回答

-1

说实话,这是相当多的工作,而不是一个简单的例子堆栈溢出答案。有两个地方你应该看看类似于你的要求的例子。

Apple CoreData recipes 这是非常接近你所需要的。阅读并参与他们提供的项目,以了解需要什么以及涉及多少工作。

另外: Ray Wenderlich CoreData Getting Started 这会给你一个更温柔的介绍CoreData和如何从一个视图转换到另一个。本教程是一步一步解释和适合任何级别的经验。

Ray还有一个关于如何与服务器交谈的很好的教程,这些教程可以帮助您了解如何通过api调用来更新数据库。

希望这会有所帮助。你必须潜入并弄脏你的双手去学习。

+0

感谢您的这一点,它可以节省大量的Google搜索:)。我所问的是可能的吗? –

+0

是的,只需粘贴一段代码就可以复制到这里,这是不公平的,特别是因为你不会学到任何东西。最好的学习方法是有一个可靠的方法。你知道你想要达到的目标,记下所需的步骤,然后解决如何完成这些步骤。这就是我们专业人员每天都在做的事情。 – latenitecoder