Search This Blog

Sunday, September 25, 2011

Saving Images in MySQL.



Here in an Step-by-Step guide to storing an Image in MqSQL database. Similarly, other binaty files(like documents or videos) can also be stored in the database.

1) Download and install MySQL Query Browser.
For ubuntu: sudo apt-get install mysql-query-browser

2) Start it and create a connection to your database. Use the "test" as default schema.

3a) Excute the following SQL statement.

CREATE TABLE `test`.`pic` (
`s_no` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`img` LONGBLOB NOT NULL,
PRIMARY KEY(`s_no`)
)
TYPE = InnoDB;

3b) Instead of creating the table via SQL, you can create a table by pressing the right mouse button on the schemata tree and select "Create New Table"

Type "pic" in the Table Name field. Press enter. Press enter again to create the first column named "s_no" (that will be your primary key). Press enter to accept INTEGER as datatype. Enter "name" to store a name for you picture. Press enter. Type "v" (which will trigger VARCHAR(45)) and press enter. Type "img" and enter. Type "longb" (which will trigger LONGBLOB) and press enter. Click [Apply Changes]. This will show you the SQL statement that will be executed.

Press [Execute] to create the table.

5) Close the table editor or refresh the schemata tree and double-click the new generated table in the schemata tree.

6) This will generate the SQL statement

SELECT * FROM pic p

Now click the table again to execute it. You can press Ctrl+Enter instead (of course)

7) Now click on the [Edit] button in the action bar at the bottom. Double-click the empty caption field. Enter a name for your picture. Press enter.

8) Click the "Open" overlay icon in the left of the img field in the first row. Or you can press the right mousebutton on the field and select "Load Field Content" from the popup menu.

9) Click [Apply Changes] to actually store the new data in the database.

10) Now click the "Magnifyer" overlay icon and if you have inserted a JPG, PNG or BMP image you will see it in the field viewer.

Tip: To be able to insert images that are bigger than 1MB you have to increase the max_allowed_packed option in the server configuration file. You can change that in the Startup Variables section in the MySQL Administrator on the Networking page.

Thursday, September 15, 2011

Storing Images/video in MySQL database



MySQl databases are the most widely used databases. While storing data, sometimes it is needed to store binary files(like documents/images/videos) in a database, instead of storing them as files on the server. MySQL allows us to store such data in the database. For this purpose, MySQl has a special data type called as Blob (Binary Large OBject).
Blob can store any type of binary file. A BLOB column has a maximum length of 4,294,967,295 or 4GB (232 – 1) bytes. The effective maximum length of LONGBLOB columns depends on the configured maximum packet size in the client/server protocol and available memory. Each LONGBLOB value is stored using a four-byte length prefix that indicates the number of bytes in the value.

Now, when we are storing files in a database, it would also result in a considerable amount of increase the size of database itself. Since database is nothing but a file in itself, there are limitations on the maximum size of Database, depending on the Filesystem being used by the database.The effective maximum table size for MySQL databases is usually determined by operating system constraints on file sizes, not by MySQL internal limits. The following table lists some examples of operating system file-size limits. This is only a rough guide and is not intended to be definitive. For the most up-to-date information, be sure to check the documentation specific to your operating system.

Operating System File-size Limit
Linux 2.2-Intel 32-bit 2GB (LFS: 4GB)
Linux 2.4+ (ext3 file system) 4TB
Linux 2.6+ (ext3 file system) 16TB
Solaris 9/10 16TB
MacOS X w/ HFS+ 2TB
NetWare w/NSS file system 8TB
Win32 w/ FAT/FAT32 2GB/4GB
Win32 w/ NTFS 2TB (possibly larger)


Note: Even though MySQL is capable of storing this data, it is generally discouraged because it decreases the performance of MySQL drastically. The preferred Solution is to save the files on the server and store their path in the table.

In the next post, i will explain(with sample code) how to store an image/video in the database.
Here is the Link:

Thursday, September 1, 2011

Compiling programs with gcc and g++



Using Linux can be a lot difficult, but only for those who are new to it. But that's pretty obvious. Isn't it?
So, one of the most common problem faced by all the new comers to this platform, who are also into computer programming, is... HOW DO I COMPILE MY C/C++ PROGRAM????
After being asked by too many people on how to compile programs on Linux, i felt a need to write a post describing the whole process in a single place.

The only pre-requisition to getting started is -- an Internet connection.
So, firstly we need to install the compiler for c++. GNU-gcc is the standard compiler for C/C++.
To install GCC, go to the terminal and type:

sudo apt-get install g++
Since gcc has some dependencies for its advanced functionality on g++, it is recommended g++ is installed as well. You can skip this step if you want to.
Your output screen would look a lot different, because my computer had g++ and gcc installed at the time of writing of this post. While in your case, it would probably download it from the internet and install it. It would take a while depending on the speed of your internet connection.


Also, this is of-course for those who are using Ubuntu (or any other Debian variant). The users of other operating systems should use their respective installers. For example,
Fedora: yum install g++
RHEL : rpm -ie g++


Once g++ is installed, now we need to install gcc. For installing gcc, again go to terminal and type
sudo apt-get install gcc


Now you are all set. The compiler is installed and auto-configured to be used on your machine.

Simply open a text editor and start writing a program. I prefer gedit, but vim, emacs and nano can be used as well.


Save the file and remember the location where you have saved it.



Now, go to the terminal and change your present working directory path to the place where your program is saved using "cd" command.
when you are there, type the following commands.

gcc filename.c -o output.o

To generate an object file as output.
chmod +x output.o
This would make the generated output file executable.
./output.o
To execute the program from the object file.