0

I want to update records from several textfiels in detail viewcontroller, but when I cliCk on update button, its goes to failed to update database. pls suggest me.

-(IBAction)updateQuery:(id)sender
{
    sqlite3_stmt *statement;

    const char *dbpath = [databasePath UTF8String];

    if (sqlite3_open(dbpath, &Information) == SQLITE_OK)
    {
        NSString *updateSQL = [NSString stringWithFormat: @"update CONTACTS set address=\"%@\",phone=\"%@\",imageUrl=\"%@\" WHERE name=\"%@\"",daddress.text,dphone.text,durl.text,dname.text];

        const char *update_stmt = [updateSQL UTF8String];

        sqlite3_prepare_v2(Information, update_stmt, -1, &statement, NULL);

        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            dstatuslabel.text=@"updated successfully";
        }
        else
        {
            dstatuslabel.text = @"Failed to update contact";
        }

        sqlite3_finalize(statement);
        sqlite3_close(Information);
    }

}
4

4 回答 4

0
    NSString *updateSQL = [NSString stringWithFormat: @"update CONTACTS set address='%@',phone='%@',imageUrl='%@' WHERE name='%@'",daddress.text,dphone.text,durl.text,dname.text];
于 2013-08-13T09:31:06.700 回答
0

试试这个......它应该工作......

1)请确保在更新数据库之前将数据库从捆绑包复制到文档目录...

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,   YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"YOUR DBNAME.sqlite"];


// Check if the SQL database has already been saved to the users phone, if not then copy it over
BOOL success;

// Create a FileManager object, we will use this to check the status
// of the database and to copy it over if required
NSFileManager *fileManager = [NSFileManager defaultManager];

// Check if the database has already been created in the users filesystem
success = [fileManager fileExistsAtPath:filePath];

// If the database already exists then return without doing anything
if(success) return;

// If not then proceed to copy the database from the application to the users filesystem

// Get the path to the database in the application package
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:_databaseName];

// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath:databasePathFromApp toPath:_databasePath error:nil];

}

2)第 2 步更新

sqlite3 *database;
sqlite3_stmt *updateStmt=nil;
const char *dbpath = [filePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
     NSString *updateSQL = [NSString stringWithFormat: @"update CONTACTS set address=\"%@\",phone=\"%@\",imageUrl=\"%@\" WHERE name=\"%@\"",daddress.text,dphone.text,durl.text,dname.text];
    const char *insert_stmt = [insertSQL UTF8String];
    sqlite3_prepare_v2(database, insert_stmt,-1, &updateStmt, NULL);
    if (sqlite3_step(updateStmt) == SQLITE_DONE)
    {

        NSLog(@"updated");
    }
}
于 2013-08-13T09:46:39.697 回答
0

在我的方法中,我使用以下方式工作:

-(BOOL)updateProfileFromCreatorId:(int)creatorProfileId{

    char *st,*errorMsg;
    NSLog(@"profile.creatorProfileId:%d",creatorProfileId);
    st = sqlite3_mprintf("UPDATE Profile SET `CreatorID`=%d WHERE `CreatorID`= 1"
                         ,creatorProfileId //username

                         );

    NSLog(@"updateQUERY: %@",[NSString stringWithUTF8String:st]);
    int ret = sqlite3_exec(rlraDb, st, NULL, NULL, &errorMsg);
    if (ret != SQLITE_OK) {
        sqlite3_free(errorMsg);
        sqlite3_free(st);
        return NO;
    }
    sqlite3_free(st);
    return YES;

}
于 2013-08-13T09:21:46.123 回答
0

像应用程序包中的任何其他文件一样添加 Sqlite DB

通过代码将其复制到文档目录并使用它(方法:checkAndCreateDatabase)。这样做的目的是更新 sqlite 中的内容只能在 Documents 目录中进行

-(void) checkAndCreateDatabase
{
    // Check if the SQL database has already been saved to the users phone, if not then copy it over
    BOOL success;

    // Create a FileManager object, we will use this to check the status
    // of the database and to copy it over if required
    NSFileManager *fileManager = [NSFileManager defaultManager];

    // Check if the database has already been created in the users filesystem
    success = [fileManager fileExistsAtPath:_databasePath];

    // If the database already exists then return without doing anything
    if(success) return;

    // If not then proceed to copy the database from the application to the users filesystem

    // Get the path to the database in the application package
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:_databaseName];

    // Copy the database from the package to the users filesystem
    [fileManager copyItemAtPath:databasePathFromApp toPath:_databasePath error:nil];

}

- (id)init {
    if ((self = [super init]))
    {
        _databaseName = DB_NAME;

        NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDir = [documentPaths objectAtIndex:0];
        _databasePath = [documentsDir stringByAppendingPathComponent:_databaseName];

        if (sqlite3_open([[self dbPath] UTF8String], &_database) != SQLITE_OK)
        {
            [[[UIAlertView alloc]initWithTitle:@"Missing"
                                       message:@"Database file not found"
                                      delegate:nil
                             cancelButtonTitle:@"OK"
                             otherButtonTitles:nil, nil]show];
        }
    }
    return self;
}
于 2013-08-13T09:24:39.607 回答