I know how to upload images to my database but how can I make a longblob's/blob's default an image from my computer instead of just being null?
The only way to set DEFAULT property to BLOB column (except NULL, of course) is to specify it as an expression.
If you want to set DEFAULT value as some image, then you may:
Load this image into BLOB column. For example
CREATE temptable (blob_column BLOB)
SELECT LOAD_FILE('image_filename') AS blob_column;
Convert loaded binary value to some textual representation which can be typed in the table definition. For example, use HEX() function
SELECT HEX(blob_column)
FROM temptable;
Long string literal will be obtained in the output.
Use obtained literal (copy-paste, avoid excess linebreaks/spaces/truncation/etc.) in the expression of the DEFAULT value of the BLOB column. Apply the expression which reverses the convertion applied at the step 2. Like
CREATE TABLE tablename ( ...
blob_column_name BLOB DEFAULT (UNHEX('long HEX literal')),
... );
Pay attention to the parenthesis which wraps the expression (UNHEX() function in shown example) - they're compulsory! Single DEFAULT UNHEX('long HEX literal') will produce syntax error.
Related
I have a database where there are 3 tables with 3 columns which are AES_ENCRYPTed.
One table has a column which is a VARCHAR(255) with charset latin1. It contains some text encrypted.
Applying the AES_DECRYPT with an UNHEX of the column value shows the output of the real value properly.
Another table has a column which is a mediumblob with charset utf8. It contains some large text encrypted.
Applying just the AES_DECRYPT and then casting it to char displays the original value properly too.
But the 3rd table has a column which is also a mediumblob but charset latin1. It contains a large JSON data stringified.
Now when I apply just the AES_DECRYPT('<column_name>', '') it outputs null. Applying unhex or casting the column to other types before the decryption did not do anything.
For the first 2 tables, just applying the AES_DECRYPT without the conversion also output something.
But the 3rd table does not output anything; just shows NULL.
Any idea what is happening here? It would be very helpful if someone with DB expertise can point me to the right direction why the output is NULL and what needs to be done in the query to get the real output.
EDIT:
The DB Columns are populated by a microservice which uses JAVA Hibernate ColumnTransformer for doing the write and read.
write = AES_ENCRYPT(, ), read = AES_DECRYPT(, )
The values posted via this is also returned properly in the GET response. But the same query does not output the 3rd column value and print NULL as described.
With this structure :
And this command :
UPDATE encryption SET `encrypted` = AES_encrypt(CONVERT(`json` USING latin1), "key");
UPDATE encryption SET `decrypted` = AES_decrypt(encrypted, "key");
This works well for me.
However blobs doesn't any character sets...
Should I exec directly (pseudo code)...
q = "CREATE TABLE `usermood` { `id` INT, `name` TEXT, `mood` VARCHAR DEFAULT 'gloomy' }";
exec(q);
...or bind to a (un)named placeholder?
q = "CREATE TABLE `usermood` { `id` INT, `name` TEXT, `mood` VARCHAR DEFAULT :mood }";
prepare(q);
bind(q, ":mood", 'gloomy');
exec(q);
I've never seen it in any example code.
It's less about the security of escaping (because I control the create statements) but rather about converting the value into a database compatible format (automatic selection of content representation by type).
I'm using MySQL as well as SQLite3.
Are there database drivers that don't support binding in create statements?
If anyone is interested: I'm using QSqlQuery with QVariant as value.
You would use parameter binding when:
You are using a value from an unknown source, and you want to protect against SQL injection.
You want to prepare a statement and execute it repeatedly using different values for the parameter.
Neither of these is likely for your CREATE TABLE example.
I have never used a parameter in any DDL statement.
P.S.: You can't set a DEFAULT for a TEXT column regardless of whether it's a bound parameter or a literal value in the DDL statement, but I'm guessing your example above is artificial.
SQLite explicitly forbids binding default values:
from the SQLite docs:
An explicit DEFAULT clause may specify that the default value is NULL, a string constant, a blob constant, a signed-number, or any constant expression enclosed in parentheses. A default value may also be one of the special case-independent keywords CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP. For the purposes of the DEFAULT clause, an expression is considered constant if it does contains no sub-queries, column or table references, bound parameters, or string literals enclosed in double-quotes instead of single-quotes.
(emphasis by me)
I dont usually use MySQL workbench, I am trying to create a table with a text column by choosing TEXT(), however I am not sure what value I should put in between the '()'. If I leave it blank it gives me the error:
The given data type
TEXT()
contains errors and cannot be accepted. The previous value is kept instead.
I can put a number inside the parentheses but I wanted to know how it affects the column.
I could not find any answers online. I know how it affects things such as DATETIME() but not this data type.
In TEXT(M) M is an optional length. From the docs:
An optional length M can be given for this type. If this is done,
MySQL creates the column as the smallest TEXT type large enough to
hold values M characters long.
So you can use it to specify the maximum number of characters you anticipate for the field.
If you omit the parens - just TEXT - it will default to 65,535. Otherwise, if you supply the parens and a value it will default to the appropriate text type (TINYTEXT, TEXT, MEDIUMTEXT, LONGTEXT).
See here for a comparison with VARCHAR. One difference is that you cannot directly index a TEXT field, it requires a prefix.
I had to bring in a whole bunch of tables from CSV files. A lot of these files had column that were INT but had null values. To speed up the import I just made all of the column VARCHAR. Now I have all this data in the tables but need to change the type. I'm able to do this in the MySQL workbench except for one problem -- It error's because of the null/blank values. Is there some sort of SQL magic that will allow me to convert these column types and ignore the nulls or replace them with the correct 'null value' for that data type?
You can update the columns to set blank fields as NULL as follows:
UPDATE mytable SET mycolumn=NULL WHERE TRIM(mycolumn,' ')='';
Then do your normal table alters as follows:
ALTER TABLE mytable MODIFY mycolumn VARCHAR(255);
The 'DEFAULT NULL' is optional as fields by default allow null. This should allow you to convert the columns to whatever data types you wish without any problem except in the case where there is mixed data -- such as numbers, and strings, and you wish to make that column FLOAT.
The above example also does not take into account removing carriage returns, etc, in the event that a column contains a "\n" or "\r\n" and nothing else, it will not set it to NULL, but you can modify the "TRIM(mycolumn, ' ')" to meet those requirements if you have them : aka ...
UPDATE mytable SET mycolumn=NULL WHERE TRIM(mycolumn,"\n")='';
I have a function that post on a users facebook wall.
One thing that I send to facebook is some text that I get from my mysql table that is set to LONGTEXT. If I have the table set as LONGTEXT then the text is not send to facebook, but if I set the table to VARCHAR then the text is send to facebook!
So how can I convert the LONGTEXT value that I get so it becomes like a VARCHAR value, before I send it to facebook?
I use the table in many other places so I just cant convert the table itself to VARCHAR, its to much work! I have to convert the output instead.
Any input appreciated thanks!
In mysql you can do:
SELECT ID, CAST(YourLongText as char(255)) AS YourVarchar FROM some_table
Did you mean like that
use cast as in the post from #Sudhir or use the mysql function convert:
http://dev.mysql.com/doc/refman/5.1/de/charset-convert.html
use the CONVERT or CAST functions: http://www.geeksengine.com/database/single-row-functions/conversion-functions.php
CONVERT(expr,type)
In this form, CONVERT takes a value in the form of expr and converts it to a value of type.
CAST(expr AS type)
Using CAST() function is the same as using CONVERT() function except that it uses keyword AS in between expr and type rather than a comma.