编辑:解决了!在第一个密码框下方回答

当我尝试连接sqlite数据库(使用表TblTest)时。在我的应用程序的调试模式下,错误为:

*-[QueryClass getTestData:]中的断言失败,
*由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“无法构建SQL来读取项目[没有此类表:TblTest]”

在发布模式(模拟器)下,它不会给出错误,但不会读取数据库。
该代码的行为就像可以连接到数据库,但无法读取TblTest吗?

我已经重置了模拟器并使用了清理和构建。

对不起,长代码:

@implementation QueryClass
@synthesize databaseName;
@synthesize databaseLite;

-(id)initWithDatabase:(NSString *)databaseNameP{
    if(self = [super init]){
        [self createEditableCopyOfDatabaseIfNeeded];
        self.databaseName = databaseNameP;
        self.databaseLite = [self getNewDBConnection];
    }
    return self;
}

- (void)createEditableCopyOfDatabaseIfNeeded {
    NSLog(@"Creating editable copy of database");
    // First, test for existence.
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:self.databaseName];
    success = [fileManager fileExistsAtPath:writableDBPath];
    if (success) return;
    // The writable database does not exist, so copy the default to the appropriate location.
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:self.databaseName];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    if (!success) {
        NSAssert1(0, @"Failed to create writable database file with message ‘%@’.", [error localizedDescription]);
    }
}

- (sqlite3 *) getNewDBConnection{
    sqlite3 *newDBconnection;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:self.databaseName];
    // Open the database. The database was prepared outside the application.
    if (sqlite3_open([path UTF8String], &newDBconnection) == SQLITE_OK) {

        NSLog(@"Database Successfully Opened  ");

    } else {
        NSLog(@"Error in opening database  ");
    }

    return newDBconnection;
}

-(NSMutableArray *)getTestData:(NSInteger) testId{
    (NSMutableArray *testArray = [[NSMutableArray alloc]init];

    int ret;
    sqlite3_stmt *selStmt = nil;
    const char *sql = "SELECT testId, name, FROM TblTest WHERE testId = ?";

    if (!selStmt)
    { // build update statement
        if (sqlite3_prepare_v2(self.databaseLite, sql, -1, &selStmt, NULL)!=SQLITE_OK)
        {
            selStmt = nil;
        }
    }

    sqlite3_bind_int(selStmt, 1, testId);

    if(!selStmt){
        NSAssert1(0, @"Cant build SQL to read item [%s]", sqlite3_errmsg(self.databaseLite));
    }

    while ((ret=sqlite3_step(selStmt))==SQLITE_ROW)
    { // get the fields from the record set and assign to item

        Test *test = [[Test alloc]init];

        NSNumber *testId = [NSNumber numberWithInt: (int) sqlite3_column_int(selStmt, 0)];
        NSString *name = [NSString stringWithUTF8String:(char *) sqlite3_column_text(selStmt, 1)];


        test.testId =testId;
        test.name   =name;


        [testArray addObject:pill];
        [test release];
    }
    sqlite3_reset(selStmt);

    return [testArray autorelease];
}



//in my function i do:
qc = [[QueryClass alloc]initWithDatabase:@"databaseLite.sql"];
[qc getTestData:0];




我使用this教程重写了代码,它的工作原理很吸引人。 :)

谢谢

-(id)initWithDatabase:(NSString *)databaseNameP{
    if(self = [super init]){
        [self createEditableCopyOfDatabaseIfNeeded];
        [self initializeDatabase];
    }
    return self;
}

// Creates a writable copy of the bundled default database in the application Documents directory.
- (void)createEditableCopyOfDatabaseIfNeeded {
    // First, test for existence.
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"databaseLite4.sqlite"];
    success = [fileManager fileExistsAtPath:writableDBPath];
    if (success) return;
    // The writable database does not exist, so copy the default to the appropriate location.
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"databaseLite4.sqlite"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    if (!success) {
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
}

// Open the database connection and retrieve minimal information for all objects.
- (void)initializeDatabase {

    // The database is stored in the application bundle.
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    self.path = [documentsDirectory stringByAppendingPathComponent:@"databaseLite4.sqlite"];

}

最佳答案

解决自己的问题时,应提供解决方案作为答案,而不是更新问题。可以(在这种情况下建议)回答您自己的问题并将其接受为答案。您不会因为接受自己的答案而获得任何声誉,但是该问题将正确列出为已解决。

以下是我从您的问题中提取的答案。随时接受。



我使用this教程重写了代码,它的工作原理很吸引人。 :)

这是我的解决方案:

-(id)initWithDatabase:(NSString *)databaseNameP{
    if(self = [super init]){
        [self createEditableCopyOfDatabaseIfNeeded];
        [self initializeDatabase];
    }
    return self;
}

// Creates a writable copy of the bundled default database in the application Documents directory.
- (void)createEditableCopyOfDatabaseIfNeeded {
    // First, test for existence.
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"databaseLite4.sqlite"];
    success = [fileManager fileExistsAtPath:writableDBPath];
    if (success) return;
    // The writable database does not exist, so copy the default to the appropriate location.
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"databaseLite4.sqlite"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    if (!success) {
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
}

// Open the database connection and retrieve minimal information for all objects.
- (void)initializeDatabase {

    // The database is stored in the application bundle.
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    self.path = [documentsDirectory stringByAppendingPathComponent:@"databaseLite4.sqlite"];

}

关于sql - SQLite表不存在,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5950189/

10-11 05:49