Storing deflated HTML in MySQL - html

I need to store HTML data in a MySQL database. I read about this and found that the best method is to use NVARCHAR or VARCHAR. Furthermore I'd like to compress the input to make it less space consuming. I use PHP's gzdeflate() function for deflating the HTML input, but in this case what MySQL data type should I use?
EDIT: Since I need to store quite big HTML sources I decided to go with the TEXT data type but the question: Is MySQL's TEXT field compatible with a deflated HTML string?

Use BLOB type instead of TEXT to use it for binary data.

Related

Varchar or Blob object for very large string? in Mysql through Eclipselink

I have an application where I am going to store the JSON string in MySql database through Eclipselink JPA.
The JSON string can be of any length. Most of the time a String from a JSON file of length around 200 to 300 lines.
What is the best way to store the string? To use varchar or Blob?
Please provide an example if any.
You should not save it as a BLOB, as it is primarily used for image data or other binary data... Use Varchar() or use TEXT which has a size of 65535 characters if you are unsure about how many characters you might need to store..
There was a thread previously discussing WHEN to use varchar or text: Thread
To store text - use a TEXT column (or even a LONGTEXT), blobs are for binary.
Also if you're on Mysql 5.7+ - there's now a JSON data type, which is checked for being a correct json, stored more efficiently and have pretty manipulation methods

What mysql data type should I use for encrypted text?

Here's the type of text my encryption function throws out:
I generated several strings and they're never bigger than 50 characters, but I would like to give it 75 characters in mysql. I tried using varchar, but the string gets cut off because it doesn't like some characters. Any idea what data type I should use?
If it's binary data (it looks like it is), you probably should store it in a BLOB.
You can use a blob, but for short data, that will make your selects slow.
Use binary(75) or varbinary(75).

Insert a Coldfusion struct into a database

If I wanted to save a contact form submission to the database, how can I insert the form scope in as the submission? It's been some time since I used Coldfusion.
The contact forms vary depending on what part of the site it was submitted from, so it needs to scale and handle a form with 5 fields or one with 10 fields. I just want to store the data in a blob table.
Most space efficient way and least complicated to turn back into original shape is using serializeJSON. After that, you can use something like key:value|key:value, or XML representation of your struct.
Cfwddx is also an alternative.
I don't know that there is a way to store a native structure into a database, but have you thought about using JSON to represent your object as key-pair values and then parsing it into a native structure after retrieving it from the database?
There are tags/functions out there that will help you with the encoding and decoding into JSON:
cfJSON Tag
CF8 Serialize JSON
CF8 Deserialize JSON
If you can't normalize the form fields into proper table(s), you can try storing them:
in XML (SQL Server supports XML pretty well), or
in JSON (in a plain varchar field), or
ObjectLoad() & ObjectSave() (CF9 only) to store as blob.
IIRC there are ways to get object load/save functionality in pre-CF9 by tapping into Java. http://www.riaforge.org/ or http://cflib.org/ might have it.

rails mysql BLOB type - to_xml returns binary, would like to force to string

I am running RoR, Ruby 1.8.6/Rails 2.3.2.
I am persisting a String within a model object to MySQL to a Blob field. For a number of reasons, I have to use MySQL Blob data type instead of a Text data type, yet I want Rails to treat the field as a regular String (instead of a binary String - which is what I think it is doing?).
How can I force this? Everything works well in the display of the pages, the main issue is XML that is being produced.
A specific example is a "description" field of type Blob in MySQL displays perfectly in the HTML ERB, but when using the to_xml method on that model object the resulting XML is:
<description type="binary" encoding="base64">
VGhpcyB0d28tc2NyZWVuIGZvcm0gYWxsb3dzIGJ1c2luZXNzIGN1c3RvbWVy
cyB0byByYXRlIHRoZWlyIGxldmVsIG9mIHNhdGlzZmFjdGlvbiBhbmQgcmVx
dWVzdCB0byBzcGVhayB3aXRoIG1hbmFnZW1lbnQuIEl0IGlzIGVzcGVjaWFs
bHkgZGVzaWduZWQgZm9yIHRoZSBDb21taXNzaW9uIG9uIFZvY2F0aW9uYWwg
UmVoYWJpbGl0YXRpb24u
</description>
I just want it to appear be formatted as a normal string field like:
<description>test description</description>
What are the best ways to force this? I've been searching and reading through docs, but haven't been finding any helpful suggestions.
I appreciate any and all help,
thank you
I ended up monkey patching the XmlSerializer to treat binary data as text. I will never have to send binary data down in pure binary form via XML, so this was a valid option - not perfect, but in doing research couldn't find any way to override the default column settings.

Convert blob to text in a mysql export

I'd have some blob data such as:
0x3333332c2044e963617269652c20356520e9746167650d0a53742d4c617572656e7420285175e9626563292048344e20334d390d0a
that I'd like to convert to text because the new database has text field instead of blobs and now it makes trouble with some accentuated characters.
Is there somekind of blob to string converter somewhere?
Thanks a lot!
Try:
CONVERT(blobname USING latin1)
It depends on what the blob is. For example, I've dealt with some blobs that could be represented as basic XML files. Those would have been relatively easy to convert. However, I dealt with other blobs that were image files. If you tried to represent them as text you'd lose data.
What are in your blobs?
Create your new database with your export, once done create your text column on the table, update that using a CONVERT drop the old column, renaming the old one if required.
However if the data contains simple byte stream (that is, unstructured data, files, audio, video, whatever) and you need to represent them as pure ASCII you could change into a Base64 string.
If using phpmyadmin, tick the box that says "Dump binary columns in hexadecimal notation (for example, "abc" becomes 0x616263)" at the bottom of the export page.