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 回答