从CSV任务标记为SQLite标记多少?

时间:2012-11-23 00:16:01

标签: ruby-on-rails sqlite ruby-on-rails-3.2 acts-as-taggable-on rake-task

我有一个rake任务,可以通过身份证的CSV文件进行操作。然后我查询数据库以查看我的数据库中是否存在该ID。如果它存在,我使用act_as_taggable添加标签。该脚本运行正常,直到它到达第一个匹配并尝试将标记写入我的SQLite DB。我得到一个数据库锁定错误。我想知道我是否达到了SQLite的I / O限制,需要切换到一个完整的MySQL数据库。

if defined?(Rails) && (Rails.env == 'development')
  Rails.logger = Logger.new(STDOUT)
end

require 'csv'
desc "Tag Voters that early voted from the Secretary of State Website"
task :tag_early => [:environment] do
  file ="db/AllAbsentees.csv"

  CSV.foreach(file, :headers=> true) do |row|
    @voter_id = row[1]
    puts "Working on Row" + row[1]
    @voter = Voter.where(:state_id => @voter_id).first()
    unless @voter.blank?
      @voter.tag_list = "2012_GEN_EARLY_VOTER"
      @voter.save()
      puts "Voter #" + @voter_id + "tagged"
    end
  end
end


    Voter Load (1.2ms)  SELECT "voters".* FROM "voters" WHERE "voters"."state_id" = '00008030' LIMIT 1
  ActsAsTaggableOn::Tag Load (0.2ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 11944 AND "taggings"."taggable_type" = 'Voter' AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)
   (0.1ms)  begin transaction
   (0.4ms)  UPDATE "voters" SET "updated_at" = '2012-11-23 00:02:33.438114' WHERE "voters"."id" = 11944
  ActsAsTaggableOn::Tag Load (0.1ms)  SELECT "tags".* FROM "tags" WHERE (lower(name) = '2012_gen_early_voter')
  ActsAsTaggableOn::Tag Load (0.2ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 11944 AND "taggings"."taggable_type" = 'Voter' AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)
  ActsAsTaggableOn::Tagging Exists (0.1ms)  SELECT 1 AS one FROM "taggings" WHERE ("taggings"."tag_id" = 19 AND "taggings"."taggable_type" = 'Voter' AND "taggings"."taggable_id" = 11944 AND "taggings"."context" = 'tags' AND "taggings"."tagger_id" IS NULL AND "taggings"."tagger_type" IS NULL) LIMIT 1
  SQL (1.4ms)  INSERT INTO "taggings" ("context", "created_at", "tag_id", "taggable_id", "taggable_type", "tagger_id", "tagger_type") VALUES (?, ?, ?, ?, ?, ?, ?)  [["context", "tags"], ["created_at", Fri, 23 Nov 2012 00:02:33 UTC +00:00], ["tag_id", 19], ["taggable_id", 11944], ["taggable_type", "Voter"], ["tagger_id", nil], ["tagger_type", nil]]
   (5053.1ms)  commit transaction
SQLite3::BusyException: database is locked: commit transaction
   (99.7ms)  rollback transaction
rake aborted!
SQLite3::BusyException: database is locked: commit transaction

1 个答案:

答案 0 :(得分:1)

SQLite没有太多的并发性;对于要写入DB的事务,必须没有其他读写连接。

确保没有其他程序同时读取或写入数据库,并且您自己程序中的所有数据库访问都使用相同的数据库连接。