将数据插入表格时出现问题

时间:2011-02-03 18:45:19

标签: iphone objective-c

sqlite3 *数据库;     sqlite3_stmt *语句;     NSString * dPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@“UserData.sqlite”];     const char * dbpath = [dPath UTF8String];    // NSLog(@“%@”,dbpath);     // UserArray = [[NSMutableArray alloc] init];

if(sqlite3_open(dbpath, &database) == SQLITE_OK)
{

    NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO User (UserName,FullName,Email,PhoneNo) VALUES (\"%@\", \"%@\", \"%@\" ,\"%@\")",txtUserName.text,txtFullName.text ,txtEmail.text , txtPhoneNo.text];

    const char *insert_stmt = [insertSQL UTF8String];

    sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL);

    if(sqlite3_step(statement)==SQLITE_DONE) 
    {
        txtUserName.text=@"";
        txtFullName.text=@"";
        txtEmail.text=@"";
        txtPhoneNo.text=@"";
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Add Record" message:@"Contact Added" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];    
        [alert show];
        [alert release];
        alert=nil;

    }
    else 
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"record" message:@"record not created" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];   
        [alert show];
        [alert release];
        alert=nil;
    }


    // Release the compiled statement from memory
    sqlite3_finalize(statement);

    sqlite3_close(database);
}

- >>我正在尝试将数据插入表中。代码执行正常但记录未添加到表中。所以请帮助我或这个问题..谢谢你的回复。

2 个答案:

答案 0 :(得分:0)

尝试执行commit,因此将db设置为autocommit

// COMMIT
  const char *sql2 = "COMMIT TRANSACTION";
  sqlite3_stmt *commit_statement;
  if (sqlite3_prepare_v2(database, sql2, -1, &commit_statement, NULL) != SQLITE_OK) {
    NSAssert1(0, @"Error Message: '%s'.", sqlite3_errmsg(database));
  }
  success = sqlite3_step(commit_statement);
  if (success != SQLITE_DONE) {
    NSAssert1(0, @"Error Message: '%s'.", sqlite3_errmsg(database));
  }
}

答案 1 :(得分:0)

我担心您为什么要对应用程序包中的db执行INSERT操作。尝试使用我确信可能对您有用的示例代码

-(void)checkAndCreateDB {
    NSString* databaseName = @"MasterDB.sqlite";
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
    BOOL success1;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    success = [fileManager fileExistsAtPath:databasePath];
    if(success) return;
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}

-(void)insertData{

    sqlite3 *database;
    sqlite3_stmt *statement; 
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
    {

        NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO User (uId,userName) VALUES (\"%d\", \"%@\")",1,@"RAHUL"];
        const char *insert_stmt = [insertSQL UTF8String];
        sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL);

        if(sqlite3_step(statement)==SQLITE_DONE) 
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Add Record" message:@"Contact Added" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];    
            [alert show];
            [alert release];
            alert=nil;

        }
        else 
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"record" message:@"record not created" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];   
            [alert show];
            [alert release];
            alert=nil;
        }   
        // Release the compiled statement from memory
        sqlite3_finalize(statement);    
        sqlite3_close(database);
    }
}

这里我在My Project资源文件夹中复制了MasterDB.sqlite文件。在运行应用程序时,我们通常会将数据库复制到应用程序文档文件夹中,然后执行任何READ / WRITE操作。它是执行INSERT操作的熊最小代码,只需调用CheckAndCreatreDB后跟insertData函数。对于包含这些方法的类,变量databasePath在.h文件中声明。

希望这会有所帮助!!