jeudi 27 août 2015

No such file or directory - questions.csv

Hi friends i am getting above error when i tried to import .csv file from the user. I tried with different methods but it is not working. Please help me resolve this issue. I am new to Ruby family. Below i am mentioning all the codes i have tried .

1 question.rb

def import(file)

     CSV.foreach(file.name, :headers => true) do |row|

     values = row.inject([]){|k,v| k<<"'#{v}'";k}.join(',')

        sql = "INSERT INTO questions (description,question_type_id,mark) VALUES #{values}"
        @question = Question.find_by_sql(sql)
    end
end 

error is :-NoMethodError (undefined method `name' for "questions.csv":String

2 question.rb

def import(file)
     f = File.open(file)

     CSV.foreach(f, :headers => true) do |row|

     values = row.inject([]){|k,v| k<<"'#{v}'";k}.join(',')

        sql = "INSERT INTO questions (description,question_type_id,mark) VALUES #{values}"
        @question = Question.find_by_sql(sql)
    end
end

error is :- Errno::ENOENT (No such file or directory - questions.csv

3 question.rb

def import(file)

    path = File.expand_path(file)
      CSV.foreach(path, :headers=> true) do |row|
        question_hash = row.to_hash

        @question = Question.where(:id=> question_hash["id"])

        if @question.count == 1

          @question.first.update_attributes(question_hash)

        else
          Question.save!(question_hash)

        end # end if !@question.nil?
      end # end CSV.foreach
    end # end self.import(file)

error is :- Errno::ENOENT (No such file or directory - /var/www/VirtualX/questions.csv): app/models/question.rb:94:in import' app/controllers/questions_controller.rb:824:inimport'

4 question.rb

def import(file)

     CSV.foreach(file, :headers => true) do |row|

     values = row.inject([]){|k,v| k<<"'#{v}'";k}.join(',')

        sql = "INSERT INTO questions (description,question_type_id,mark) VALUES #{values}"
        @question = Question.find_by_sql(sql)
    end
end

error is :- Errno::ENOENT (No such file or directory - questions.csv):

5 question.rb

def import(file)
    CSV::Reader.parse(file) do |row|
          values = row.inject([]){|k,v| k<<"'#{v}'";k}.join(',')

        sql = "INSERT INTO questions (description,question_type_id,mark) VALUES #{values}"
        @question = Question.find_by_sql(sql)

    end
end

In this case code is working but not taking value of csv which i have uploaded instead taking name of that csv file . I am getting below error .

****1mINSERT INTO questions (description,question_type_id,mark) VALUES 'questions.csv'[0m Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''questions.csv'' at line 1: INSERT INTO questions (description,question_type_id,mark) VALUES 'questions.csv' Completed in 407ms**

**ActiveRecord::StatementInvalid (Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''questions.csv'' at line 1: INSERT INTO questions (description,question_type_id,mark) VALUES 'questions.csv'): app/models/question.rb:95:in import' app/models/question.rb:91:inimport' app/controllers/questions_controller.rb:824:in `import'****

Please help me friends i need to insert data of .csv file to corresponding table. I am using Ruby 1.8.7 and Rails of 3

And also suggest any other good method to import. And also tell me What is parse and foreach and what their functionalities .

Aucun commentaire:

Enregistrer un commentaire