我有一个NSMutableArray,我想用sqlite数据库填充并在表视图中显示数据。该应用程序构建并显示"数据库已打开"在控制台,但只是显示一个空的tableview,我有它完美的工作但突然似乎已停止工作。这是将数据添加到数组的代码:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize entries;
- (void)viewDidLoad
{
[super viewDidLoad];
//[entries addObject:@"object1"];
//[entries addObject:@"object2"];
//[entries addObject:@"object3"];
//[entries addObject:@"object4"];
[self openDB];
entries = [[NSMutableArray alloc] init];
NSString *sql = [NSString stringWithFormat:@"SELECT * FROM speakers ORDER BY name"];
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil)==SQLITE_OK)
{
while (sqlite3_step(statement)==SQLITE_ROW) {
char *field2 = (char *) sqlite3_column_text(statement,1);
NSString *field2Str = [[NSString alloc]initWithUTF8String:field2];
NSString *str = [[NSString alloc]initWithFormat:@"%@", field2Str];
[entries addObject:str ];
}
}
}
//file path to db
-(NSString *) filePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
return [[paths objectAtIndex:0] stringByAppendingPathComponent:@"speakerdb.sqlite"];
}
//open the db
-(void)openDB {
if (sqlite3_open([[self filePath] UTF8String], &db) != SQLITE_OK){
sqlite3_close(db);
NSAssert(0,@"error preparing statement: %s", sqlite3_errmsg(db));
}else{
NSLog(@"database opened");
}
}
NSMutableArray在头文件中定义如下:
#import <UIKit/UIKit.h>
#import "sqlite3.h"
#import "speakerViewController.h"
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>{
IBOutlet UITableView *theTable;
sqlite3 *db;
}
@property (nonatomic,retain) NSMutableArray *entries;
@end
以下是将数据添加到表视图的代码:
-(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 [entries count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//Congigure the cell...
cell.textLabel.text = [entries objectAtIndex:indexPath.row];
return cell;
}
数据显示在表视图中是否手动将对象添加到数组中(注释掉了viewDidLoad方法中显示的行)但似乎无法使用sqlite数据库。
非常感谢任何帮助。
编辑:我正在使用xcode 5.1.1
答案 0 :(得分:1)
尝试调试代码...在NSString *field2Str = [[NSString alloc]initWithUTF8String:field2];
行放置一个断点,并检查field2是否为空......
尝试像这样打开db:
NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:@"speakerdb"
ofType:@"sqlite"];
if (sqlite3_open([sqLiteDb UTF8String], &_database) != SQLITE_OK) {
NSLog(@"Failed to open database!");
}
您的代码中缺少一件事:
sqlite3_finalize(statement);
像这样使用它:
if(sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil)==SQLITE_OK)
{
while (sqlite3_step(statement)==SQLITE_ROW) {
char *field2 = (char *) sqlite3_column_text(statement,1);
NSString *field2Str = [[NSString alloc]initWithUTF8String:field2];
NSString *str = [[NSString alloc]initWithFormat:@"%@", field2Str];
[entries addObject:str ];
}
sqlite3_finalize(statement);
}