Question

I'm having a hard time using variables in my oci8 insert statement.

CSV.foreach('C:/csv_test.csv') do |row|
gender = row[0]
name = row[1]
year = row[2]
command = 'INSERT INTO CSV_TEST VALUES('+gender+','+name+','+year+')'
conn.exec(command)
end

i made sure that everything works fine. Gender, name and year contain my data and the insert works just fine. But i can't call the gender, name and year variables in my insert statement. The error I'm receiving is:

column not allowed here

Any help will be appreciated. Thanks in advance.

Was it helpful?

Solution

The safest way to do this is using bindvariables. This will eliminate any escaping bugs and security flaws.

Oracle has a decent ruby tutorial for working with ruby. Unfortunately I don't have Oracle installed, so I can't test this this. I'm guessing the following will work though:

cursor = conn.parse('INSERT INTO CSV_TEST VALUES(:gender,:name,:year)')
cursor.bind_param(1,String)
cursor.bind_param(2,String)
cursor.bind_param(3,Fixnum) 
CSV.foreach('C:/csv_test.csv') do |row|
    gender = row[0]
    name = row[1]
    year = row[2]
    cursor[1] = gender
    cursor[2] = name
    cursor[3] = year.to_i
    cursor.exec  
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top