Wednesday, 5 October 2016

Restoring a single table from a full mysqldump file



Yesterday I needed to restore one table from larger database dump file. To begin with I found out which lines I needed end with sed i got my data.
sed -n 955,991p data.sql > new_data.sql
Pretty easy, but in a large file you have to work your way to find the line you need. The solution I found was more specific and works like a charm. The first version of the command was a rather inaccurate because it was missing the "DROP TABLE IF EXIST" for the table you are restoring and you need to edit out few lines from the bottom of the new file before using it in order to prevent deleting the next table, "address". leaves "DROP TABLE IF EXIST" for the next table at the end of the new file.
sed -n -e '/CREATE TABLE.*`person`/,/CREATE TABLE/p' data.sql > new_data.sql
Leves this at the end og the new file.
--
-- Table structure for table `address`
--

DROP TABLE IF EXISTS `address`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `address` (

Then I came a cross better solution to get your table data. While this is the backup layout.
--
-- Table structure for table `person`
--

DROP TABLE IF EXISTS `person `;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `person` (
  `id` int(4) NOT NULL AUTO_INCREMENT,
  `name` varchar(225) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `person`
--

LOCK TABLES `person` WRITE;
/*!40000 ALTER TABLE `person` DISABLE KEYS */;
INSERT INTO `person` VALUES (1,'Emma'),(2,'Noah'),(3,'Olivia'),(4,'Liam'),(5,'Sophia');
/*!40000 ALTER TABLE `person` ENABLE KEYS */;
UNLOCK TABLES;

As you can see "UNLOCK TABLES" is the last line, so that is what we look for in the sed ccommand.
sed -n -e '/DROP TABLE IF EXISTS.*`person`/,/UNLOCK TABLES/p' data.sql > new_data.sql

Now your new file should be ready to use, but I recommend that you check before you use.

--cheers
Monday, 12 September 2016

How to disable the Caps Lock key on Mac


Caps Lock is the the most unnecessary key on the keyboard for me. I rarely need to write more than couple of words in capitals and to often I accidently hit the key, suddenly finding myself typing IN ALL CAPS
Here’s an easy way to change the behavior of your Mac’s Caps Lock key and even disable it altogether.

  • Click the  icon in the top-left corner of your desktop and select System Preferences
  • Select the Keyboard icon and click the “Modifier Keys” button in the bottom-right corner 
  • From the pull-down menu next to “Caps Lock Key”, select “No Action” 
  • Press “OK”



Monday, 20 June 2016

How you can make someone unfollow you on Twitter

This trick is known as a "soft block", and it comes handy when someone’s annoying you on Twitter and you really do not want to block him. You have a few options for dealing with annoying accounts.
Block, which prevents them seeing your tweets.
Mute, which stops you seeing their tweets and replies to you.
And Soft block which forces them to unfollow you.
If you are active Twitter user you are most likely familiar to Block and Mute. But you might or might not have heard about Soft block. Because It’s not an official feature, soft block is not commonly known.
The procedure is quite simple, all you have to do is promptly block and unblock the user in question.

If the person doesn’t notice you’re not coming up in their feed anymore, you’re good. Else repeat.

--Cheers


Wednesday, 25 May 2016

Restoring applications after minimizing to Mac OS X dock


When you click the yellow pill button in the upper left corner of a window to minimize that window in Mac OS X or if you use ⌘ Cmd + M you might want to bring the application back. When cycling through running applications, including the minimised applications, using ⌘ Cmd + Tab you might expect to retrieve the application back.

This sequence on the other hand will bring wour application window.
1. Use ⌘ Cmd + Tab untill you find your application.
2. Before releasing ⌘ Cmd, press and hold ⌥ Alt

Other method is to hide application using ⌘ Cmd + H, then you can restore your window with ⌘ Cmd + Tab
Thursday, 12 May 2016

Using Python, MySQL and UTF-8

I have been spending way to long time figuring out how I can encode my data to utf8 when fetching records from MySQL database. All the time I was focusing on encoding the output, resulting in error like this one.
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe1 in position 1: 
ordinal not in range(128)
The solution I finally found is to set the character set when I prepare the MySQL connection
con.set_character_set('utf8')
Ending up in my code like this.
!/usr/bin/env python
# -*- coding: utf-8 -*-
import MySQLdb as mdb

def getname(p):
    con = mdb.connect('hostname', 'username', ' password', 'databasname')
    con.set_character_set('utf8')
    sql = "SELECT cellname1 from tablename where cellname2='%s'"%(str(p))
    cur = con.cursor()
    cur.execute(sql)
    row = cur.fetchone()
    if not row:
        return 'n/a'
    else:
        return str(row[0])
    cur.close()
Now everything works like a charm.
Tuesday, 15 December 2015

How To Allow Usb To Serial Adapters In Osx “el Capitan


In my line of work I use serial port on some devices frequently and most new computers do not have serialport. In Linux I found out that it’s easy to connect using usb to serial and screen, but now when I’ve got mac it looks like there is no driver for the usb to serial by default.
I bought PL-2303 Driver (€6.17/$7.36) and all my problems are solved. It'a easy and fast installation.
After I connect the USB serial converter I simply type in:
screen /dev/tty.Repleo-PL2303-00001014
-cheers
Monday, 23 November 2015

Windows is blocking random websites


My gf is running Windows and some time after she got Windows 8 some DNS issue occurred and random webpages started to get blocked. After update to Windows 10 this problem was still ongoing. To "fix" this she had to use
ipconfig /flushdns
But that was just a temporary fix because later same day you could need to do this again.
I did multiple web searches and in the end I found this method that seems to fix this issue.

Run CMD as admininstrator.
In the command line type:
netsh winsock reset
Press Enter then restart your PC.

Netsh winsock reset is a helpful command that you can use in Windows to reset the winsock catalog back to default setting or clean state.
In computing, the Windows Sockets API (WSA), later shortened to Winsock, is a technical specification that defines how Windows network software should access network services, especially TCP/IP.