2011-05-10 60 views
2

编辑:已解决!回答下面的第一个代码框SQLite表不存在

当我尝试连接一个sqlite数据库(与表TblTest)。在我的应用程序调试模式,错误的是:

*在断言失败 - [QueryClass getTestData:], *终止应用程序由于未捕获的异常“NSInternalInconsistencyException”,理由是:“广东话建立SQL读取项目[没有这样的表格:TblTest]'

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

我已经重置了模拟器并使用了clean和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"]; 

} 
+0

“没有这样的表:TBLTEST”意味着表不存在于数据库中。要么你有一个旧的数据库,你需要先删除应用程序,或者你拼错了表名,或者你从来没有创建过这样的表。你有没有检查拼写,也许它是tblTest或什么? – vakio 2011-05-10 13:23:57

回答

1

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

下面是我从你的问题中提取的答案。随时接受它。


我使用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"]; 

} 
+0

免费积分为你;) – Oritm 2011-05-11 07:27:55