ruby sqlite error 'ensure in insert_row': undefined local variable or method 'stmt' -
i have following method in dbmgr ruby class, im using within loop insert data csv sqlite3 db:
def insert_row(arow) begin @db = sqlite3::database.open("#{@dbn}") rhash = arow.row_hash stm = @db.prepare("insert 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
when on console after 1st insertion following error:
`ensure in insert_row': undefined local variable or method `stmt' #<dbmgr:0x007f9511050188> (nameerror)
initially did not use prepare , bind_params features of ruby sqlite. however, after characters " ' " in text inserting generated exceptions, read somewhere using bind_params clean input used bind_params. im getting error.
can please?
thanks
stm = @db.prepare("insert programs (programname, episodename) values (? , ? )") stm.bind_param(1, '#{rhash["program"]}' ) stm.bind_param(2, '#{rhash["episode name"]}' ) stmt.execute() # <-- you're referring stmt instead of stm here programid = @db.last_insert_row_id
you've used stm throughout statement, refer stmt.execute()
, stmt.close
Comments
Post a Comment