2

使用MacOS终端我创建了一个数据库名称数据库。sql并插入了一些记录。使用iOS我可以检索插入的值。

但是使用iOS代码我尝试将记录插入数据库并且它没有在数据库中输入记录。

我应该设置需要设置一些权限吗?这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self openDB];
}

-(IBAction)save:(id)sender{

    [self insertRecordIntoTableNamed:@"Contacts" 
                         field1Value:fname.text
                         field2Value:lname.text
                         field3Value:comp.text 
                         field4Value:email.text 
                         field5Value:pnumber.text 
                         field6Value:mnumber.text 
                         field7Value:add.text 
                         field8Value:city.text 
                         field9Value:state.text];
}


-(void)openDB{
    NSString *sqlfile=[[NSBundle mainBundle]pathForResource:@"database" ofType:@"sql"];

    if(sqlite3_open([sqlfile UTF8String], &db)!= SQLITE_OK){
        sqlite3_close(db);
        NSLog(@"Database connected");
        NSAssert(0,@"Database failed to open");
    }
    else
    {
        NSLog(@"Database connected");
    }
}


-(void) insertRecordIntoTableNamed:(NSString *) tableName
                       field1Value:(NSString *) field1Value
                       field2Value:(NSString *) field2Value
                       field3Value:(NSString *) field3Value
                       field4Value:(NSString *) field4Value
                       field5Value:(NSString *) field5Value
                       field6Value:(NSString *) field6Value
                       field7Value:(NSString *) field7Value
                       field8Value:(NSString *) field8Value
                       field9Value:(NSString *) field9Value {

    NSString *sql = [NSString stringWithFormat:@"INSERT INTO %@ VALUES ('%@','%@','%@','%@',%@,%@,'%@','%@','%@');",tableName, field1Value, field2Value,field3Value,field4Value,field5Value,field6Value,field7Value ,field8Value,field9Value];


    NSLog(@"%@",sql);

    // char *err;
    sqlite3_stmt *statement;
    if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil)== SQLITE_OK)
    {
        if (SQLITE_DONE!=sqlite3_step(statement))
        {
            sqlite3_close(db);
            NSAssert(0, @"Error updating table.");
        }
        else{
            NSLog(@"Success");
            NSLog(@"%@",sql);
        }
    }
    sqlite3_finalize(statement);
}

@end

这是我所做的:

  1. 使用 CHMOD 更改文件夹和路径权限

  2. 拆分sqlite3_exec()sqlite3_prepare()sqlite3_step()并且sqlite3_finalize()-我得到相同的输出-创建了查询,但未在数据库中创建记录

  3. 我能够检索记录信息
4

2 回答 2

2

您不能写入资源包中的数据库。您需要复制它然后使用它。

这是我已成功用于执行此操作的一些代码(密钥是 ensureDatabasePrepared,它是从资源中复制的):

- (BOOL)ensureDatabaseOpen: (NSError **)error
{
    // already created db connection
    if (_contactDb != nil)
    {
        return YES;
    }

    NSLog(@">> ContactManager::ensureDatabaseOpen");    
    if (![self ensureDatabasePrepared:error])
    {
        return NO;
    }

    const char *dbpath = [_dbPath UTF8String]; 
    if (sqlite3_open(dbpath, &_contactDb) != SQLITE_OK &&
        error != nil)
    {
        *error = [[[NSError alloc] initWithDomain:@"ContactsManager" code:1000 userInfo:nil] autorelease];
        return NO;
    }

   NSLog(@"opened");

    return YES;
}

- (BOOL)ensureDatabasePrepared: (NSError **)error
{
    // already prepared
    if ((_dbPath != nil) &&
        ([[NSFileManager defaultManager] fileExistsAtPath:_dbPath]))
    {
        return YES;
    }

    // db in main bundle - cant edit.  copy to library if !exist
    NSString *dbTemplatePath = [[NSBundle mainBundle] pathForResource:@"contacts" ofType:@"db"];
    NSLog(@"%@", dbTemplatePath);

    NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
    _dbPath = [libraryPath stringByAppendingPathComponent:@"contacts.db"];

    NSLog(@"dbPath: %@", _dbPath);

    // copy db from template to library
    if (![[NSFileManager defaultManager] fileExistsAtPath:_dbPath])
    {
        NSLog(@"db not exists");
        NSError *error = nil;
        if (![[NSFileManager defaultManager] copyItemAtPath:dbTemplatePath toPath:_dbPath error:&error])
        {
            return NO;
        }

        NSLog(@"copied");
    }    

    return YES;    
}
于 2013-01-10T12:41:40.510 回答
0

如果您仅通过发布的代码与数据库交互 - 您缺少 sqlite3_close。很可能更改没有刷新到磁盘上

于 2013-01-10T12:41:29.080 回答