To be organized I want to have a file db/seeds/countries.rb with:
ActiveRecord::Base.connection.execute(IO.read("db/sql/countries_iso3166.sql"))
And this file db/sql/countries_iso3166.sql has a SQL that when I paste in server it runs successfully, but when I run it with rake db:seed I get:
Mysql2::Error: 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
'INSERT INTO `countries` (`code`, `alpha2`, `alpha3`, `langCS`, `langDE`, `langEN'
at line 34:
These are lines 34 and 35:
34 INSERT INTO `countries` (`code`, `alpha2`, `alpha3`, `langCS`, `langDE`, `langEN`,
`langES`, `langFR`, `langIT`, `langNL`) VALUES
35 (4, 'AF', 'AFG', 'Afghanistán', 'Afghanistan', 'Afghanistan', 'Afganistán',
'Afghanistan', 'Afghanistan', 'Afghanistan'),
Related
The full error code is:
Error Code: 1064. 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 ''CPU', 'null', '0', '0', '0'")' at line 2
I'm trying to add product data from my PHP webpage, and getting this error. So I tried doing it manually on MySQL and I get the same error.
Here is a copy of the database and the code that I'm using to insert data:
INSERT INTO store_db.components (product_name, price, description, manufacturer, socket, date_added, type, form_factor, expansion_slots, sata_ports, capacity)
VALUES ('Intel Core i7 4790K','250',
'agsdfg sdfg sdfg sdfg sdfg ','Intel','LGA 1150',now()),
'CPU', 'null', '0', '0', '0'");
You have an extra parenthesis:
now()), 'CPU',
should be
now(), 'CPU',
You have a redundant ) after now(). Get rid of it and you should be OK:
INSERT INTO store_db.components (product_name, price, description, manufacturer, socket, date_added, type, form_factor, expansion_slots, sata_ports, capacity) VALUES ('Intel Core i7 4790K','250','agsdfg sdfg sdfg sdfg sdfg ','Intel','LGA 1150',now(), 'CPU', 'null', '0', '0', '0');
Update: This issue was resolved in the comments and is awaiting an answer
When executing the following query in PyMySQL, I receive the error ‘1064, u"You have an error in your SQL syntax;’ (full error message below)
INSERT INTO `table_name` (`id`, `colName1`, `colName2`, `colName3`)
VALUES (820, 'string', 5, 'N')
ON DUPLICATE KEY UPDATE
`colName1`=VALUES(`colName1`),
`colName2`=VALUES(`colName2`),
`colName3`=VALUES(`colName3`)
-- Tried with and without ` surrounding column names after ON DUPLICATE KEY UPDATE
Syntax in Python:
sql = "INSERT INTO `table_name` (`id`, `colName1`, `colName2`, `colName3`) VALUES (820, 'string', 5, 'N') ON DUPLICATE KEY UPDATE `colName1`=VALUES(`colName1`),`colName2`=VALUES(`colName2`),`colName3`=VALUES(`colName3`)"
I received this error both when trying to update many rows (as suggested elsewhere, using the format of the accepted answer), and for one row at a time.
As noted in the comments, the stripped down example didn't produce an error (although it was not executed with Python). The following is the actual query. Let me know if other information is needed.
Query string executed in Python:
INSERT INTO `Listings` (`id`, `row_last_updated`, `maxBidCountPreRsvMet`, `maxBidPreRsvMet`, `minBidCountPostRsvMet`, `occupancy_status`, `waitingForHnb`, `ownItNow_price`, `high_bid`, `prop_id`, `hot_property`, `status`, `end_date`, `reserve_met`, `hours`, `backupBidSet`, `listing_type`, `low_bid`, `winning_bid`, `bids`, `days`, `high_bid_updated`, `minutes`, `lowBidIsOpt1`) VALUES (820, '2018-01-28 19:16:02', '5', '234000', None, 'N', None, 0, 234000, u'9007092665103', 'N', 'New', '2018-1-31-22-0', 'N', 7, 0, 'AUCN', 0, 0, 5, 3, '2018-01-28 14:16:02.001906', 44, 0) ON DUPLICATE KEY UPDATE `row_last_updated`=VALUES(`row_last_updated`),`maxBidCountPreRsvMet`=VALUES(`maxBidCountPreRsvMet`),`maxBidPreRsvMet`=VALUES(`maxBidPreRsvMet`),`minBidCountPostRsvMet`=VALUES(`minBidCountPostRsvMet`),`occupancy_status`=VALUES(`occupancy_status`),`waitingForHnb`=VALUES(`waitingForHnb`),`ownItNow_price`=VALUES(`ownItNow_price`),`high_bid`=VALUES(`high_bid`),`prop_id`=VALUES(`prop_id`),`hot_property`=VALUES(`hot_property`),`status`=VALUES(`status`),`end_date`=VALUES(`end_date`),`reserve_met`=VALUES(`reserve_met`),`hours`=VALUES(`hours`),`backupBidSet`=VALUES(`backupBidSet`),`listing_type`=VALUES(`listing_type`),`low_bid`=VALUES(`low_bid`),`winning_bid`=VALUES(`winning_bid`),`bids`=VALUES(`bids`),`days`=VALUES(`days`),`high_bid_updated`=VALUES(`high_bid_updated`),`minutes`=VALUES(`minutes`),`lowBidIsOpt1`=VALUES(`lowBidIsOpt1`)
The full error message:
(1064, u"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 ''9007092665103', 'N', 'New', '2018-1-31-22-0', 'N', 7, 0, 'AUCN', 0, 0, 5, 3, '2' at line 1")
Side notes: Python None values are converted to MySQL null values. The same error occurred when changing None values to strings.
I'm using import to add rows to a table. I have almost 600 to add but working with just the 2 below for now. My file:
INSERT INTO `wp_wlm_userlevel_options` ( `userlevel_id`, `option_name`, `option_value`, `autoload`) VALUES ( '341', registration_date', '2017-11-30 16:14:43#0', 'yes' ),( '341', 'transaction_id', 'WL-6806-1506616728', 'yes' );
There is an auto generated value (ID) before userlevel_id. I've tried including it with same results. Also tried removing "16:14:43#0" same results.
Full response***********************
Error
Static analysis:
2 errors were found during analysis.
Unexpected character. (near ":" at position 153)
Unexpected character. (near ":" at position 156)
SQL query:
INSERT INTO `wp_wlm_userlevel_options` ( `userlevel_id`, `option_name`, `option_value`, `autoload`) VALUES ( '341', registration_date', '2017-11-30 16:14:43#0', 'yes' ), ( '341', 'transaction_id', 'WL-6806-1506616728', 'yes' )
MySQL said: Documentation
#
1064 - 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 '', '2017-11-30 16:14:43#0', 'yes' ), ( '341',
'transaction_id', 'WL-6806-1506' at line 2)
Any help appreciated!! Thanks
I'm trying to write a SQL INSERT statement (deliberately not using ActiveRecord), to eventually insert a large number of rows in one go.
inserts.push "('#{x}', #{p}, '#{day.strftime("%Y-%m-%d")}', #{student.id}, '#{created_at}', '#{created_at}', '#{session}')"
sql = "INSERT INTO attendance_marks (mark_code, present, date_recorded, student_id, created_at, updated_at, sess) VALUES #{inserts.join(", ")}"
conn = ActiveRecord::Base.connection
conn.execute sql
But I get an error because the value of x is "\". How do I get around this and successfully insert the backslash character into the MySQL DB?
It feels like it should be simple, and it probably is, but I've got to my wit's end trying to work it out.
Error message:
>> Attendance.parse_attendance_string Student.find(1), "\\", DateTime.new(2012,9,1)
Student Load (0.7ms) SELECT `students`.* FROM `students` WHERE `students`.`id` = 1 LIMIT 1
(0.4ms) INSERT INTO attendance_marks (mark_code, present, date_recorded, student_id, created_at, updated_at, sess) VALUES ('\', 1, '2012-09-01', 1, '2012-08-03 15:58:19', '2012-08-03 15:58:19', 'AM')
ActiveRecord::StatementInvalid: Mysql2::Error: 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 '2012-09-01', 1, '2012-08-03 15:58:19', '2012-08-03 15:58:19', 'AM')' at line 1: INSERT INTO attendance_marks (mark_code, present, date_recorded, student_id, created_at, updated_at, sess) VALUES ('\', 1, '2012-09-01', 1, '2012-08-03 15:58:19', '2012-08-03 15:58:19', 'AM')
from /Library/Ruby/Gems/1.8/gems/activerecord-3.2.0/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:233:in `query'
from /Library/Ruby/Gems/1.8/gems/activerecord-3.2.0/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:233:in `execute'
from /Library/Ruby/Gems/1.8/gems/activerecord-3.2.0/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `log'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.2.0/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /Library/Ruby/Gems/1.8/gems/activerecord-3.2.0/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
from /Library/Ruby/Gems/1.8/gems/activerecord-3.2.0/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:233:in `execute'
from /Library/Ruby/Gems/1.8/gems/activerecord-3.2.0/lib/active_record/connection_adapters/mysql2_adapter.rb:214:in `execute'
from /Users/mike.campbell/projects/cpoms_messy/app/models/attendance.rb:76:in `parse_attendance_string'
from (irb):115
>>
Rather than sanitize the inputs, deal with backslashes, and join the query all on your own, I would recommend checking out ActiveRecord import. It provides a Gem that lets you semantically create bulk inserts using this syntax:
books = []
10.times do |i|
books << Book.new(:name => "book #{i}")
end
Book.import books
Why does the following query fails on MySql?
NHibernate: INSERT INTO UserSetting (userId, key, value) VALUES (?p0, ?p1, ?p2);
?p0 = ccda78da-689d-4d86-ba72-d65eaf281edf [Type: Guid (0)], ?p1 = 'Hello' [Type: String (5)], ?p2 = 'World' [Type: String (5)]
With this error:
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 'key, value) VALUES ('ccda78da-689d-4d86-ba72-d65eaf281edf', 'Hello', 'World')' at line 1"}
key is a reserved word.
Try this:
INSERT INTO UserSetting (userId, `key`, value) VALUES (?p0, ?p1, ?p2)
Key is a reserved word, use ticks, example in link
http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html