Python + MySQL in Ubuntu (9.10)

Assuming you did a fresh install of Karmic Koala – Ubuntu 9.10

Here are the steps:

– Install Python:

apt-get install python2.6

– Install XAMPP/LAMP stack:

sudo tasksel install lamp-server

After you install follow the steps on setting up the root password see the notes here .

– Install MySQLdb:

I tried downloading the sourceforge gzip and untarring the file etc, not the right route … instead use apt-get:

sudo apt-get install python-mysqldb

Test this through Python

username@machinename:~$ python
Python 2.6.4 (r264:75706, Dec  7 2009, 18:45:15)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>> exit()

If there any errors the one below then check your MySQLdb install.

>>> import MySQLdb
   Traceback (most recent call last):
     File "<stdin>", line 1, in ?
   ImportError: No module named MySQLdb

– Create a simple table:

If you have installed mysql then you should be able to do the following steps and create a dummy table and insert data into it:
username@machine-name:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 53
Server version: 5.1.37-1ubuntu5.1 (Ubuntu)

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql> CREATE DATABASE test;
Query OK, 1 row affected (0.12 sec)

mysql> use test;
Database changed
mysql> create table employees( id INT, name VARCHAR(20), email varchar(20));
Query OK, 0 rows affected (0.18 sec)

mysql> show tables;
+—————-+
| Tables_in_test |
+—————-+
| employees |
+—————-+
1 row in set (0.00 sec)

mysql> insert into employees(id,name,email) values (1,”emp1″,”emp1@mycomp.com”);
Query OK, 1 row affected (0.00 sec)

mysql> select * from test.employees;
+——+———+—————–+
| id   | name    | email           |
+——+———+—————–+
|    1 | emp1 | emp1@mycomp.com |
+——+———+—————–+
1 row in set (0.01 sec)

mysql> exit

– Query the table by connecting from Python and MySQLdb:

username@machine-name:~$ python
Python 2.6.4 (r264:75706, Dec  7 2009, 18:45:15)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>> conn = MySQLdb.connect (host = "localhost", user = "root" , passwd ="abc123" , db = "test")
>>> cursor = conn.cursor()
>>> cursor.execute("SELECT name from employees where id=1");
1L
>>> row = cursor.fetchone()
>>> print "employee name ", row[0]
employee name emp1
>>> cursor.close()
>>> conn.close()
>>> exit()

Errors:

If you specify the “row = cursor.fetchone()” before the “execute” line you will get:
_mysql_exceptions.ProgrammingError: execute() first

If you specify the table name wrong, say “employ” instead of “employees” you will get:
_mysql_exceptions.ProgrammingError: (1146, "Table 'test.employ' doesn't exist")

If you select for one item, say “Select name from”, then the row will have 1 item in it … if you access an index that is larger than the number of elements you specified (minus 1) – for example you do “print “employee name “, row[1]” in the above example, you will get:
IndexError: tuple index out of range

Conclusion:

I had tried to do Python2.6 and MySQL on Windows and wasted a fair bit of time on the MySQLdb install part.  Then I built a new home-development environment over Ubunut 9.10 and boy was this a cake-walk!

You should be on your merry way coding up cool python scripts that craw the web and gather information about pages and store them to a DB. Check out “Programming Collective Intelligence

References:

http://ubuntuguide.org/wiki/Ubuntu:Karmic – For Ubuntu Karmic notes
http://www.kitebird.com/articles/pydbapi.html – For Python db API
http://www.cyberciti.biz/faq/howto-linux-unix-creating-database-and-table/ – For MySQL how to

Stuff to read

Must read up more on these – download, test, play

Design Patterns

Okay so I am officially going to start working through each and every design pattern in the “Sun Certified Enterprise Architect for J2EE Technology” study guide. I keep going through the book from time to time and while it is fun reading UML, there is absolutely no replacement for code and your own experience.

List of design patterns I am going to work on
Design Patterns - Creational, Structural and Behavioral

I started with a top level “design.patterns” package which will have Creational, Structural and Behavioral patterns as a sub-package.  I thought about then writing the examples all under the pattern-type package (design.pattern.Creational) but realized that we may soon have a lot of files from different patterns and not know at-a-glance how many classes we may need per pattern, for example. So I decided to create another level which the Pattern Name so that we can build the Tester and pattern implementation classes under a proper subtree,  “design.pattern.Creational.AbstractFactory” for example.

I also hope to explore some of the anti-pattern implementations we have made and try to reason if that was a good choice then or good choice in hindsight : )

So until then … lets brush up our types (look at the image above – “Creational”, “Structural” and “Behavioral”)  and understand what they mean – i.e. ask your self what is the difference between Creational pattern and Structural pattern and when you would think-about one vs the other.   Good luck!

Matrix manipulation

When solving Boggle, Sudoku or some n-by-n board game or when doing Image analysis or Parallel Computing – it is often useful to be able to represent one-dimensional data structures in 2-dimension and vice-versa.

Simple formulae exists for doing this conversion, as illustrated below:

Converting from Row by Col 2-dimensional (2-index) format to a 1-by-(Row*Col) matrix with only one index:

What we start out with
...what we end up with
...actually its more like this!

Downloaded Software List – 3/25

  • Spring Framework Examples:

spring-framework-3.0.1.RELEASE-A-dependencies.zip

  • Java Connector for MySQL Simple Demo with JDev:

mysql-connector-java-3.1.14.zip

  • PHP/MySQL/Apache:

xampp-win32-1.7.3.exe

  • Java Applets with Killer Graphics using Processing:

processing-1.0.9.zip

  • Java Date/Time API an improvement over the JDK Date/Time:

joda-time-1.6.zip

  • IDE of choice for building/testing on my open source downloads:

eclipse-jee-galileo-SR2-win32.zip

  • Python 2.6

python-2.6.4.msi

  • Python MySQL DB

MySQL-python-1.2.3c1.tar.gz

  • Linux/Ubuntu stuff

SSH Server

LAMP (PHP, Apache, MySQL)

Firestarter

VNC (*desired)

Eclipse (*desired)

Test Scenario

Required Downloads

Completed

Spring Examples Eclipse, Spring 20%
DB Connection Test with Spring and Hibernate Eclipse, Spring, MySQL, JConnector for MySQL No
MQ test Eclipse, Spring, Active MQ No
JDev and MySQL Test JDev, MySQL, MySQL JConnector Yes. PKC site tables created

Create tables in MySQL using JDev

Steps:

  • Setup MySQL
  • Setup JDev
  • Create a new JDev Application
  • Create a new Database Diagram with a sample Table
  • Add columns to the table
  • Create a database connection from JDev to MySQL
  • and Publish the Table to MySQL

Note:  Put the mysql-connector-java-3.1.14-bin.jar in the lib path (JDEV or JDK) to avoid Driver Not Found type errors. Google mysql connector and you should find the installer or zip file – once you extract it you can copy the jar into your lib path (if there is a “proper” way – i.e. extract and set the classpath on windows so that JDEV knows where to look for this driver jar then do it that way).

Welcome …

Hey!!! Techiecook is ALIVE! Wohoooooo!!!

So what’s in this blog?

  • Recipes
  • Code
  • Tech talks
  • And very random walks through my thought patterns

we will post in this blog – stuff about Food and Technology and whatever comes to our mind!

Alright! Say, did you know that “Kokum” is not the same as “Mace” ? Well I did not …

Kokum - spice
Kokum - The Spice

Mace - Spice
Mace - Spice

I used mace a few times when cooking dishes like “Chicken Shahi Korma” (with and without nuts!) … and the flavour in there was just amazing. I promise to put up the recipe soon!

The first recipe

Hey guess what Sundays are famous for? Omlettes!  As one advert put it – “Eggs love a good omlette” – true. It is also the first dish we are going to talk about because it involves a few basic in cooking.

  • Cooking duration
  • Ingredients and their impact to the flavour
  • Heat (how high your burner is at)
  • And preparation (Chopping/cutting or the lack of it)

So an Omlette can be made in a variety of ways by varying how much you cook your eggs, on how many sides and with how many other things.

Simple Receipe:

Two eggs, some milk (optional), some butter (optional) and some salt and pepper are all that is needed … however if you really want to go a notch up you need to think bigger!

Level 1 – The Garlic Oil:

The very first twist to the simple Omlette is Garlic – yes you Draculas! G-A-R-L-I-C, the white pod thingy (and no I AM NOT TALKING ABOUT SOME DARN PASTE IN A BOTTLE!!!! – Sorry had to scream to make sure you got the point about using quality ) …. anyways here is what you need to do with the GarlicReduce the heat and with a spoon mix the eggs a little so that some of the cooked lumps are incorporated into the uncooked mixture … keep doing this until the beaten mix is half way cooked (i.e. 50% lumpy and 50% fluidy) …then sprinkle some pepper on top.

Remove some garlic cloves from the pod . Smash the garlic with a knife and remove the outer skin. Heat some butter in a pan and throw the garlic in. Keep flame at High. Beat some eggs, add salt.

Watch the garlic and smell – when they turn brown and the aroma of garlic + butter fills the air (yeah that is how I cook) … throw the eggs in the pan.Cover or cook in open and in about 5 mins when you think the omlette is ready flip onto a plate and devour with some toast and butter! Simple yet yummy. Oh yeah and don’t forget the ketchup : )

Level 2 – Milk, Ginger and Chili:

Now cut a small piece of ginger (about 1-2 sq inch),  chop it up fine and then do the same with a couple of green chillis. Add this to the eggs with a little bit of milk when beating the eggs … and then cook the same way as we cooked above. Ginger and chillis add that extra bite to the Omlette and the milk just makes it fluffier.

Level 3 – Vegetables:

Okay its time to get a little extravagant with the dish … if you chop up some Onions (finely) and Green peppers (again chop fine) and umm some Mushrooms … or even a little bit of spinach … you can take these and fry them once on the pan before cooking your garlic and eggs as described above.

I usually start with the butter in the pan, wait till the butter makes “Shwoosh” sound and before it turns brown/black I add the Garlic and cook that … then I add the Onions (use Red Onions if possible) and cook until they are soft and slightly brown. Then I add the Green Peppers and Mushrooms under very high heat … working quickly and tossing my veggies until they are slightly cooked. Finally I add the eggs, beaten with milk and some ginger and chillis. Cook the eggs in low heat, stirring the bottom and sides to fold the cooked portion in until most of it is cooked and then I let it settle. Top off with sliced tomatoes (thin or chunky) and cheese on top (Feta is the best).

Finally  I cover up the whole thing, if possible with an aluminum foil and let it cook slowly for 5-10mins until I can smell the aroma.

I will never add these:

So you experiment and the goal is not just to come up with the next best thing but also to keep good notes of what not to put into an omlette … somethings are just not right for it

– Curry Leaves (I tried it once and the leaves broke the nicely sealed Omlette plus it tasted too much like curry  – my Omlette should smell like eggs and veggies)

– Big chunky vegetables from the frozen section of your fridge Ex: Cauliflower florets and Brocolli florets – if you don’t cook them well enough they will make your eggs had to fit into a sandwich plus they retain the freezer smell (yuck!),

– Chicken (Why?!)

– Soggy tomatoes – dry roast or remove seeds before adding tomatoes to my Omlette please –  but then when you cook its yours so do as you please!

…..

– “Kaali Daal and Chicken” – yes you heard me! It is a bit of an inside joke between my friend and me, and a big lesson learned at the same time.

…..

[ to add more here over time]