使用我的iphone应用程序部署数据库很困难

时间:2010-04-23 07:32:47

标签: iphone objective-c xcode

我有一个需要数据库的应用程序。应用程序在模拟器中运行良好。但是,当我尝试将其部署到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 个答案:

答案 0 :(得分:2)

我认为您需要查看正在转移到iPhone上的内容。我遇到了这个问题,关于如何在实际的iPhone上创建或不创建数据库存在一些奇怪的问题。我认为你的案例中发生的事情是没有数据库被转移,然后你的呼叫

sqlite3_open

实际上是在创建数据库,因此在调用select语句之前不会收到错误。

检查文档中有关将数据库放在资源中的位置,以确保在构建时将其复制到iPhone。

答案 1 :(得分:1)

最可能的原因是AnimalDatabase.sql文件不存在,因此没有要插入的Animal表。