Tuesday, August 16, 2016

MySQL: Import CSV file data into temporary tables.

Sometimes is needed to query the database based on an external data file (.csv).
So we could consider to import this data to a temporary table to achieve this, and to ensure not to alter the database schema at all.

Let's say we have a table like this:

mysql> SELECT * from User;
+------+----------+-------------+------------+
| id   | name     | last_name   | sport      |
+------+----------+-------------+------------+
| 1000 | Oscar    | Soliz       | cycling    |
| 1001 | Wendy    | Cornejo     | athletics  |
| 1002 | Karen    | Torrez      | swimming   |
| 1003 | Rosemary | Quispe      | marathon   |
| 1004 | Stefany  | Coronado    | athletics  |
| 1005 | Jose     | Quintanilla | swimming   |
+----+------------+-----------+--------------+

And a CSV file which content some ids:

id
1002
1004
1005

Now, assume we have the following scenario:

"Given a list of User IDs (CSV file), we want to know the name and sport of these people."

Steps:

1. Create a temporary table:

CREATE TEMPORARY TABLE user_ids (id INT(11));

2. Import data from the CSV file:

LOAD DATA INFILE "/file_path/user_ids.csv"
INTO TABLE user_ids
COLUMNS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES; # To ignore headers (first row)


3. Finally you can build the query and work like it is one more table in the database, e.g:

SELECT CONCAT(u.name, ' ', u.last_name) name, u.sport
FROM User u
WHERE u.id IN (SELECT id FROM user_ids);

+------------------+------------+
| name             | sport      |
+------------------+------------+
| Karen Torrez     | swimming   |
| Stefany Coronado | athletics  |
| Jose Quintanilla | swimming   |
+------------------+------------+

Tuesday, April 19, 2016

MySQL: How to export query results to a CSV file

You can use the following query, making sure you have write permission over the destination path.

SELECT field1, field2
FROM table_name INTO OUTFILE '/destination_path/result.csv' 
FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';

Including headers:

(SELECT 'label_field1','label_field2')
UNION 
(SELECT field1, field2
FROM table_name
INTO OUTFILE '/destination_path/result.csv'
FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n');

Tuesday, April 5, 2016

Empower your Gmail with Mailtrack

What is Mailtrack?


Mailtrack is a Chrome extension for Gmail, which allows you to know when the messages you send have been read.
Basically it brings the double-checks (✓✓) we usually see in mobile messaging to your Gmail inbox and it is free for unlimited time!


Main Features:
- See which messages have been read, how long ago, how many times and which device they were opened on.
- Real-Time desktop notifications
- The only email tracking app with double-checks for Gmail.



Who is it for?
- Account managers, businesses, sales teams and people with direct client relationships
- Professionals looking to increase their productivity
- Teams, project managers, coordinators and internal communications
- Independent people (individuals, freelancers) and people actively looking for employment

Tuesday, March 29, 2016

Configuring Google Chrome Proxy

It should be as easy as going through the Chrome Settings->Network (Change Proxy settings..)


But if for any reason you are not able to access this page in Linux.
e.g.
"When running Google Chrome under a supported desktop environment, the system proxy settings will be used. However, either your system is not supported or there was a problem launching your system configuration."



We can use a workaround by editing the desktop configuration file (assuming you are using GNOME desktop environment).


1. Open the google-chrome.desktop file:

sudo vim /usr/share/applications/google-chrome.desktop

2. And add the proxy you want to use next to the exec value:
e.g: --proxy-server="112.199.65.190:3128" (Philippines proxy server)


3. Save the changes and restart the browser
Verify you are navigating under the configured proxy.

You can use any page to scan and show you what is your current IP address.
e.g. http://ipaddress.com/


Wednesday, March 23, 2016

Hide vertical line from Atom editor

You may first want to know why this is supposed to be there.



The wrap-guide package places a vertical line in each editor at a certain column to guide your formatting, so lines do not exceed a certain width.

But In case you prefer just have it hidden, you can simple do it in different ways:

Editing Preferences:
1. Go to Edit->Preferences->Packages
2. Search for the 'wrap-guide' package and disable it



Editing the Stylesheet:
1. Go to Edit->Stylesheet..
2. Here you personalize the styles as desired:
2.1 To just hide it:

atom-text-editor::shadow .wrap-guide {  
  visibility: hidden;
}

2.2. To play a little bit with other properties:

atom-text-editor::shadow .wrap-guide {
  width: 10px;
  opacity: .3; //barely visible
  background-color: red; //test purpose
}