MySQL can't replace new line characters - mysql

I try to remove new line symbols from text type field, statement I use - below, tried all statements I found here with no luck:
UPDATE `ae1_jshopping_products`
SET `short_description_lt-LT` = REPLACE(`short_description_lt-LT`, '\r\n', ' ');
I also attach print with error messages.
Thanks in advance!

put your fieldname short_description_lt-LT into backticks:
UPDATE `ae1_jshopping_products`
SET `short_description_lt-LT` = REPLACE(`short_description_lt-LT`, '\r\n',' ');
explanation: MySQL can't know if you want to subtract LT FROM short_description_lt or if - belongs to your fieldname.

I can't reproduce the problem. Perhaps there is a hidden character that may be causing the problem.
Using:
MariaDB: 10.0.22
phpMyAdmin: 4.6.0

Your outdated phpMyAdmin version is affected by a bug with the SQL parsing library where it thinks your statement is incorrect. You should be able to solve it by upgrading to a more recent version (4.6.1 is expected to be released within a week).

Related

MySQL REPLACE in UPDATE does not work properly

The following query:
select replace(`Abilities`, 'export_import', 'auto') from fl_account_types;
gives me 'auto,listings' correct replacement from Abilities column. However, when I execute:
update fl_account_types set `Abilities` = replace(`Abilities`, 'export_import', 'autos');
MySQL just omits 'export_import' string and replaces Abilities with 'listings' string.
What could be the reason?
The problem was that Abilities was of type SET and I was trying to replace with a value which was not listed in a definition of it. But I still do not understand why select replace works well and why MySQL do not throw an error.

What is the correct syntax for a Regex find-and-replace using REGEXP_REPLACE in MariaDB?

I need to run a regex find-and-replace against a column named message in a MySQL table named post.
My database is running MariaDB 10.
According to the docs, MariaDB 10 has a new REGEXP_REPLACE function designed to do exactly this, but I can't seem to figure out the actual syntax.
It will affect 280,000 rows, so ideally there's also a way to limit it to only changing one specific row at a time while I'm testing it, or simply doing a SELECT rather than an UPDATE until I'm sure it does what I want.
The regex I want to run:
\[quote\sauthor=(.+)\slink=[^\]]+]
The replacement string:
[quote="$1"]
The following was what I tried, but it just throws a SQL error:
UPDATE post SET message = REGEXP_REPLACE(message, '\[quote\sauthor=(.+)\slink=[^\]]+]', '[quote="$1"]') WHERE post_id = 12
In this case, the original message was:
[quote author=Jon_doe link=board=2;threadid=125;start=40#msg1206 date=1065088] and the end result should be [quote="Jon_doe"]
What is the proper syntax to make this REGEXP_REPLACE work?
You have to do a lot of escaping here:
REGEXP_REPLACE(message, "\\[quote\\sauthor=(.+)\\slink=[^\\]]+]", "\\[quote=\"\\1\"\\]")
Please note that you have to reference the Group by \\1

Ruby SQL Insert using Mysql2 gem

I'm trying to insert into a remote mysql database. I am able to connect correctly and can query 'select' no problem from it. However, I cannot perform inserts into the same table that I can select from. I suspect it has something to do with my binds, but this is nearly identical to what I was using to get sqlite3 working which I think uses the same Arel to insert.
#result = #db.query("insert into lead_to_processes (case_number, style_of_case) values (?,?)", [
self.case_number.to_blob.force_encoding("UTF-8"),
self.style_of_case.to_blob.force_encoding("UTF-8")
]
)
Ultimate goal is to be able query a remote database from inside of a model and insert data into it. I've tried using Octopus and that didn't quite work because the tables will be different from the databases.
I have full permissions with this user on the database.
So following guidance from comments i changed the syntax and am getting a different error
Mysql2::Error: You have an error in your SQL syntax;
However i'm doing the query like this now
#db = Mysql2::Client.new(connectionstring)
#case_number = #db.escape(self.case_number)
#style_of_case = #db.escape(self.style_of_case)
#db.query("insert into lead_to_processes (case_number, style_of_case) VALUES
(#{#case_number}, #{#style_of_case})
Any ideas or guidance? I've also tried this with '' encapsulating the variables that i'm inserting
I guess there were some weird characters in my code so I had to force UTF-8 encoding and then removed the characters using gsub below, everything is flowing now.
Thanks for the advice
#db.escape(self.style_of_case.force_encoding("UTF-8"))
#db.escape(self.case_number.gsub(/[\xC2]/,'').gsub(/[\xA0]/,'').force_encoding("UTF-8"))
Is it possible that you are missing an end quote?
this
#db.query("insert into lead_to_processes (case_number, style_of_case) VALUES
(#{#case_number}, #{#style_of_case})
should be
#db.query("insert into lead_to_processes (case_number, style_of_case) VALUES
(#{#case_number}, #{#style_of_case}") <== notice the quote at the end.

Entity Framework : Set MySQL custom environment variables

I have an issue with Entity Framework 5.0. I'm working with Silverlight 5 and MySQL 5.6 too.
I need to set an environment MySQL variable before each connexion to the MySQL server.
E.g
SET #my_var = 'test';
Under Mysql I don't have any issues.
The following raises an EntityFrameworkException (syntax error near '#').
this.ObjectContext.CreateQuery<object>(" SET #my_var = 'test' ");
OR
this.ObjectContext.CreateQuery<object>(" CALL set_my_var('test') ");
This last method raises a MySQLException saying that a DataReader is already open and need to be closed.
this.ObjectContext.ExecuteStoreQuery<object>(" CALL set_my_var('test') ", null);
I also tried to set a MySQL system environment (no '#') with the same result every time.
Any help will be much appreciated !
Thank you.
I tried so many things that I misspelled my variable in my code.
So the following finaly worked : ctx.ExecuteStoreCommand("SET #my_var = 'test'");
I decided to leave the instruction in the method Initialize of my domain service. This method is inherited of the LinqToEntitiesDomainService class.
But you need to set Allow User Variables=True in your MySQL connection string
(ref : Is it possible to use a MySql User Defined Variable in a .NET MySqlCommand?)
You simply need to use a recent version of the MySQL Connector because older versions use the '#' mark to define SQL parameters so it could conflict with custom variables. Now it uses the '?' mark : http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples-mysqlcommand.html
My library was already up to date (6.6.5).
Thank you for the help !
Since your statement is not a query (i.e. does not return any result) you should use ExecuteStoreCommand. Something like this should work:
ctx.ExecuteStoreCommand("SET #my_var = 'test'")

MySQL Update disallowed header names?

To save tearing any more of my hair out I thought I'd just pose the question here, as I'm having an infuriating time with PHP Mysql UPDATE: something I use quite a lot and thought I understood!
Basically, are there any table header names that are known to break MySQL update functions? I have an Update mysql_query function that works perfectly, for example:
UPDATE table_name SET
part_number='000 - New Product',
product_code='1',
barcode_ref='1',
type='new type'
WHERE id='999'
However, if I include the table header called 'trigger' in the code it breaks it!
UPDATE table_name SET
part_number='000 - New Product',
trigger='YES',
product_code='1',
barcode_ref='1',
type='new type'
WHERE id='999'
The above sql returns an error of: **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
'trigger='YES', part_number='000 - New Product', product_code='1', barcod' at line 1**
I have made an identical duplicate of the column called 'trigger' and re-named it 'testing_t' and immediately everything works perfectly as before. I've tried both dumping my mysql_real_escape_string to variables for use in the UPDATE command and doing them inline, and even tried hard-coding the string and it still breaks.
Can anyone shed any light on this? Ideally I'd really like to not have to change my table header name, as I don't want to modify the references to it across the site. Obviously if there is no other option then I will, but I'm hoping I'm just being stupid and that someone can explain why it's happening/how to stop it happening!
Thanks in advance,
Joe
use this query
UPDATE table_name SET
`part_number`='000 - New Product',
`trigger`='YES',
`product_code`='1',
`barcode_ref`='1',
`type`='new type'
WHERE id='999'
your query will fail because you have written trigger without ``
trigger is reserved word in mysql for creating triggers.
To use reserved word as column name you have to write that word inside ``
trigger is a mySQL keyword. If you enclose all column names in `` you will be safe. It's good practice to not use these keywords as column names.
Other keywords are here http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html
mySQL has a series of reserved words, of which TRIGGER is one. Consult this list of words as a guide for what NOT to call your columns/tables:
http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html