mysql using the command line client- Image Loading - mysql

I am trying to load an image into mysql using the command line client and following is the the code that i have been using;
INSERT INTO AutomobileParts (Part_ID, Part_Name,Img_Path)
VALUES (
"101AA",
"BikePanel",
load_file("F:/PYQT Projects/bikepanel.jpg")
) WHERE i=1;
can someone please help me out in understanding as in where i am going wrong on entering this piece of code.

MySQL LOAD_FILE() reads the file and returns the file contents as a string.
That will probably not work with an image I guess.
Your field name "Img_Path" indicates that you only save the path in that field, not the image, and that the image itself is on your file system # F:/PYQT Projects/bikepanel.jpg. This would be the normal way of saving images: The image file itself is in some folder on your server and you save the path to that folder in your table.
So if you want to show the image you would select the saved path from your MySQL table and read the file with your respective programming language (e.g. PHP or whatever you are using).
This might do the job unless there is an issue with variable "i" below:
INSERT INTO AutomobileParts (Part_ID, Part_Name,Img_Path)
VALUES (
"101AA",
"BikePanel",
"F:/PYQT Projects/bikepanel.jpg"
) WHERE i=1;

Related

SQL - Changing Stored img URL in SQL db to local file path

I have image URLs stored in my SQL database, but the source of the images has not been reliable recently and causing long load time on my pages. The contents of my database update regularly. Is there a method I can use to store the images locally and display the images on my page via my server file path? Most of the images or over 1 MB so I was told not to store them in SQL directly as a blob. I am using ASP, HTML, SQL Server
You can do something like this: Create a Column to store path to pictures.
Then in the ImagePath enter: For each image/images
C:Users/JohnDoe/Documents/Pictures/SQLimages/JohnDoe.jpg
Update !!!
Save the images in your chosen Directory for Example.
Currently Your Table might look like this:
ID ImagePath
---------------------------------
1 \\fakeURL\image111
2 \\fakeURL\image222
3 \\fakeURL\image333
4 \\fakeURL\image444
Once you have saved your images to chosen local folder you can run this script
UPDATE dbo.yourDatabase
SET Value = REPLACE(Value, '\fakeURL\', '')
WHERE ID <=100000 --arbitrary number can be any number
This will replace the current URL path to your local path(You won't have to manually enter path name).

importing a csv file into mysql as BLOB

I'm working on test scripts and I want to load the results.csv file into a database as a BLOB.
Basically I want my table to look like:
serial_number | results |.
So one row for each device. My code would look like this for a device with serial number A123456789 (changed names and path for simplicity). The table is called test.
create table test (serial_number varchar(20), results longblob);
insert into test values ('A123456789',load_file('C:/results.csv'));
When I do this, however, the second column which should contain a BLOB, comes out containing NULL, with no exceptions raised.
If I open my results.csv file in notepad, then save as .txt file with no changes whatsoever, I get exactly what I want when I run the same code substituting ".csv" with ".txt" in the path. Basically it would also solve my problem if I could load the csv file as a text file.
thanks for anything you may be able to contribute.

ColdFusion 9 and filestream

I have an application that allows users to upload files. Right now it is uploading directly to the file system. I have my database set up to use filestream (in SQL Server 2008). I have a form with an input field type of file.
<input type="file" name="ul_path1" id="ul_path1">
that allows the user to select the file. I then tried to use the files name selected to do a query to insert the file into the database.
<cfquery datasource=#ODSN# name="upFiles">
insert into redbook_uploads
'#session.buildno#', '#form.ul_path1#', Cast('#form.ul_path1#' As varbinary(max))
</cfquery>
But I get an error right before #form.u1_path1# and in the error message it shows the SQL statement as
insert into redbook_uploads '009', 'C:\apps\ColdFusion9\runtime\servers\coldfusion\SERVER-INF\temp\wwwroot-tmp\neotmp7306602622243140924.tmp', Cast('C:\apps\ColdFusion9\runtime\servers\coldfusion\SERVER-INF\temp\wwwroot-tmp\neotmp7306602622243140924.tmp' As varbinary(max))
when it should be:
insert into redbook_uploads '009', 'C:\users\username\file.pdf', Cast('C:\users\username\file.pdf' As varbinary(max))
I can't figure out how to get the actual file name and am not sure why it's not getting it.
This forum post might have the correct answer:
Here are the basic steps for uploading a file and storing it in your database.
Create a page with a form that includes a file tag. This form will submit to an 'action.cfm' page. Note the name of the .cfm page is not important, 'action.cfm' is an example.
On your 'action.cfm' use CFFILE with action="upload" tag to save the file to your filesystem.
Use the CFFILE tag with action="readbinary" to read the contents of the uploaded file into a variable. You may also want to use CFFILE to delete the uploaded file after it has been read into a variable.
Use CFQUERY with CFQUERYPARAM to insert the file conents.
For whatever reason, the path to the file is defaulting to:
C:\apps\ColdFusion9\runtime\servers\coldfusion\SERVER-INF\temp\wwwroot-tmp\neotmp###.tmp.
We need to see some code to provide a more specific answer. But to answer part of your question ...
Newly uploaded files are always placed in a server temp directory and given a temporary file name. On your action page, you must use cffile action=upload tag to move the file from the temp directory to the desired location.
The action name is a bit misleading. It does not really upload anything. As you can see from the temporary path, the file was already uploaded to the server. All action=upload does is to move/rename the file. It also populates the CFFILE (or result) structure with details about the file, such as it is name, file extension, mime type, etcetera. (For more details, see the documentation.) Those values can be used to save file information to your database.
<cffile action="upload"
fileField = "form.nameOfYourInputField"
destination = "c:\path\to\target\directory"
nameConflict = "makeUnique"
result="fileDetails" />
<cfdump var="#fileDetails#" label="Uploaded File Details">

How do I use LOAD_FILE to insert value from a file into a table?

I tried to insert a text file into my database, using this code:
INSERT INTO test.table (url_address, html)
VALUES ('abc', LOAD_FILE('C:\Documents and Settings\eran\Desktop\1.txt'));
However, I get null in the html column. How can I get the data from the text file to my database?
MySQL LOAD_FILE() reads the file and returns the file contents as a string.
To use this function, the file must be located on the host server, user must specify the full path name of the file, and user must have the FILE privilege. The file must be readable and size must be less than max_allowed_packet (set in the my.ini file) bytes. It returns NULL if the file does not exist or can’t be read.
Try using double back slash "\" for the path. I do not know if it will work.
http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_load-file
http://www.w3resource.com/mysql/string-functions/mysql-load_file-function.php

Images in MySQL

Is there a Image related data type in MySQL that can be used for storing images for each recor?
Performance-wise it's probably better to store the file as a file on the drive and only write the filename or filepath and maybe the mime type into the database. But as the others say, BLOB is what you seek.
Like said before, blob is the way to go, however, as SanHolo points out it's not really performance-wise and you will eventually run into problems as your database can grow really really fast!
Why don't you index the file-name on the database and store the file on the server?
The mainly reason to not allow something like this would be security issues. If you are really trying to cover your bases by not allowing all users to see or grab content you have two options.
Option A) give the file a unique, non-identifiable name like Flickr does. The name of the file comprehends two hashes. A user hash and a file-hash. The second hash is secret and the only way you could get it would be by trial and error. Take a look into this file I have on Flickr. Is user-protected (only family can see) but you will be able to access it just fine as the URL itself serves as a protection: http://farm2.static.flickr.com/1399/862145282_bf83f25865_b.jpg, even if you were randomly trying to generate hashes and found a valid one it would be hidden by anonymity as you wouldn't know who it was from.
Option B) use a server side thecnology to limit the access. This method is safer but more expensive to the server. You will set up a script that will allow/deny access to the file based on session_permissions or something alike. Look at the following code that would be called by accessing something like:
http://yourserver.com/getprotectedfile.php?filename=213333.jpeg
session_start();
// logic to verify the user is ok
if($_SESSION['user_access']!=true) {
exit('user not allowed here');
// WATCHOUT! THIS IS NOT SECURE! EXAMPLE ONLY.
// on a production site you have to be sure that $filename will not point to a system file
$filename = $_GET['filename'];
// gets the file and outputs it to the user
header('Content-type: image/jpeg');
header('Content-Length: '.filesize($filename));
readfile($filename);
You'll need to use some sort of BLOB type:
MySQL Reference: BLOB and TEXT types
Which type you use depends on the size of the image you wish to store.
Try one of the BLOB data types.
Yes , there are some datatypes to store image into database.
BLOB (Binary large object)
which can be specially use for Storing of image files into database.
The four BLOB types are ' TINYBLOB ',' BLOB ', ' MEDIUMBLOB and 'LONGBLOB '. These differ only in the maximum length of the values they can hold.
Creating table:
CREATE TABLE image (imgname VARCHAR(33) , photo LONGBLOB );
For inserting Values refer to Insert Picture into SQL Server 2005 Image Field using only SQL
I have Stored one image file in database by using java jdbc, So if you want to see how image file is stored in database
click the link : https://ibb.co/frCpt37