我该如何更新sqlite数据库

时间:2013-08-13 09:15:59

标签: iphone ios objective-c sqlite

我想在详细视图控制器中更新来自多个文本字段的记录,但是当我在更新按钮上cliCk时,它将无法更新数据库。请建议我。

-(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 个答案:

答案 0 :(得分: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;

}

答案 1 :(得分:0)

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

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

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

答案 2 :(得分:0)

    NSString *updateSQL = [NSString stringWithFormat: @"update CONTACTS set address='%@',phone='%@',imageUrl='%@' WHERE name='%@'",daddress.text,dphone.text,durl.text,dname.text];

答案 3 :(得分:0)

试试这个......它应该有用......

1)在更新db ...

之前,请确保将数据库从数据包复制到文档目录
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");
    }
}