0

ビューコントローラーの詳細でいくつかのテキストフィールドからレコードを更新したいのですが、更新ボタンをクリックすると、データベースの更新に失敗します。plsは私を提案します。

-(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

私の方法では、次のように動作します:

-(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

これを試してください...うまくいくはずです...

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

アプリケーション バンドル内の他のファイルと同様に、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 に答える