MYSQL drop all tables in database in linux

Taken from http://knaddison.com/technology/mysql-drop-all-tables-database-using-single-command-line-command#comment-3588.

#!/bin/sh

mysql --user=$1 --password=$2 -BNe "show tables" $3 | tr 'n' ',' | sed -e 's/,$//' | awk '{print "SET FOREIGN_KEY_CHECKS = 0;DROP TABLE IF EXISTS " $1 ";SET FOREIGN_KEY_CHECKS = 1;"}' | mysql --user=$1 --password=$2 $3

Save code above in a file, say, mysql-drop-tables. Then change the permission to excecuteable

chmod +x mysql-delete-tables

Then run it

mysql-drop-tables dbuser dbpassword dbname

Here’s fhe file if you’re too lazy to copy paste
http://dl.dropbox.com/u/112837/scripts/mysql-drop-tables

Advertisements

Installing PIL in virtualenv in Ubuntu

To install PIL in virtualenv in ubuntu, these are the steps:

  1. install the dependencies needed to build PIL

    sudo apt-get build-dep python-imaging
  2. then run these commands

    sudo ln -s /usr/lib/i386-linux-gnu/libfreetype.so /usr/lib/
    sudo ln -s /usr/lib/i386-linux-gnu/libz.so /usr/lib/
    sudo ln -s /usr/lib/i386-linux-gnu/libjpeg.so /usr/lib/

    If you’re using 64bit system, switch i386-linux-gnu to x86_64-linux-gnu.

    If you don’t do it, you might see these messages when after building PIL, and you won’t have jpeg support.

    *** JPEG support not available
    *** ZLIB (PNG/ZIP) support not available
    *** FREETYPE2 support not available
  3. Last command, install PIL

    pip install PIL

Ubuntu 11.04 (Natty Narwhal) Daily Build

It’s available, ubuntu 11.04 daily build. No it’s not released yet. It’s the development build. If feel like to help ubuntu development, you could try it.

According to the release schedule, the first alpha will be released on December 2nd, and the final release will be available on April 28th.

Tagged , , ,

CodeIgniter and Ajax Using jQuery tutorial using JSON

This post is outdated, and might and might not work. Unfortunately I currently don’t have time to check and/or fix it. Sorry ๐Ÿ˜ฆ

So now you have created CodeIgniter + AJAX jQuery, you’ll want to use JSON, javascript way to exchange data.

Assuming you already understand how to do CI + AJAX jQuery, using JSON should be pretty easy. We’ll only need to extend couple things. First, we’ll be extending Message::view in Message controller from that tutorial. Then we’ll extend the view to fetch/process the data.

Controller

The previous method was like this:

function view($type = NULL)
{
    // get data from database
    $data['messages'] = $this->Message_model->get();

    if ($type == "ajax") // load inline view for call from ajax
        $this->load->view('messages_list', $data);
    else // load the default view
        $this->load->view('default', $data);
}

Since we already handled the view type we want to return, all we have to do is add more handler for json. We’ll just convert the array of objects we get from model using json_encode function. Here’s the code:

function view($type = NULL)
{
    // get data from database
    $data['messages'] = $this->Message_model->get();

    if ($type == "ajax") // load inline view for call from ajax
        $this->load->view('messages_list', $data);

    else if ($type == 'json') // json, print the output directly
    {
        echo json_encode($data['messages']);
    }

    else // load the default view
        $this->load->view('default', $data);
}

View

We’ll be adding a button to trigger the new “fetch json data” so the previous handling is still functional.

The previous view (default.php) was:

load->view(‘messages_list’) ?>

Then we added second submit button:

load->view(‘messages_list’) ?>

Now the javacript. The previous javascript/jquery code was:

$(document).ready(function() {
    $('#submit').click(function() {

        var msg = $('#message').val();

        $.post("", {message: msg}, function() {
            $('#content').load("");
            $('#message').val('');
        });
    });
});

Then we need to create handler to fetch the data (in json) and put it in correct html tag. Here’s the code after we added handler for second submit button:

$(document).ready(function() {
    $('#submit').click(function() {

        var msg = $('#message').val();

        $.post("", {message: msg}, function() {
            $('#content').load("");
            $('#message').val('');
        });
    });

    $('#submit2').click(function() {
        var msg = $('#message').val();
        $('#message').val('');
        $.post("", {message: msg}, function() {
            $.getJSON("", function(data) {
                var contentHtml = "
    “; $(data).each(function(index, item) { contentHtml += ”

  1. “; contentHtml += item.message; contentHtml += “
  2. “; }); contentHtml += “

";
                $('#content').html(contentHtml);
            });
        });
    });
});

It’s done. Try yourself, or download the files needed.

I know, it’s pretty basic. But I think it’s enough to get you started to use json in exchanging data. Why json instead of complete html? less data transfer.

Tagged , , , ,

You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column

Error Code: 1175
You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column

Every found that kind of error when trying to update rows in mysql? It’s because you tried to update a table without a WHERE that uses a KEY column (err …).

Anyway, The quick fix is to add SET SQL_SAFE_UPDATES=0; before your update query. Here’s the example:

SET SQL_SAFE_UPDATES=0;
DELETE FROM people WHERE person_status = 'deceased';
Tagged , , ,

Enable PHP in Apache userdir in Lucid Lynx (Ubuntu 10.04)

If you’re using php + apache in Lucid Lynx (Ubuntu 10.04), you’ll notice that the php is disabled when you’re using userdir module (http://localhost/~yourusername). To enable it, open /etc/apache2/mods-enabled/php5.conf (alt+F2, then type gksudo gedit /etc/apache2/mods-enabled/php5.conf) and comment this part (add ‘#’ character in every line):


    
        php_admin_value engine Off
    

The result should be like this:

#
#    
#        php_admin_value engine Off
#    
#

Save the file, then restart the apache by typing sudo /etc/init.d/apache2 restart in terminal.

(taken from http://ubuntu-virginia.ubuntuforums.org/showthread.php?t=1478721)

Tagged , , , ,

Convert pdf to image

Ever want to convert pdf file to an image? Itโ€™s easy with imagemagick. Ubuntu default installation has it. I believe itโ€™s installed in most linux distribution by default. Anyway, hereโ€™s how:

convert file.pdf file.jpg
convert file.pdf file.png

Yeah. That easy.

Tagged , , , ,

An Error Occurred During The Signature Verification

If you’re using ubuntu as your choice of linux distribution, and you like to add some unofficial repositories, you probably find some warnings when you run apt-get update command.

The warnings are probably like this

W: A error occurred during the signature verification. The repository is not updated and the previous index files will be used.GPG error: http://dl.google.com stable Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY A040830F7FAC5991

W: Failed to fetch http://dl.google.com/linux/deb/dists/stable/Release  

W: Some index files failed to download, they have been ignored, or old ones used instead.
W: You may want to run apt-get update to correct these problems

That’s the warnings I got when I added google debian repository. There won’t be anything bad about it, since we could still install the packages from that repositories. But, I we could get rid the warnings, that’s would be a lot better. We could use gpg command to get the key.

Short Version

For you who don’t want to read pointless explanations below, here’s the short version

gpg --keyserver hkp://subkeys.pgp.net --recv-keys A040830F7FAC5991
gpg --export --armor 7FAC5991 | sudo apt-key add -

Long Version

Here’s the explained step. First we have to get the key from the key server.

gpg --keyserver hkp://subkeys.pgp.net --recv-keys A040830F7FAC5991

The A040830F7FAC5991 is from the warning shown before. You might want to change it if you have different repository. For the google repository, you should get this as the output

gpg: requesting key 7FAC5991 from hkp server subkeys.pgp.net
gpg: key 7FAC5991: public key "Google, Inc. Linux Package Signing Key " imported
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
gpg: Total number processed: 1
gpg:               imported: 1

After that, type

gpg --export --armor 7FAC5991 | sudo apt-key add -

Where the 7FAC5991 is from the output shown before.

After that you’ll get OK as the output. You may run apt-get update again.

Oh, one more thing, I only tested this method in opera and google repositories ๐Ÿ™‚

Tagged , ,

Replace Space With Underscore in Filename

This is merely my personal note. I was about to rename all my files under a directory. I need to replace the space with underscore character ('_'). My first thought was a simple bash script to do that. Apparently, it’s been very long time since my last bash coding session. I spent 15 minutes reading how to read all files and rename them. And I got nothing.

Luckily, I know python. Stupid me. Why didn’t I think it at first time. It was couple minutes of python and all the spaces were replaced by underscores. Thanks to python. All I did were

$ python
Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49) 
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> files = os.listdir('./')
>>> for f in files:
...     os.rename(f, f.replace(' ', '_'))
... 
>>> 

Or if you want to save in a script, you could you this

#!/usr/bin/python

import os
import sys

files = os.listdir(sys.argv[1])
for f in files:
    os.rename(f, f.replace(' ', '_'))

The script takes the directory path as the argument. You could modify the script to use regex to have a better rename rule ๐Ÿ™‚

Tagged , ,

Banshee 1.0 Beta 2 in Ubuntu 8.04 Hardy Heron

Now Banshee 1.0 Beta 2 is released. If you want to try the music player in your ubuntu hardy box, you could add sources.list entry from banshee PPA team.

Open software sources (System > Administration > Software Sources). In Third-Party Software tab, click Add and paste the code below

deb http://ppa.launchpad.net/banshee-team/ubuntu hardy main

If you need the source, you could repeat the Add proccess, and paste this

deb-src http://ppa.launchpad.net/banshee-team/ubuntu hardy main

Close the Software Sources. Wait for a few moment while the software update your package database. Then open the terminal, and type

sudo apt-get install banshee-1

Yes, use banshee-1. If you use banshee, you’ll get the old Banshee 0.13.2 from universe repository.

And, enjoy your new Banshee ๐Ÿ™‚

Tagged , , , , , ,