Tuesday, September 25, 2012

MySQL Tutorial - How to calculate time correctly

A database server has a database called "Cinema" which has a table called "Movies". This table has next fields:

  • ID
  • Name
  • Duration

Hereafter, you can see table 'movies':

movies_id movies_name movies_duration
1 Movie I 02:23:34
2 Movie II 01:44:56
3 Movie III 02:56:34

To calculate duration of all movies, we could write:

SELECT sum(movies_duration) FROM movies;

But it is wrong!

Result:

sum(movies_duration)
62424

To fix this problem, it is necessary convert time to seconds with next SQL statement:

SELECT time_to_sec(movies_duration) FROM movies;

Result:

time_to_sec(movies_duration)
8614
6296
10594

And next, use function SUM() to calculate duration of all movies

SELECT sum(time_to_sec(movies_duration)) FROM movies;

Result:

sum(time_to_sec(movies_duration))
25504

Finally, convert the sum (in seconds) to time using the next statement:

SELECT sec_to_time(sum(time_to_sec(movies_duration))) FROM movies;

Result:

sec_to_time(sum(time_to_sec(movies_duration)))
07:05:04

Thus you can calculate time correctly using three MySQL functions:

  1. TIME_TO_SEC(...)
  2. SUM(...)
  3. SEC_TO_TIME(...)

Saturday, September 1, 2012

JavaScript Tutorial - How to print a Web Page

I was working on my project called "Project Manager". And I was needing to create a link to print a web page. After I was researching on Web, I found a small code written in JavaScript.

<a href="javascript.window.print()">Print</a>

Clicking on link, it will show up a box dialog of printer.

Tuesday, August 21, 2012

Oracle PL/SQL


For professional reasons, I was forced to study Oracle PL / SQL.


PL/SQL is an imperative 3GL that was specifically designed for the seamless processing of SQL commands. It provides specific syntax for this purpose and supports exactly the same datatypes as SQL. Server-side PL/SQL is compiled and stored in Oracle Database and Oracle runs within the executable. It automatically inherits the robustness, security, and portability of Oracle Database.

To study Oracle PL / SQL, I found a great website and I highly recommend.

Wednesday, August 8, 2012

How to install Samba

Since 1992, Samba is an open-source software that your main goal is to remove the barriers of interoperability between different operating systems.

I need Samba to move files from Windows to Ubuntu and vice-versa for work's issues. Hereafter, you can see the steps how to install Samba:

  1. sudo apt-get install samba samba-tools
  2. sudo smbpasswd -a yourusername
    • Type your password
    • Retype your password
  3. mkdir /srv/samba/share
  4. sudo nano /etc/samba/smb.conf
  5. Press Ctrl+W
    • Search for: #security=user
    • remove '#' (from "#security=user" to "security=user")
  6. On bottom of file write the next statements:
  7. [share]
    path = /srv/samba/share
    comment = some random files
    available = yes
    valid users = yourusername
    read only = no
    
  8. sudo restart smbd
  9. testparm /etc/samba/smb.conf

Note: After you typed the last command and got errors message, you have to fix it. But I am 100% sure, it will not show up a error message unless you mistyped some statements.

Monday, July 23, 2012

Set index.php as default page

I was programming a Web app and I noticed that always insert address of Web app, I had to type "www.mywebapp.com/index.php" to show up a web page. If I just type the next address "www.mywebapp.com", the Web server (Apache) list the directories and some files.

I would rather type "www.mywebapp.com" and same time the Web server show up the index.php. But how can I do that? Just follow the next steps:

  1. Open notepad and write the next statement "DirectoryIndex index.php"
  2. Save file as
    • Write name of file as ".htaccess"
    • Change type of file from " Text file (*.txt)" to "All files (*.*)"

Note: You must save the file on the same folder where is "index.php". If you don't save on the same folder, it will not work correctly.

Conclusion: The DirectoryIndex command allows specify a default page to display.