I am using mysql and I'm trying to populate the database in build.xml.
How can I insert a blob file in this table:
CREATE TABLE CONTENT (
idContent varchar(30) not null,
price int,
url blob,
primary key (idContent)
);
I've tried this:
INSERT INTO CONTENT VALUES ("Tecnico.png", 0, LOAD_FILE("src/Tecnico.png"));
but the url return is null, then I tried the entire path to the .png and returned null to.
Can anyone help me please?
MySQL LOAD_FILE() reads a file that is already present on the server, do not uploads the file.
So, if the file is on the server, and the full path is something like "/var/www/[AnotherFolfer]/src/Tecnico.png"
Try, to use:
INSERT INTO CONTENT VALUES ("Tecnico.png", 0, LOAD_FILE('/var/www/[AnotherFolder]/src/Tecnico.png'));
Related
So i have a set of code which will enter specific data from a .sql file into Wamp to use for a database and place it into a table this table has images in it which are from the www folder in wamp aswell as another source folder in my system. calling the images from the www folder works fine but it keeps having the images from the source folder as NULL even though the path is correct.
Here is the table code
create table emp (
Picture LONGBLOB DEFAULT NULL,
Picture_Path varchar(20) DEFAULT NULL
)
Here is the table values
INSERT INTO emp (Picture, picture_path)
VALUES
(load_file('c:/kittens/cat1.jpg'),'/cat1.jpg')
I tried to change make sure i didnt make any mistakes but i am new to SQL so i cant really be sure exactly what i did wrong
edit sorry i changed my original code to show the part i had an issue with there is more code but i forgot to adjust the text to have no comma at the end
I am new to SQL databases and I am learning about the BLOB data type.
My SQL table structure looks like this:
CREATE TABLE test (
NAME VARCHAR(20),
AGE INT,
CITY VARCHAR(20),
FILE BLOB);
I insert values into it using the command line as I was observing the behavior of the BLOB field. According to my knowledge BLOB field encoded the data. But it is not showing me the encoded data when I retrieve all the records. I inserted the following values:
INSERT INTO test VALUES ('Neha', 21, 'Lahore', 'Computer Science');
It is showing me the following output:
Please help me how this is happening. What is the right concept of this.
I am trying to insert an image into a MySQL database however I keep receiving this error;Error Code: 1048.
Column 'image' cannot be null. This is the code I am using.
create table test_img (
id int(10) not null AUTO_INCREMENT PRIMARY KEY,
name varchar(25) not null default '',
image blob not null
)
INSERT INTO test_img(ID,IMAGE) VALUES(1,LOAD_FILE ('C:\\human_centered_design_infographic.jpg'));
From the MySQL manual:
LOAD_FILE(file_name)
Reads the file and returns the file contents as a string. To use this function, the file must be located on the server host, you must specify the full path name to the file, and you must have the FILE privilege. The file must be readable by all and its size less than max_allowed_packet bytes. If the secure_file_priv system variable is set to a nonempty directory name, the file to be loaded must be located in that directory.
If the file does not exist or cannot be read because one of the preceding conditions is not satisfied, the function returns NULL.
You probably get the NULL error because one of those conditions mentioned is applicable.
It's not readable by all or the server does not have the file privilege enabled or the file is larger than max_allowed_packet bytes.
I am writing a file to a MySql table with a blob type.
There are various reasons why I NEED to do this - I cannot just use the filesystem.
I find that writing the file into the db and then reading back from the db, the file is corrupted.
I can replicate this using SQL and nothing else.
The file should fit easily into the LONGBLOB so I do not think the data is being truncated. In fact the data being read back is larger than the data being written.
I have experienced the same issue with BLOB and MEDIUMBLOB too.
Here is the SQL:
CREATE TABLE Video(
Id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
ShopAssistant_Id int NOT NULL,
Data LONGBLOB NOT NULL,
FOREIGN KEY (ShopAssistant_Id) REFERENCES ShopAssistant(id)
);
--insert data from a file
insert into video (shopassistant_id, data) values (1, LOAD_FILE('/Users/tmck/temp/introduction.mp4'));
--read data back into a different file
select data into outfile '/Users/tmck/temp/ret.mp4' from Video;
If we now compare the files, ret.mp4 is bigger than introduction.mp4, the individual bytes have been changed and the file is corrupt and won't play.
The file itself is small, circa 245 KB.
What 'option' in MySql is causing it to convert my data?
Ok, found the answer here
http://ask.metafilter.com/161415/Export-my-Mysql-PDF-binaryblob-into-a-file
The data was being written correctly, and the select into outfile was mangling the data
The soln is to add some escape hints to MYSQL as follows:
select data into outfile '/Users/tmck/temp/ret.mp4' FIELDS TERMINATED BY '' ENCLOSED BY '' ESCAPED BY '' LINES TERMINATED BY '' STARTING BY '' from Video
I use a txt file to upload data into a mysql database.
The txt file I use is the following:
http://www.repubblica.it/
http://www.repubblica.it/minify/sites/repubblica/nazionale/config_01.cache.php?name=site_home_css
http://www.repubblica.it/minify/sites/repubblica/nazionale/config_01.cache.php?name=social_home_css
http://quotidiano.repubblica.it/home?adv=t&source=homerepit
http://www.repubblica.it/servizi/mobile/index.html
http://inchieste.repubblica.it/
http://espresso.repubblica.it/
http://altoadige.gelocal.it/
http://corrierealpi.gelocal.it/
http://gazzettadimantova.gelocal.it/
http://gazzettadimodena.gelocal.it/
http://gazzettadireggio.gelocal.it/
http://mattinopadova.gelocal.it/
http://ilpiccolo.gelocal.it/
http://trentinocorrierealpi.gelocal.it/
http://lacittadisalerno.gelocal.it/
In my java program I use the following mysql code to upload the txt file:
//here I create the table
CREATE TABLE table(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,url VARCHAR(1000) NOT NULL);"
//here the txt is loaded
LOAD DATA LOCAL INFILE \'/tmp/test/url.txt\' INTO TABLE table LINES (url)
All that works fine. I have my table with two columns: id and url.
When i try to search a value in that table using a simple:
SELECT url FROM table WHERE url LIKE 'http://www.repubblica.it/'
or
SELECT url FROM table WHERE url ='http://www.repubblica.it/'
MySQL return an empty result set (i.e. zero rows). ( Query took 0.0004 sec )
Here a screenshot of phpmyadmin:
Why I am not able to seach for my values?
What I am doing wrong?
Thanks in advance
You need use wildcards in your like statment LIKE '%..%' to match a part of a string
SELECT url FROM table WHERE url LIKE '%http://www.repubblica.it/%'
If you try to search directly on phpmysqmin you have an option to search by 'LIKE %..%'
Maybe you have spaces before and after the string, try this:
UPDATE `table`
SET `url`=TRIM(`url`)
Then:
SELECT
`url`
FROM
`table`
WHERE
`url`='http://www.repubblica.it/'