0

私はdbmgr ruby​​クラスに次のメソッドを持っています。これをループ内で使用して、csvからsqlite3 dbにデータを挿入します。

  def insert_row(aRow)
    begin
    @db = SQLite3::Database.open("#{@dbn}")
    rhash = aRow.row_hash

    stm = @db.prepare("INSERT INTO programs (programName, episodeName) VALUES (? , ? )")
        stm.bind_param(1, '#{rhash["Program"]}' )
        stm.bind_param(2, '#{rhash["Episode Name"]}' )
        stmt.execute()
        programId = @db.last_insert_row_id


     rescue SQLite3::Exception => e
        puts "Exception occured"
        puts e.message
        puts e.backtrace

    ensure
        stmt.close if stmt
        @db.close if @db

    end #db

最初の挿入後にコンソールでこれを行うと、次のエラーが表示されます。

  `ensure in insert_row': undefined local variable or method `stmt' for #<Dbmgr:0x007f9511050188> (NameError)

最初は、sqlite の ruby​​ の prepare および bind_params 機能を使用しませんでした。ただし、テキスト内の「 ' 」などの文字の後に、生成された例外を挿入していたので、bind_params を使用すると入力が消去されることをどこかで読んだので、bind_params を使用しました。しかし、今はこのエラーが発生しています。

誰か助けてくれませんか?

ありがとう

4

1 に答える 1

0
stm = @db.prepare("INSERT INTO programs (programName, episodeName) VALUES (? , ? )")
    stm.bind_param(1, '#{rhash["Program"]}' )
    stm.bind_param(2, '#{rhash["Episode Name"]}' )
    stmt.execute() # <-- you're referring to stmt instead of stm here
    programId = @db.last_insert_row_id

このステートメント全体で stm を使用しましたstmt.execute()stmt.close

于 2013-07-25T11:44:35.830 に答える