Stephan1990
Stephan1990

Reputation: 465

ruby on rails sql statement error

Hi guys i have a model livestream which has two columns in the database, 'user_id' and 'activity_id'. Very straight forward.

Now when I try to insert multiple records into the database I get an invalid SQL statement:

SQLite3::SQLException: near ",": syntax error: INSERT INTO livestreams (user_id, activity_id) 
          VALUES (1, 2), (101, 2)

The code used to do this comes from insoshi, and is as followes:

def do_livestream_insert(users_ids, activity_id)
      sql = %(INSERT INTO livestreams (user_id, activity_id) 
              VALUES #{values(users_ids, activity_id)})
      ActiveRecord::Base.connection.execute(sql)
end

def values(ids, common_value)
      common_values = [common_value] * ids.length
      convert_to_sql(ids.zip(common_values))
end

def convert_to_sql(array_of_values)
      array_of_values.inspect[1...-1].gsub('[', '(').gsub(']', ')')
end

Why does rails do that? Does SQLlite not support the insertion of multiple records at one time?

I have reseted the database, and repopulated it.

Thanks for your help, much appreciated.

Have a nice day, Stefano

Upvotes: 2

Views: 550

Answers (2)

Li0liQ
Li0liQ

Reputation: 11264

SQLite did not support multiple records insertion until version 3.7.11.

Upvotes: 2

Ransom Briggs
Ransom Briggs

Reputation: 3085

I would suggest redefining do_livestream_insert to instead to N inserts

Upvotes: 0

Related Questions