SQLite 表不存在

标签 sql objective-c sqlite ios4 database-connection

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

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

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

在 Release模式(模拟器)下,它不会报错,但不会读取数据库。
该代码的行为就像它可以连接到数据库,但不能读取 TblTest?

我已经重置了模拟器并使用了 clean and build。

抱歉,代码很长:

@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"];

}

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

相关文章:

mysql - 过去 12 个月的每月创建帐户

ios - 在 SLRequestHandler 中显示 UIAllertView 时系统挂起

android - 通过房间数据库的一对多关系出现问题

mysql - SQLite:连接两个表中的选定列

java - 如果不存在则插入,否则根据用户的选择更新 android

mysql - 尝试查找酒吧营业时间内发生的账单 - Mysql

sql - 删除具有重复值的行

mysql - 选择具有确切姓名和生日的所有客户

objective-c - 如何运行 WatchKit 应用程序

iphone - Objective-C 输出流刷新问题?