I’m trying to load a .sql file in a ruby script into a string and then execute it.
For some reason i’m getting a Syntax error, however if i copy and paste the statements directly into mysql it works just fine.
Here’s how i do it:
text = File.read(src_sql_file)
new_text = text.DOINGSOMEGSUBSTUFF
@dbh.select_db(dbname)
sql = @dbh.prepare(new_text)
sql.execute()
i’ve also tried this:
sql = @dbh.prepare(new_text) do |sth|
sth.execute()
end
and i always get:
Error message: You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near
'CREATE TABLE `the_logs` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PR' at line 5
where the sql for this looks like that:
CREATE TABLE `the_logs` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`timestamp` INT( 11 ) NOT NULL ,
...
Any idea if i’m doing this wrong using prepare? i’ve also tried query.. didn’t work either.
Any help is greatly appreciated.
T
I expect whichever library you’re using to talk to MySQL wants to handle one statement at a time and you’re passing it several statements at once.
Try splitting the string on semicolons and executing each statement individually.