无法使用sqlite3数据库

时间:2016-06-25 11:04:50

标签: csv sqlite ruby-on-rails-4.2

我的csv文件从大文件中获取最多26或78个值,并且不会导入整个数据。

rails console view

irb(main):009:0> Stocking.count
   (0.0ms)  SELECT COUNT(*) FROM "stockings"
=> 26
irb(main):010:0> Stocking.delete_all
  SQL (109.4ms)  DELETE FROM "stockings"
=> 26
irb(main):011:0> Stocking.count
   (0.0ms)  SELECT COUNT(*) FROM "stockings"
=> 78
irb(main):012:0> Stocking.delete_all
  SQL (62.5ms)  DELETE FROM "stockings"
=> 78
irb(main):013:0> Stocking.count
   (0.0ms)  SELECT COUNT(*) FROM "stockings"
=> 26

当我使用count命令检查rails控制台时。

index.html.erb

<h2>Import Stock File</h2>
    <%= form_tag import_stockings_path, multipart: true do %>
    <%= file_field_tag :file %>
    <%= submit_tag "Import" %>
    <% end %>

stockings_controller.rb

class StockingsController < ApplicationController
    before_action :set_stocking, only: [:show, :edit, :update, :destroy]

  # GET /Stockings
  # GET /deldetails.json
  def index
    @stockings = Stocking.all
  end

  def import
    Stocking.import(params[:file])
    redirect_to stockings_url, notice: "Stockings imported."
  end

  # GET /Stockings/1
  # GET /Stockings/1.json
  def show
  end

  # GET /Stockings/new
  def new
    @stocking = Stocking.new
  end

  # GET /stockings/1/edit
  def edit
  end

  # POST /Stockings
  # POST /Stockings.json
  def create
    @stocking = Stocking.new(stocking_params)

    respond_to do |format|
      if @stocking.save
        format.html { redirect_to @stocking, notice: 'Stocking was successfully created.' }
        format.json { render :show, status: :created, location: @stocking }
      else
        format.html { render :new }
        format.json { render json: @stocking.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /Stockings/1
  # PATCH/PUT /stockings/1.json
  def update
    respond_to do |format|
      if @stocking.update(stocking_params)
        format.html { redirect_to @stocking, notice: 'Stocking was successfully updated.' }
        format.json { render :show, status: :ok, location: @stocking }
      else
        format.html { render :edit }
        format.json { render json: @stocking.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /stockings/1
  # DELETE /stockings/1.json
  def destroy
    @stocking.destroy
    respond_to do |format|
      format.html { redirect_to stockings_url, notice: 'Stocking was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_stocking
      @stocking = Stocking.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def stocking_params
      params.require(:stocking).permit(:season, :category, :articleno, :description, :color, :quantity, :rprice, :tamount, :cartonno )
    end
  end

stocking.rb

类库存&lt;的ActiveRecord ::基

def self.import(file)
        CSV.foreach(file.path, headers: true) do |row|
        puts row.inspect
        end
    end

end

application.rb中

require File.expand_path('../boot', __FILE__)

require 'csv'

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module Stock
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    # config.time_zone = 'Central Time (US & Canada)'

    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    # config.i18n.default_locale = :de

    # Do not swallow errors in after_commit/after_rollback callbacks.
    config.active_record.raise_in_transactional_callbacks = true


    config.middleware.use 'Utf8Sanitizer'

end
end

的routes.rb

Rails.application.routes.draw do

  root 'stockings#index'

  resources :stockings do
    collection { post :import }
  end


end

我想将所有数据导入我的sqlite3数据库。

欢迎任何建议。

提前谢谢。

1 个答案:

答案 0 :(得分:0)

真正的问题在于我的上传文件,因为格式不正确,因为它没有正常化。