Beav Petrie wrote:
Hi Oscar,

I have codes in shell script that I did
as follows:

#! /usr/bin/sh
mysql -t -u beavpetrie  -pmypassword <<MYSQL
    use systems;
    SELECT uid, pid, tty,  ppid, pr, tty, time, command
            FROM  processes
                LIMIT 100;
MYSQL

I'd like to translate the code in Python.

Are you able to show a sample code ?


Yes, I can.

I'm using Fedora Core 5. Apart from Python
devel, I also have libdbi-dbd-mysql installed.

#cat pro_cess.py

import sys
import MySQLdb
import Admin
dbh = Admin.connect()
try:
        cursor = dbh.cursor ()
        cursor.execute ("SELECT .... FROM processes LIMIT 100")
        rows = cursor.fetchall ()
        for row in rows:
                print "| %4s|..." % (row[0], ...)
        cursor.close ()
except MySQLdb.Error, e:
        print "Oops, There is a problem !"
        print e
dbh.close ()
sys.exit (0)


#cat Admin.py

import sys
import MySQLdb
def connect ():
        host_name = "localhost"
        db_name = "processes"
        user_name = "beavpetrie"
        password = "mypassword"
        try:
                dbh = MySQLdb.connect (db = db_name,
                        host = host_name,
                        user = user_name,
                        passwd = password)
                return dbh
        except MySQLdb.Error, e:
                print "Cannot connect to server"
                print "Error code:", e.args[0]
                print "Error message:", e.args[1]
                sys.exit (1)

Have not tested the above codes. Let me know if it
does not work.

Hope this helps.

O Plameras

_______________________________________________
coders mailing list
coders@slug.org.au
http://lists.slug.org.au/listinfo/coders

Reply via email to