Replace a word in BLOB text by MySQL - mysql

I've got a huge mysql table (called tcountriesnew) and a column (called slogen, blob type).
In each of those slogen blobs I'd like to replace a word, for example: banana to apple.
Unfortunately I tried to print all the rows with word banana, and it did not work.
select * from tcountriesnew where slogen like '%banana%';
Please help me.
What i missed, what is the problem with my query?
How can i replace text in blob?

Depends what you mean by "replace"; using replace to show modified text in select:
select replace(slogen, 'bananas', 'apples') from tcountriesnew where slogen like '%bananas%';
Or update data in a table:
update tcountriesnew set slogen=replace(slogen, 'bananas', 'apples') where slogen like '%bananas%';
BTW. Why are you using blob for text? You should use text type for textual data and blob for binary data.

In some cases it is needed to save texts BLOB. In my case, for whatever reason Drupal 7 developers choose to use a blob for all TEXT columns - this is out of developer's control.
To convert a blob to text, use the MySql convert function. Then you have to save it into the database and convert it again to blob - but this is handled automatically by MySQL. So the following query would do the trick:
UPDATE tcountriesnew
SET slogen = replace(CONVERT(slogen USING utf8), 'bananas', 'apples')
WHERE slogen LIKE '%bananas%';
On MySQL 5.5, This resolved my issue completely.
Also, configure your PhpMyAdmin to show Blob data

Which version are you using ? Maybe it's this bug : http://bugs.mysql.com/bug.php?id=27.
Otherwise try to cast your blob column.

Related

Decode Base64 column in mysql shows BLOB [duplicate]

I keep finding that MySQL Workbench shows query results as BLOB. e.g:
SELECT INET_NTOA(167773449) --> BLOB
If I select to 'view value' I can determine the text value is '10.0.5.9' but it's pretty irritating when I SELECT multiple rows and want to glance at the contents.
Is there a way around this or is it a limitation of the tool?
Background:
This problem occurs when the binary string values (BINARY/VARBINARY type) are returned in the results. The binary strings contain the zero bytes and for some reason, apparently security, have not been shown by default. More details about binary strings here.
Even in the reported example SELECT INET_NTOA(167773449), the function returns binary string. Check this for reference.
Solution:
Since MySQL Workbench v5.2.22, it can be set through preferences whether to SHOW or HIDE such values.
In MySQL Workbench, go to: "Edit -> Preferences... -> SQL Queries" OR "Edit -> Preferences... -> SQL Editor -> SQL Execution" (depending upon what version of Workbench you have).
Check the option 'Treat BINARY/VARBINARY as nonbinary character string' to show the actual value.
Reference:
The original issue has been reported and answered with fix here.
What you can do is Cast your BLOB type to a string. This will simply allow you to glance at whats in your BLOB type when browsing your select statement.
SELECT CAST(`blob_column_name` AS CHAR(10000) CHARACTER SET utf8) FROM `table_name`;
Another (slightly shorter) solution
SELECT CONVERT(FROM_BASE64(myMEDIUMTEXTcol) using utf8) notAblobject FROM myTable;
Unrelated background
My table contains a MEDIUMTEXT column which stores a Base64 string. In my frontend JS code I kept getting an Object returned instead of a string. The Object appeared to contain an array of ASCII values.
Testing the associated query in phpMyAdmin indicated the result was a BLOB. I updated my SQL query, as above, in order to get a simple string returned instead of an Object.

I have a text file that contains non english word and i need to put it into mysql how can i?

I have a text file that contains non english word and i need to put it into mysql how can i ?
203851 ኣብ
70351 ናይ
56687 ካብ
46018 እቲ
41928 ምስ
40221 ከም
38702 ድማ
29739 ናብ
28806 እዩ
23066 ከኣ
21459 ግን
21013 እዚ
20638 ሓደ
If by that you mean how can you insert a non-unicode string the answer is: the same way as you would put any other string. Just be careful when you create the column to select an encoding that supports it (for example utf8-mb4 - that means utf8 on 4 bytes, because the default mysql utf8 only uses 3 and... long story here actually :) - should do the trick for mostly anyhting you want to put there).
Then... INSERT INTO tableName (columnName) VALUES ('yourString');
Important note here: You should convert that string to the encoding you use before (again... utf8 would be your best choice). If you don't do that and respect the next paragraph (check below) you are still fine, but if you don't you will end up with messed up stuff.
Just be careful on data processing unfortunately mysql has so many points of failure regarding encoding. It has 1 encoding for db, 1 for table, 1 for column, 1 for data saving, even 1 for the connection so... just try to use the same for all to avoid surprises.
You have a text file with 2 columns of data? Are they separated by tabs or what?
Use LOAD DATA INFILE with suitable parameters.

MySQLAdmin replace text in a field with percent in text

Using MySQLAdmin. Moved data from Windows server and trying to replace case in urls but not finding the matches. Need slashes as I don't want to replace text in anything but the urls (in post table). I think the %20 are the problem somwhow?
UPDATE table_name SET field = replace(field, '/user%20name/', '/User%20Name/')
The actual string is more like:
https://www.example.com/forum/uploads/user%20name/GFCI%20Stds%20Rev%202006%20.pdf
In a case you are using MariaDB you have REGEXP_REPLACE() function.
But best approach is to dump the table into the file. Open it in a Notepad ++
and run regex replace like specified on a pic:
Pattern is: (https:[\/\w\s\.]+uploads/)(\w+)\%20(\w+)((\/.*)+)
Replace with: $1\u$2\%20\u$3$4
Then import the table again
Hope this help
If its MariaDB, you can do the following:
UPDATE table_name SET field = REGEXP_REPLACE(field, '\/user%20name\/', '\/User%20Name\/');
First, please check, what is actually stored in the database: %20 is a html-entity which represents a whitespace. Usually, when you are storing this inside the database, it will be represented as an actual whitespace (converted before you store it) -> Hence your replace doesn't match the actual data.
The second option that might be possible - depending on what you want to do: You are seeing the URL containing %20, therefore you created your database records (which you would like to fetch) with that additional %20 - And when you now try to query your results based on the actual url, the %20 is replaced with an "actual" whitespace (before your query) and hence it doesn't match your stored data.

How can I convert BLOB to text in MySQL?

I have a BLOB field in one of tables and I used the following command to convert it to text:
ALTER TABLE mytable
ADD COLUMN field1_new TEXT;
update mytable set
field1_new = CONVERT(field1 USING utf8);
This did not work and gave me some random characters. Like:
9x
This result is returned as a content of message which does not make sense. I changed the character set to 'latin1'. This one gave me a larger sequence of characters yet still something non-sense. For example:
¢xœ}T]k1|/ô?¬Á/‡ZJpMK“–<$„Ô¥ôqO§»ÑI®¤³¹ß...
Is there anyway to figure out what character set the BLOB field is using so that I can convert it to text properly?
Any help with this problem will be much appreciated. Thanks
Edited: I have to also mention that I used CAST command and it returned:
�x�}T]k1|/�?��/��ZJpMK��<$�ԥ�qO���I������������$:���̬�4�...
try using cast:
CAST(field1 AS CHAR(10000) CHARACTER SET utf8)
you can see this post also for more:How do I convert from BLOB to TEXT in MySQL?

Convert text to blob in mysql

I want to convert a "text" field to "blob" in mysql 5. Will the data be affected in any way if I simply run
alter table <table> change <col> <col>
blob;
I tried it and it worked with no problems, I was just wondering if there's something I may be missing or should take special care of.
You shouldn't have any problems. The main differences between blob and text are in the way they are sorted (eg numeric v lexicographic). They hold the same size of data and there doesn't seem to be any fiddling (eg with carriage returns / newlines).
Useful page in the manual
http://dev.mysql.com/doc/refman/5.0/en/blob.html
ALTER TABLE myTableName MODIFY COLUMN columnName BLOB;
Yup you shouldn't face any problem except sorting.
As mysql will convert "text" to binary data when changed to "blob" and vice versa.
you can simply get
String str = resultSet.getString(columnIndex)
// or
byte[] byteArr = resultSet.setBytes(columnIndex).
String str = new String(byteArr );
You can simply make a new dump and compare it against your backup. Use WinMerge, KDiff3 or your tool of choice.