【问题标题】:ios-issue with sqlite-database path [closed]sqlite数据库路径的ios问题[关闭]
【发布时间】:2013-01-22 13:28:37
【问题描述】:

这是我的代码: 它适用于其他方法,但仅在此处创建问题。 在这里我无法插入数据... 这里有什么问题?代码之前完美运行。

-(void) insertleadership {
[self trunleadership];
// Setup the database object
sqlite3 *database;
NSLog(@"Inside leadership");
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
    // Setup the SQL Statement and compile it for faster access
    // NSString *query=[stringWithFormat: @"select name,password from customer where name=%@ and password=%@",*login_name,*login_pass];
    sqlite3_stmt *compiledStatement;
    for(int i=0;i<ltitle.count;i++)
    {
        // NSString *insertSQL = [NSString stringWithFormat:@"insert into category values(\'aa\',\'bb\',\'cc\',\'cc\',\'cc\')"];
        // NSLog(@"Inside leadership");
        NSString *insertSQL = [NSString stringWithFormat:@"insert into leadership values(\'%@\',\'%@\',\'%@\',\'%@\',\'%@\',\'%@\',\'%@\',\'%@\',\'%@\',\'%@\',\'%@\',\'%@\',\'%@\')",[ltitle objectAtIndex:i],[ldate objectAtIndex:i],[lfunction objectAtIndex:i],[lservice objectAtIndex:i],[lsubservice objectAtIndex:i],[labstract objectAtIndex:i],[ldoctype objectAtIndex:i],[lcreated objectAtIndex:i],[lcorridor objectAtIndex:i],[lfeature objectAtIndex:i],[lindiastory objectAtIndex:i],[lpublish objectAtIndex:i],[ltopic objectAtIndex:i]];
        // NSLog(@"SQL Query for leader:%@",insertSQL);
        const char *insert_stmt = [insertSQL UTF8String];
        // sqlite3_prepare_v2(databaseName, insert_stmt, -1, &statement, NULL);
        sqlite3_prepare_v2(database, insert_stmt, -1, &compiledStatement, NULL);
        if(sqlite3_step(compiledStatement) == SQLITE_DONE){
            NSLog(@"Insert into leadership successful");
        }
        else {
            NSLog(@"Unable to insert in leadership");
        }
        // Release the compiled statement from memory
        sqlite3_finalize(compiledStatement);
    }
    sqlite3_close(database);
   }
}

【问题讨论】:

  • 几乎可以肯定,您的“databasePath”指的是捆绑包中的数据库。该捆绑包是只读的,您必须在使用前将数据库文件复制到读/写空间。

标签: iphone cocoa-touch xcode4.3


【解决方案1】:

如果您在代码中添加一些报告,sqlite 本身会告诉您为什么它没有打开:

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
    // Do something
    splite3_close(database);
}
else
{
    NSLog(@"Failed to open database '%@': %s", databasePath, sqlite3_errmsg(database));
}

@HotLicks 也是正确的,因为您应该在访问它之前将数据库从应用程序包中复制出来,除非您以只读方式打开它(通过使用 sqlite3_open_v2() 并传入 SQLITE_OPEN_READONLY 作为标志之一)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    • 2021-08-13
    • 2013-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-06
    相关资源
    最近更新 更多