草庐IT

iphone FMDB/SQLite 更新查询不起作用

coder 2024-01-15 原文

我尝试使用 sql wrapper (FMDB) 在表中进行更新。插入方法没问题,当我尝试更新表中的数据时出现问题。

- (void)saveUserMoneyValidation:(UserDebtInfo *)entities
{
    NSLog(@"entities.client_index = %d", entities.client_index);

    [self deleteUserMoneyValidation:entities.client_index];

    FMDatabase *newdb = [FMDatabase databaseWithPath:databasePathStore];

    [newdb setLogsErrors:TRUE];
    [newdb setTraceExecution:FALSE];

    if (![newdb open])
    { 
        NSLog(@"!!! Could not open db."); 
        return; 
    }
    else 
    { 
        NSLog(@"DB Open successfuly."); 
    }

    [newdb beginTransaction];
    NSLog(@"entities = %@",entities);

    [newdb executeUpdate:@"INSERT INTO MoneyValidation (client_index, client_name, sum, duration, monthly_payment, payment_type, debt_type, debt_code, categoryType) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
    [NSNumber numberWithInt:entities.client_index],
     entities.client_name,
     entities.sum,
     entities.duration,
     entities.monthly_payment,
     entities.payment_type,
     entities.debt_type,
     [NSNumber numberWithInt:entities.debt_code],
     entities.categoryType
     ];

    [newdb commit];
    [newdb close];
}

更新方法:

   -(void)updateUserMoneyValidation:(UserDebtInfo *)entities{
NSLog(@"entities.client_index = %d", entities.client_index);

FMDatabase *newdb = [FMDatabase databaseWithPath:databasePathStore];

[newdb setLogsErrors:TRUE];
[newdb setTraceExecution:FALSE];

if (![newdb open])
{ 
    NSLog(@"!!! Could not open db."); 
    return; 
}
else 
{ 
    NSLog(@"DB Open successfuly."); 
}

BOOL success = NO;

[newdb beginTransaction];
NSLog(@"entities = %@",entities);

success = [newdb executeUpdate:@"UPDATE MoneyValidation SET client_name = ?, sum = ?, duration = ?, monthly_payment = ?, payment_type = ?, debt_type = ?, debt_code = ?, categoryType = ?  WHERE client_index = %d;", entities.client_name, entities.sum, entities.duration, entities.duration, entities.monthly_payment, entities.payment_type, entities.debt_type, entities.debt_code, entities.categoryType, entities.client_index];

if (success)
{
    NSLog(@"OK");
    [db commit];
    [db close];
}
else 
{
    NSLog(@"FAIL");
}

NSLog(@"OBJ - %@", entities);}

表结构:

CREATE TABLE MoneyValidation (ID INTEGER PRIMARY KEY, client_index INTEGER, client_name TEXT, sum TEXT, duration TEXT, monthly_payment TEXT, payment_type TEXT, debt_type TEXT, debt_code INTEGER, categoryType TEXT)
  • client_index是唯一码;

    有人可以帮我提供一些关于这个问题的建议吗? 谢谢!

最佳答案

我认为问题在于您的更新语句。传递给“executeUpdate”的所有值都应该是对象。

在您的更新 SQL 语句中,entities.debt_codeentities.client_index 将作为 INTEGERS 传递,您需要将它们作为 NSNumber 传递对象。

尝试以下操作:

success = [newdb executeUpdate:@"UPDATE MoneyValidation 
           SET client_name = ?, sum = ?, 
           duration = ?, monthly_payment = ?, 
           payment_type = ?, debt_type = ?, 
           debt_code = ?, categoryType = ?  
           WHERE client_index = %d;", 

          entities.client_name, entities.sum, 
          entities.duration, entities.duration, 
          entities.monthly_payment, entities.payment_type, 
          entities.debt_type, 
          [NSNumber numberWithInt:entities.debt_code],  // Convert int to object type
          entities.categoryType, 
          [NSNumber numberWithInt:entities.client_index] // Convert int to object type
          ];

关于iphone FMDB/SQLite 更新查询不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12621073/

有关iphone FMDB/SQLite 更新查询不起作用的更多相关文章

  1. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  2. ruby - ECONNRESET (Whois::ConnectionError) - 尝试在 Ruby 中查询 Whois 时出错 - 2

    我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.

  3. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  4. ruby-on-rails - 使用 rails 4 设计而不更新用户 - 2

    我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它​​不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数

  5. ruby-on-rails - 在 Rails 和 ActiveRecord 中查询时忽略某些字段 - 2

    我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr

  6. sql - 查询忽略时间戳日期的时间范围 - 2

    我正在尝试查询我的Rails数据库(Postgres)中的购买表,我想查询时间范围。例如,我想知道在所有日期的下午2点到3点之间进行了多少次购买。此表中有一个created_at列,但我不知道如何在不搜索特定日期的情况下完成此操作。我试过:Purchases.where("created_atBETWEEN?and?",Time.now-1.hour,Time.now)但这最终只会搜索今天与那些时间的日期。 最佳答案 您需要使用PostgreSQL'sdate_part/extractfunction从created_at中提取小时

  7. objective-c - 在设置 Cocoa Pods 和安装 Ruby 更新时出错 - 2

    我正在尝试为我的iOS应用程序设置cocoapods但是当我执行命令时:sudogemupdate--system我收到错误消息:当前已安装最新版本。中止。当我进入cocoapods的下一步时:sudogeminstallcocoapods我在MacOS10.8.5上遇到错误:ERROR:Errorinstallingcocoapods:cocoapods-trunkrequiresRubyversion>=2.0.0.我在MacOS10.9.4上尝试了同样的操作,但出现错误:ERROR:Couldnotfindavalidgem'cocoapods'(>=0),hereiswhy:U

  8. ruby-on-rails - Rails Associations 的更新方法是什么? - 2

    这太简单了,太荒谬了,我在任何地方都找不到关于它的任何信息,包括API文档和Rails源代码:我有一个:belongs_to关联,我开始理解当您没有关联时您在Controller中调用的正常模型方法与您有关联时调用的方法略有不同。例如,我的关联在创建Controller操作时运行良好:@user=current_user@building=Building.new(params[:building])respond_todo|format|if@user.buildings.create(params[:building])#etcetera但我找不到关于更新如何工作的文档:@user

  9. ruby-on-rails - "assigns"在 Ruby on Rails 中有什么作用? - 2

    我目前正在尝试学习RubyonRails和测试框架RSpec。assigns在此RSpec测试中做什么?describe"GETindex"doit"assignsallmymodelas@mymodel"domymodel=Factory(:mymodel)get:indexassigns(:mymodels).shouldeq([mymodel])endend 最佳答案 assigns只是检查您在Controller中设置的实例变量的值。这里检查@mymodels。 关于ruby-o

  10. ruby-on-rails - solr 清理查询 - 2

    我在Rails上使用带有ruby​​的solr。一切正常,我只需要知道是否有任何现有代码来清理用户输入,比如以?开头的查询。或* 最佳答案 我不知道执行此操作的任何代码,但理论上可以通过查看parsingcodeinLucene来完成并搜索thrownewParseException(只有16个匹配!)。在实践中,我认为您最好只捕获代码中的任何solr异常并显示“无效查询”消息或类似信息。编辑:这里有几个“sanitizer”:http://pivotallabs.com/users/zach/blog/articles/937-s

随机推荐