<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dimuthu's Blog &#187; database</title>
	<atom:link href="http://www.dimuthu.org/catagory/database/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dimuthu.org</link>
	<description>Waiting for your comments</description>
	<lastBuildDate>Wed, 21 Dec 2011 05:39:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<image>
  <link>http://www.dimuthu.org</link>
  <url>http://www.dimuthu.org/favicon.ico</url>
  <title>Dimuthu's Blog</title>
</image>
		<item>
		<title>Getting the size of BLOB in MySql</title>
		<link>http://www.dimuthu.org/blog/2010/01/06/getting-the-size-of-blob-in-mysql/</link>
		<comments>http://www.dimuthu.org/blog/2010/01/06/getting-the-size-of-blob-in-mysql/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 02:53:32 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[database]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[blob]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[OCTET_LENGTH]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=1049</guid>
		<description><![CDATA[If you want to store binary in database, you can use BLOB as the data type of that column. In Mysql you can use TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOB depending on your space requirement. Here is an example of database table using BLOB as a column type. CREATE TABLE BloBTest ( id INT NOT NULL AUTO_INCREMENT, [...]]]></description>
			<content:encoded><![CDATA[<p>If you want to store binary in database, you can use <a href="http://dev.mysql.com/doc/refman/5.0/en/blob.html">BLOB</a> as the data type of that column. In Mysql you can use TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOB depending on your space requirement. Here is an example of database table using BLOB as a column type.</p>
<pre><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> BloBTest <span style="color: #66cc66;">(</span>
    id INT <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span>,
    filename VARCHAR<span style="color: #66cc66;">(</span> <span style="color: #cc66cc;">32</span> <span style="color: #66cc66;">)</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>,
    content BLOB <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>,
    <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">(</span> id <span style="color: #66cc66;">)</span>
<span style="color: #66cc66;">)</span></pre>
<p><strong>Storing Data</strong></p>
<p><strong>PHP:</strong></p>
<pre><span style="color: #0000ff;">$filename</span> = <span style="color: #ff0000;">"myimage.png"</span>;
<span style="color: #0000ff;">$filecontent</span> = <a href="http://www.php.net/file_get_contents"><span style="color: #000066;">file_get_contents</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$filename</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$filecontent_escaped</span> = <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #000066;">mysql_real_escape_string</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$filecontent</span><span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$sql</span> = <span style="color: #ff0000;">"INSERT INTO BloBTest(filename, content) "</span> +
       <span style="color: #ff0000;">"VALUES('$filename','$filecontent_escaped')"</span>;
<a href="http://www.php.net/mysql_query"><span style="color: #000066;">mysql_query</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$sql</span>, <span style="color: #0000ff;">$link</span><span style="color: #66cc66;">)</span>;</pre>
<p><strong> Java:</strong></p>
<pre><span style="color: #aaaadd; font-weight: bold;">String</span> filename = <span style="color: #ff0000;">"myimage.png"</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AInputStream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">InputStream</span></a> filecontent = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFileInputStream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">FileInputStream</span></a><span style="color: #66cc66;">(</span>filename<span style="color: #66cc66;">)</span>;

<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> sql = <span style="color: #ff0000;">"INSERT INTO BloBTest(filename, content) VALUES(?, ?)"</span>;

<span style="color: #993333;">int</span> size = filecontent.<span style="color: #006600;">available</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3APreparedStatement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">PreparedStatement</span></a> ps = conn.<span style="color: #006600;">prepareStatement</span><span style="color: #66cc66;">(</span>sql<span style="color: #66cc66;">)</span>;
ps.<span style="color: #006600;">setString</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">1</span>, filename<span style="color: #66cc66;">)</span>;
ps.<span style="color: #006600;">setBinaryStream</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">2</span>, filecontent, size<span style="color: #66cc66;">)</span>;
ps.<span style="color: #006600;">executeUpdate</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;</pre>
<p><strong>Retrieving Data</strong></p>
<p><strong>PHP</strong></p>
<pre><span style="color: #0000ff;">$sql</span> = <span style="color: #ff0000;">"SELECT filename, content FROM BloBTest"</span>;
<span style="color: #0000ff;">$result</span> = <a href="http://www.php.net/mysql_query"><span style="color: #000066;">mysql_query</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$sql</span>, <span style="color: #0000ff;">$link</span><span style="color: #66cc66;">)</span>;
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$row</span> = <a href="http://www.php.net/mysql_fetch_assoc"><span style="color: #000066;">mysql_fetch_assoc</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$result</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>

    <span style="color: #0000ff;">$filename</span> = <span style="color: #0000ff;">$row</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">"filename"</span><span style="color: #66cc66;">]</span>;
    <span style="color: #0000ff;">$content</span> = <span style="color: #0000ff;">$row</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">"content"</span><span style="color: #66cc66;">]</span>;
    <span style="color: #0000ff;">$new_filename</span> = <span style="color: #ff0000;">"new_"</span> . <span style="color: #0000ff;">$filename</span>;
    file_put_contents<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$new_filename</span>, <span style="color: #0000ff;">$content</span><span style="color: #66cc66;">)</span>;
<span style="color: #66cc66;">}</span></pre>
<p><strong>Java</strong>:</p>
<pre><span style="color: #aaaadd; font-weight: bold;">String</span> sql = <span style="color: #ff0000;">"SELECT filename, content FROM BloBTest"</span>;

PrepareStatement ps  = conn.<span style="color: #006600;">prepareStatement</span><span style="color: #66cc66;">(</span>resourceContentSQL<span style="color: #66cc66;">)</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AResultSet+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">ResultSet</span></a> result = ps.<span style="color: #006600;">executeQuery</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;

<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>result.<span style="color: #006600;">next</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
    <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> filename = result.<span style="color: #006600;">getString</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"filename"</span><span style="color: #66cc66;">)</span>;
    <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AInputStream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">InputStream</span></a> contentStream = result.<span style="color: #006600;">getBinaryStream</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"content"</span><span style="color: #66cc66;">)</span>;
    <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> newFilename = <span style="color: #ff0000;">"new_"</span> + filename;
    <span style="color: #808080; font-style: italic;">// storing the input stream in the file</span>

    <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AOutputStream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">OutputStream</span></a> out=<span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFileOutputStream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">FileOutputStream</span></a><span style="color: #66cc66;">(</span>newFilename<span style="color: #66cc66;">)</span>;
    <span style="color: #993333;">byte</span> buf<span style="color: #66cc66;">[</span><span style="color: #66cc66;">]</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #993333;">byte</span><span style="color: #66cc66;">[</span><span style="color: #cc66cc;">1024</span><span style="color: #66cc66;">]</span>;
    <span style="color: #993333;">int</span> len;
    <span style="color: #b1b100;">while</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">(</span>len=contentStream.<span style="color: #006600;">read</span><span style="color: #66cc66;">(</span>buf<span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>&gt;<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">)</span>

    out.<span style="color: #006600;">write</span><span style="color: #66cc66;">(</span>buf,<span style="color: #cc66cc;">0</span>,len<span style="color: #66cc66;">)</span>;
    out.<span style="color: #006600;">close</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
<span style="color: #66cc66;">}</span></pre>
<p><strong>Retrieving the Size of the Blob</strong></p>
<p>After you store your data as a blob, you can manipulate or query the data with some of the in-built <a href="http://dev.mysql.com/doc/refman/5.0/en/string-functions.html">String functions</a> in mysql. For an example if you want to query the size of the blob you just stored, you can use <a href="http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_octet-length">OCTET_LENGTH</a> function. Here is an example,  (this will give you the size in bytes.)</p>
<pre><span style="color: #993333; font-weight: bold;">SELECT</span> OCTET_LENGTH<span style="color: #66cc66;">(</span>content<span style="color: #66cc66;">)</span> <span style="color: #993333; font-weight: bold;">FROM</span> BloBTest <span style="color: #993333; font-weight: bold;">WHERE</span> filename=<span style="color: #ff0000;">'myimage.png'</span></pre>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 228px; width: 1px; height: 1px;">
<pre>.</pre>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2010/01/06/getting-the-size-of-blob-in-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Article Introducing PHP Data Services</title>
		<link>http://www.dimuthu.org/blog/2009/01/05/article-introducing-php-data-services/</link>
		<comments>http://www.dimuthu.org/blog/2009/01/05/article-introducing-php-data-services/#comments</comments>
		<pubDate>Mon, 05 Jan 2009 13:06:55 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[database]]></category>
		<category><![CDATA[DataServices]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[RESTful]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[WSDL]]></category>
		<category><![CDATA[wsf/php]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[introduction]]></category>
		<category><![CDATA[SOAP]]></category>
		<category><![CDATA[WS-*]]></category>
		<category><![CDATA[ws-security]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=900</guid>
		<description><![CDATA[Now you can view the article I wrote titling &#8220;Introduction to PHP Data Services&#8220;. There I explain how you can design and implement Data Services in PHP using WSF/PHP Data Services Library. This article covers, Designing your Data Service API. Writing the Data Service. Deploying and Testing Data Service. Make the Data Service available in [...]]]></description>
			<content:encoded><![CDATA[<p>Now you can view the article I wrote titling &#8220;<a href="http://wso2.org/library/articles/introduction-php-data-services">Introduction to PHP Data Services</a>&#8220;. There I explain how you can design and implement Data Services in PHP using <a href="http://wso2.org/projects/wsf/php">WSF/PHP</a> Data Services Library.</p>
<p>This article covers,</p>
<ol>
<li>Designing your Data Service API.</li>
<li>Writing the Data Service.</li>
<li>Deploying and Testing Data Service.</li>
<li>Make the Data Service available in both SOAP and RESTful form.</li>
<li>Use of WS-* features in your Data Service.</li>
</ol>
<p>If you are thinking of adapting SOA in to your database backed PHP applications, this article will be a good starting point.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2009/01/05/article-introducing-php-data-services/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WordPress Database &#8211; Relationship with Tags, Catagories and Posts</title>
		<link>http://www.dimuthu.org/blog/2008/12/20/wordpress-database-relationship-with-tags-catagories-and-posts/</link>
		<comments>http://www.dimuthu.org/blog/2008/12/20/wordpress-database-relationship-with-tags-catagories-and-posts/#comments</comments>
		<pubDate>Sat, 20 Dec 2008 16:11:39 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[database]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[catgoary]]></category>
		<category><![CDATA[posts]]></category>
		<category><![CDATA[tag]]></category>
		<category><![CDATA[taxonomy]]></category>
		<category><![CDATA[term]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=825</guid>
		<description><![CDATA[WordPress has a very simple database schema. And it is well documented. You can access the complete description of the wordpress core database from here, http://codex.wordpress.org/Database_Description. Anyway first time I looked at the database I was confused with the term and the term_taxonomy table, why we need two tables for term and taxonomies. In fact [...]]]></description>
			<content:encoded><![CDATA[<p>WordPress has a very simple database schema. And it is well documented. You can access the complete description of the wordpress core database from here, <a href="http://codex.wordpress.org/Database_Description">http://codex.wordpress.org/Database_Description</a>.</p>
<p>Anyway first time I looked at the database I was confused with the term and the term_taxonomy table, why we need two tables for term and taxonomies. In fact in wordpress, the table &#8216;posts&#8217; is associated with the table &#8216;term_taxonomy&#8217; and not the table &#8216;term&#8217; itself. In the term taxonomy table the terms are associated to a link category, post category or a tag. So the associations of posts to a tag or category is something like this.</p>
<div id="attachment_828" class="wp-caption alignnone" style="width: 510px"><a href="http://www.dimuthu.org/wp-content/uploads/2008/12/wp_term_post_association.jpeg"><img class="size-full wp-image-828" title="wp_term_post_association" src="http://www.dimuthu.org/wp-content/uploads/2008/12/wp_term_post_association.jpeg" alt="wp_term_post association" width="500" height="136" /></a><p class="wp-caption-text">wp_term_post association</p></div>
<p>So in a case you try querying for posts with a given tag it will be like this. (Note that I have skipped the optional database table prefix which is by default &#8216;wp_&#8217;)</p>
<pre class="sql"><span style="font-weight: bold; color: #993333;">SELECT</span> post_title,
       post_content,
       post_date
<span style="font-weight: bold; color: #993333;">FROM</span> posts p,
     terms t,
     term_relationships r,
     term_taxonomy tt
<span style="font-weight: bold; color: #993333;">WHERE</span> p.post_status=<span style="color: #ff0000;">'publish'</span> <span style="font-weight: bold; color: #993333;">AND</span>
      tt.taxonomy = <span style="color: #ff0000;">'post_tag'</span> <span style="font-weight: bold; color: #993333;">AND</span>
      p.id=r.object_id <span style="font-weight: bold; color: #993333;">AND</span>
      r.term_taxonomy_id=tt.term_taxonomy_id <span style="font-weight: bold; color: #993333;">AND</span>
      tt.term_id = t.term_id <span style="font-weight: bold; color: #993333;">AND</span> t.name <span style="font-weight: bold; color: #993333;">LIKE</span> ?</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/12/20/wordpress-database-relationship-with-tags-catagories-and-posts/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

