我制作的应用基本上从麦克风获取数据并将其输入到sqlite3数据库中。我已经能够成功地将数据添加到数据库中,并且数据库会随每个条目一起更新,但出于某种原因,当我尝试在表视图控制器中显示它时,它说"数据库无法打开&#34 ; (见下面的代码)。所以在主视图控制器中数据库更新很好,显然数据库打开了,但是一旦我尝试在表视图控制器中打开数据库,我就会出错。
#import "TableViewController.h"
@interface TableViewController ()
@end
@implementation TableViewController
@synthesize entries;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
entries = [[NSMutableArray alloc] init];
[self openDB];
NSString *sql = [NSString stringWithFormat:@"SELECT * FROM PEFDATA"];
sqlite3_stmt *statement = NULL;
if(sqlite3_step(statement)==SQLITE_ROW) {
while (sqlite3_step(statement)==SQLITE_ROW) {
char *field1 = (char *) sqlite3_column_text(statement, 0);
NSString *field1Str = [[NSString alloc]initWithUTF8String:field1];
NSString *str = [[NSString alloc]initWithFormat:@"%@", field1Str];
[entries addObject:str ];
}
}
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//filePath to db
-(NSString *) filePath{
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentationDirectory, NSUserDomainMask, YES);
return [[paths objectAtIndex:0]stringByAppendingPathComponent:@"bp.sql"];
}
//open the db
-(void)openDB {
if (sqlite3_open([[self filePath] UTF8String], & dbb) != SQLITE_OK) {
sqlite3_close(dbb);
//NSAssert(0,@"Database failed to open");
NSLog(@"Database failed to open");
}
else{
NSLog(@"database opened");
}
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *myTitle = [[NSString alloc]initWithFormat:@"Peak Flow History"];
return myTitle;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return [entries count];
NSLog(@"%d",[entries count]);
}
-(IBAction)returned:(UIStoryboardSegue *)segue{
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"test");
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text = [self.entries objectAtIndex:indexPath.row];
return cell;
}
我希望能够做到这一点的真正原因是我可以将数据库(单个列)输出到一个数组中,这样我就可以绘制它,但我并不需要在表中查看数据形成。在我看过的在线教程中,他们将数据库输出到一个数组中,这样他们就可以在tableviewcontroller中显示数据。但这似乎是关于这样做的一种方式。因此,如果有人知道将单个列数据库输出到数组的方法,那将非常感激。
答案 0 :(得分:0)
在访问之前,您需要将数据库文件复制到文档目录。你做到了吗?在viewdidload中打开数据库后,您还没有关闭数据库。每次打开它都需要关闭数据库。
-(void) copyDatabaseIfNeeded
{
BOOL success;
// Create a FileManager object, we will use this to check the status
// of the database and to copy it over if required
NSFileManager *fileManager = [NSFileManager defaultManager];
// Check if the database has already been created in the users filesystem
success = [fileManager fileExistsAtPath:[self filePath]];
// Get the path to the database in the application package
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:strDatabaseName];
// If the database already exists then return without doing anything
if(success)
{
return;
}
// If not then proceed to copy the database from the application to the users filesystem
// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath:databasePathFromApp toPath:[self filePath] error:nil];
}
答案 1 :(得分:0)
你可以参考以下代码将单个列数据转换为数组这里logininfo是一个包含与数据库字段相关的属性和方法的类。
-(NSArray *)getLoginDetail
{
NSMutableArray *loginDetail = [[NSMutableArray alloc] init];
@try {
NSArray *path_array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path_row = [path_array objectAtIndex:0];
NSString *final_phone_path = [path_row stringByAppendingPathComponent:@"loginDatabase.sqlite"];
NSLog(@"%@",final_phone_path);
NSFileManager *myFile_manager = [NSFileManager defaultManager];
if (![myFile_manager fileExistsAtPath:final_phone_path])
{
NSLog(@"Fialed to find the database at path %@",final_phone_path);
//return nil;
}
if (sqlite3_open([final_phone_path UTF8String], &dataBAse)!= SQLITE_OK)
{
NSLog(@"An error occured opening the data base...");
//return nil;
}
//const char *sql_query = "SELECT id FROM page_data";
const char *sql = "SELECT NAME,PASSWORD FROM loginData";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(dataBAse, sql, -1, &statement, nil) != SQLITE_OK)
{
NSLog(@"Error failed to prepare statement...%s",sqlite3_errmsg(dataBAse));
//return nil;
}
while (sqlite3_step(statement)==SQLITE_ROW)
{
//int uniqueID=sqlite3_column_int(statement, 0);
const char *nameChar = (char *)sqlite3_column_text(statement,0);
const char *passwordChar = (char *)sqlite3_column_text(statement, 1);
NSLog(@"value from DB is : %@",[NSString stringWithUTF8String:nameChar]);
NSLog(@"value from DB is : %@",[NSString stringWithUTF8String:passwordChar]);
NSString *name1=[[NSString alloc]initWithUTF8String:nameChar];
NSString *password1=[[NSString alloc]initWithUTF8String:passwordChar];
// NSlog("%@",[NSString stringWithFormat:name1]);
loginInfo *info=[[loginInfo alloc]initWithName:name1 password:password1];
[loginDetail addObject:info];
//if(textFieldName.text==demo1 && textFieldPassword.text == demo2){
//[self buttonClicked];
//break;
}
if (sqlite3_finalize(statement) != SQLITE_OK)
{
NSLog(@"Failed to finalize data statement, normally error handling here.");
}
if (sqlite3_close(dataBAse) != SQLITE_OK)
{
NSLog(@"Failed to close database, normally error handling here.");
}
}
@catch (NSException *exception) {
NSLog(@"An exception occurred: %@", [exception reason]);
//return nil;
}
@finally {
}
return loginDetail;
}