2010-04-23 92 views
1

我有一个需要数据库的应用程序。该应用程序在模拟器中运行良好。但是当我尝试将它部署到iPhone上时,它给了我'没有这种表动物'的错误。哪里有问题?我更好地理解使用我的iphone应用部署数据库比较困难

(void)viewDidLoad 
{ 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsPath = [paths objectAtIndex:0]; 
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"AnimalDatabase.sql"]; 


sqlite3 *database; 


if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) 
{ 

    const char *sqlStatement = "insert into animal (id, name) VALUES (?, ?)"; 
    sqlite3_stmt *compiledStatement; 
    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) 
    { 
     //NSLog(filePath); 
     sqlite3_bind_int(compiledStatement, 1, 12); 
     //NSLog(@"A"); 
     sqlite3_bind_text(compiledStatement, 2, [@"abc" UTF8String], -1, SQLITE_TRANSIENT);   
     //NSLog(@"B"); 
     if(sqlite3_step(compiledStatement) != SQLITE_DONE) 
     { 
      //NSLog(@"C"); 
      NSLog(@"Error: %s", sqlite3_errmsg(database)); 
     } 
     else 
     { 
      //NSLog(@"D"); 
      NSLog(@"Insert into row id = %d", sqlite3_last_insert_rowid(database)); 
     } 
    } 
    else 
    { 
     NSAssert1(0, @"Error while creating insert statement. '%s'", sqlite3_errmsg(database)); 
    } 

    sqlite3_finalize(compiledStatement); 
} 
else 
{ 
    NSLog(@"Error Occured"); 
} 
sqlite3_close(database); 
[super viewDidLoad]; 

}提供的代码

回答

2

我认为你需要看看什么被转移到你的iPhone。我遇到了这个问题,在实际的iPhone上创建或未创建数据库时存在一些奇怪的问题。我想你的情况正在发生的事情是,没有任何数据库被转移,然后你到

sqlite3_open 

实际创建数据库,直到你调用一个select语句则不会收到一个错误的呼叫。

查看您的文档,了解如何将db放置在资源中,以确保在构建时将其复制到iPhone。

1

最有可能的原因是AnimalDatabase.sql文件不存在,所以没有动物表插入。

+0

但我会不同意你的。因为如果动物数据库不存在,程序如何在模拟器上运行? – Joy 2010-04-23 07:47:41

+0

这可能是问题所在。从我记得的地方来看,模拟器如何处理文件的大小写敏感性与手机处理灵敏度的方式有所不同 – Liam 2010-04-23 08:12:50