Unexpected character. (near ":" at position 154) - mysql

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

Related

Inserting a substring value into an already existing entry

Currently I have a database with the following parameters and entry:
Now I want to fill the column status_from in this particular entry with the id 1 with a substring of the action column. Which is basically I want to fill my status_from column with a word that comes after from and before to of the action column. To achieve this I've tried using the following statement:
INSERT INTO crm_logs2(status_from) SELECT status_from WHERE id='1' VALUES (substring_index(substring_index(action, 'from', -1),'to', 1))
But doing this gave me the following error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'VALUES (substring_index(substring_index(action, 'from', -1), ...' at line 1
You need to use the UPDATE statement
UPDATE crm_logs2
SET status_from = (SUBSTRING_INDEX(SUBSTRING_INDEX(action, "from", -1), "to", 1))
WHERE id = 1
update crm_logs2
set action=(select SUBSTRING_INDEX(SUBSTRING_INDEX(action, ' ', 6), ' ', -1) from
crm_logs2 where id=1)where id=1
Try this and let us know if it works

Why is this my sql statement wrong in the navicat? [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 6 years ago.
INSERT INTO ry_useraccount
(
'Id',
'UserId',
'FreeAmount',
'PayFrozenAmount',
'WithdrawFrozenAmount',
'CurrentAsset',
'CreateTime',
'UpdateTime'
) VALUES
(
'1',
'52501',
'600',
'0',
'0',
'0',
NOW(),
NOW()
);
If just look at the sql,it is correct,but when I run it,the navicat said
“[SQL]INSERT INTO ry_useraccount('Id', 'UserId', 'FreeAmount',
'PayFrozenAmount', 'WithdrawFrozenAmount', 'CurrentAsset',
'CreateTime', 'UpdateTime') VALUES('1', '52501', '600', '0', '0', '0',
NOW(), NOW()); [Err] 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 ''Id', 'UserId', 'FreeAmount',
'PayFrozenAmount', 'WithdrawFrozenAmount', 'Curren' at line 1”
. I cannot find the reason.I guess that maybe some white space cause the result,but after I delete all the white spaces,it still didn't work.
The single quote are for literal string not for column name (you can use backticks if you need column name with space or with reserved words )
INSERT INTO ry_useraccount(Id, UserId, FreeAmount, PayFrozenAmount, WithdrawFrozenAmount, CurrentAsset, CreateTime, UpdateTime)
VALUES(1, 52501, 600, 0, 0, 0, NOW(), NOW());

mysql union all when creating views

I have the following code, if i run the code as an sql script it works but if i want to create a view from it i got this error: 1166 - Incorrect Column Name ''
select `c`.`package_id` AS `package_id`,
`c`.`student_id` AS `student_id`,
`bs`.`name` AS `stud_name`,
`c`.`payed_date` AS `payed_date`,
(case `c`.`type` when 'e' then 'Vizsgadíj' when 'c' then 'Tanfolyam díj' else '' end) AS `name`,
`c`.`course_price` AS `price`,
`c`.`pay_form` AS `pay_form`,
`c`.`venue_id` AS `venue_id`
from (`bma_student_pays` `c`
join `bma_students` `bs` on((`bs`.`id` = `c`.`student_id`)))
union all
select '0' AS `0`,'0' AS `0`,'' AS ``,`e`.`making_date` AS `making_date`,`e`.`name` AS `name`,`e`.`price` AS `price`,`e`.`type` AS `type`,`e`.`venue_id` AS `venue_id`
from `bma_extra_makings` `e`
union all
select '','','',i.inv_due,i.inv_name,i.inv_amount,i.`mode`,i.venue_id
from bma_invoices i
where i.inv_type='K'
Any help would really good.
Thanks!
In your second query (first UNION ALL), you have as your third column:
'' AS ``
This is not valid - remove the AS clause and all should be well.

Why can't I insert this into MySQL?

I probably made a stupid error on my syntax for the query, but I can't seam to fix it. Here is the query my program tries to execute:
INSERT INTO filez (
filename,
uploadedby,
dateuploaded,
public,
FileSize,
FileTransferSize,
key,
bytes
)
VALUES(
'tacct/tesABCscdsdasdasdD.testtest',
'tacct',
'%27 %December %2012, %7:%32:%15%AM',
1,
7,
7,
'`',
'TestDoc'
)
And here's the mysql_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, bytes) VALUES('tacct/tesABCscdsdasdasdD.testtest', 'tacct', '%27 %D' at line 1
I did mysql_real_escape_string() on EVERYTHING except the FileSize and FileTransferSize in the query. Could you tell me what I am doing wrong? Thanks!
The error tells you that the error happens at the text key, and it's quite correct: key is a reserved word in MySQL.
Where you use it as a field name, you must enclose it in backticks (`); it's a good general practice anyway.
So:
INSERT INTO `filez` (
`filename`,
`uploadedby`,
`dateuploaded`,
`public`,
`FileSize`,
`FileTransferSize`,
`key`,
`bytes`
)
VALUES(
'tommy3244/tesABCscdsdasdasdD.testtest',
'tommy3244',
'%27 %December %2012, %7:%32:%15%AM',
1,
7,
7,
'`',
'TestDoc'
)
key is a reserved word and you can not use it in column name without using backtics. try this
INSERT INTO filez (`filename`, `uploadedby`, `dateuploaded`, `public`, `FileSize`, `FileTransferSize`, `key`, `bytes`) VALUES('tommy3244/tesABCscdsdasdasdD.testtest', 'tommy3244', '%27 %December %2012, %7:%32:%15%AM', 1, 7, 7, '`', 'TestDoc')
try using following query
INSERT INTO filez (filename, uploadedby, dateuploaded, public, FileSize, FileTransferSize, `key`, bytes) VALUES
('tommy3244/tesABCscdsdasdasdD.testtest', 'tommy3244', '%27 %December %2012, %7:%32:%15%AM',
1, 7, 7, '`', 'TestDoc')
key is keyword so you have to use backticks as i used above.
For more info check following question too
Select a column with a keyword name
INSERT INTO `filez` (`filename`, `uploadedby`, `dateuploaded`,
`public`, `FileSize`, `FileTransferSize`,
`key`, `bytes`)
VALUES('tommy3244/tesABCscdsdasdasdD.testtest', 'tommy3244',
'%27 %December %2012, %7:%32:%15%AM', 1, 7, 7, '`', 'TestDoc')
ok,
syntax to use near 'key, bytes) both are reserve words, Good practice is always use ` sign for user defined identifiers.
KEY is reserved key word for mysql
try other column name or make backticks like that ``
The issue is key is the reserved keyword of mysql , you can't use it as column name, if you really want then enclose it inside backtick character
key

Backslash in MySQL INSERT statement - Ruby on Rails

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