我有一个小型 ruby 程序,它登录到 sftp 服务器,从服务器上的目录中提取几个文件名,然后将这些文件名写入 sqlite3 数据库。
我将文件名从 sftp 保存到数组中。我可以确认该数组已创建并具有值,但是当我尝试使用该数组中的数据保存到数据库时出现错误,老实说,我无法弄清楚错误的含义?或者为什么数组中的数据没有被写入?
另一个奇怪的事情是我可以手动创建一个与来自 sftp 的值完全相同的数组,并使用来自该新数组的相同数据成功保存到数据库。为什么从 sftp 数据创建的数组会导致错误,但我使用完全相同的数据手动创建的数组却成功写入数据库?
谢谢你的帮助!
我的程序(文件:xb.rb):
# db
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => 'D:/prog/test/db.sqlite3.db'
)
class Db < ActiveRecord::Base
end
# connect to sftp and get last 2 filenames, put them in array
require 'net/sftp'
@files = []
Net::SFTP.start('XXX.XXX.XXX.XXX','xxx',:password => 'xxx') do |sftp|
sftp.dir.foreach("/Outgoing/") do |sftp_file|
@files.push(sftp_file.name)
end
end
# Get last 2 filenames
@files = @files.sort!.pop(2)
# Show array to confirm it has values and that it's an array
puts @files
puts @files.class
# Write filenames to db
@files.each do |new_file|
Db.create(:name => new_file)
end
回来:
PS D:\prog\test> ruby xb.rb
20120823002349.txt
20120824012407.txt
Array
D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/sqlite_adapter.rb:212:in `type_cast': NoMethodError: undefined method `error' for nil:NilClass: INSERT INTO "dbs" ("name") VALUES (?) (ActiveRecord::StatementInvalid)
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/sqlite_adapter.rb:264:in `block (2 levels) in exec_query'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/sqlite_adapter.rb:263:in `map'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/sqlite_adapter.rb:263:in `block in exec_query'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/sqlite_adapter.rb:247:in `exec_query'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:61:in `exec_insert'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:88:in `insert'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `insert'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/relation.rb:66:in `insert'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/persistence.rb:363:in `create'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/timestamp.rb:57:in `create'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/callbacks.rb:268:in `block in create'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:403:in `_run__619764720__create__513830955__callbacks'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:405:in `__run_callback'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:385:in `_run_create_callbacks'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:81:in `run_callbacks'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/callbacks.rb:268:in `create'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/persistence.rb:344:in `create_or_update'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/callbacks.rb:264:in `block in create_or_update'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:414:in `_run__619764720__save__513830955__callbacks'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:405:in `__run_callback'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:385:in `_run_save_callbacks'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:81:in `run_callbacks'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/callbacks.rb:264:in `create_or_update'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/persistence.rb:84:in `save'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/validations.rb:50:in `save'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/attribute_methods/dirty.rb:22:in `save'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/transactions.rb:241:in `block (2 levels) in save'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:190:in `transaction'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/transactions.rb:208:in `transaction'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/transactions.rb:241:in `block in save'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/transactions.rb:252:in `rollback_active_record_state!'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/transactions.rb:240:in `save'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/persistence.rb:45:in `create'
from xb.rb:27:in `block in <main>'
from xb.rb:26:in `each'
from xb.rb:26:in `<main>'
但是,如果我手动创建一个与@files 具有完全相同的值的数组(@files2)(我将@files2 与@files 进行比较以确认),我可以成功地使用@files2 中的数据写入数据库。
测试(附加到以前的程序):
.
.
.
# create manual array with same data as @files
@files2 = ["20120823002349.txt","20120824012407.txt"]
puts @files == @files2
# Write filenames to db
@files2.each do |new_file|
Db.create(:name => new_file)
end
回报:
PS D:\prog\test> ruby xb.rb
20120823002349.txt
20120824012407.txt
Array
true
测试将数组值写入数据库。我很困惑,为什么我不能使用 sftp 数组(@files)中的数据写入 db,而 @files2 具有完全相同的数据并且可以用于写入 db?
我感谢任何帮助、想法和想法。